www.webdeveloper.com
Results 1 to 7 of 7

Thread: High CPU issue on Server caused by my php Script

  1. #1
    Join Date
    Mar 2011
    Posts
    13

    Exclamation High CPU issue on Server caused by my php Script

    I need help with what I'm trying to do, my scripting knowledge is very low so any help will be appreciated!

    Short description of my script,
    it's designed to look up every user of my site that has put their streaming detail for a streaming site and check if they are online or offline, once it finds out it updates the online/offline field.

    Now I setup cron to run the script every 5-10minutes so it would stay updated, which isn't the problem.

    The problem I'm having is that now that more and more people got added to the database, the script takes longer to finish and puts lots of strain on the cpu.

    I'm very beginner at writing php and just put it together with reading tutorials so I'm pretty sure I coded it poorly.

    Here is the script:
    Code:
    <?php
    $username="username";
    $password="password";
    $sqlx="databasename";
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($sqlx) or die( "Unable to select database");
    $query="SELECT * FROM users";
    $resultx=mysql_query($query);
    $num=mysql_numrows($resultx);
    ?>
    
    <?php
    $i=0;
    while ($i < $num) {
    $f2=mysql_result($resultx,$i,"username");
    $f8=mysql_result($resultx,$i,"xfire");
    ?>
    
    <?php if($f8['xfire'] != "") { ?>
    <?php
    ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-EU; rv:1.8.1.9) Gecko/20071025 Firefox/3.6.15');
    if(!function_exists('get_string_between')) {
    function get_string_between($string, $start, $end)
    {
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0)
            return "";
        $ini += strlen($start);
        $len = strpos($string,$len,$ini) - $ini;
        return substr($string,$ini,150);
    }}
    $data = file_get_contents("http://www.xfire.com/live_video/$f8/");
    $ratingdiv = get_string_between($data, '<span class="label">', '<div class="game_product_icon_pair">');
    $ratingtext = strip_tags($ratingdiv);
    $find = 'Unknown'; 
    if(strpos($ratingtext, $find )){
    echo "<font color='#FF0000'>Offline</font>";
    mysql_query("UPDATE users SET xfirecheck ='0' WHERE xfire = '$f8'");
    }
    else{
    echo "<font color='#00FF00'>Online</font>";
    mysql_query("UPDATE users SET xfirecheck ='<font color=#00FF00>Online</font>' WHERE xfire = '$f8'");
    }
    ?>
    <?php } ?>
    <?php
    $i++;
    }
    mysql_close();
    ?>
    Any help will greatly be appreciated if possible.

  2. #2
    Join Date
    Apr 2006
    Location
    UK
    Posts
    8
    Is it your hosting provider who is saying that the CPU usage is too high? Are you on shared hosting?

    Although I've never used that Xfire API that you're using, nothing looks to far out of line to me. How many users are being added to the script each time the cronjob runs?

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    1) Your code is jumping in and out of the php processor all the time. i.e. ?> followed by <?php get rid of those.
    2) You have a function defined inside of a loop. Move the function outside of the loop. Put at the top of your code.
    3) You are using very inefficient code with mysql_result() which gets only one value at a time. Would be much more efficient with mysql_fetch_array(). Fill the array, then process the array.

  4. #4
    Join Date
    Mar 2011
    Posts
    13
    Solved problem guys, thanks for your time anyways!

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by iosys View Post
    Solved problem guys, thanks for your time anyways!
    Glad to hear it. Could you share your solution?
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  6. #6
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  7. #7
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    PHP Code:
    <?php
    ini_set
    ('user_agent''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-EU; rv:1.8.1.9) Gecko/20071025 Firefox/3.6.15');
    mysql_connect("localhost","username","password");
    mysql_select_db("databasename") or die("Unable to select database");

    $resultx=mysql_query("SELECT username, xfire FROM users");
    if(
    mysql_errno()) die(mysql_error());


    while (
    $row mysql_fetch_assoc($result)) {
      if(
    $row['xfire']) {
        
    $data str_replace("\n"''file_get_contents("http://www.xfire.com/live_video/".$row['username']."/"));
        
    $data preg_match_all('/<div>Started: .{0,500}<div class="game_product_icon_pair">/'$data$matches);

        
    $ratingtext strip_tags(implode($matches));
        
        if(
    strpos($ratingtext'Unknown' )){ //Nice use of type juggling :) I like it!
          
    echo "<font color='#FF0000'>Offline</font>";
          
    mysql_query("UPDATE users SET xfirecheck ='0' WHERE xfire = '".mysql_real_escape_string($f8)."'");
        } else {
          echo 
    "<font color='#00FF00'>Online</font>";
          
    mysql_query("UPDATE users SET xfirecheck ='<font color=#00FF00>Online</font>' WHERE xfire = '$f8'");
        }
      }
    }
    mysql_close(); //also nice ;)
    ?>
    Thats so much easier to read. I know I broke it in a couple places (I could hardly translate what you coded). The last query is probably broken, also-- it makes no sense having echo "online | offline" without a username to go with it
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles