SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question simple php causing high server load

    Hello ,

    I have been contacted by my hosting company ... got the following email :


    I apologize, but I was forced to suspend the script "public_html/.../count.php" as it was causing a high load on the server, and due to the nature of the problem, I forced to take immediate action for the health of the server.
    my php/mysql only takes a Post variable and (get the record or update the record)


    here is the simple php code :

    PHP Code:

    function get_total() {

    // get the total numbers
    $sqlstr = @mysql_query("select total from total where id = 1");

    if (@
    mysql_num_rows($sqlstr) != 0) {
        while (
    $row = @mysql_fetch_array($sqlstr)) {
                    
    $num number_format($row[total]);
                    echo 
    "<h1> $num  </h1>";
        }
      }
    }


    if(
    $_POST['count'] == 1) { get_total(); mysql_close($link);}

     else if(
    $_POST['count'] == 2) {




    @
    mysql_query("UPDATE  total set total = total + 1 where id = 1") or die("ERROR: You are Going too Fast !!");


    get_total();

    mysql_close($link);


    Anyone see a problem with the code above or anyway i can make it better ?

    What i might be doing wrong : i'm calling this page using ajax by 2 way :

    - if a user click on a button (update the row = row + 1 )
    - every 15 seconds (just get the value)


    the problem is that for every user on the page there is a process :

    Running Proceses:
    ... 7296 0.0 0.0 8192 944 ? S 21:04 0:00 pure-ftpd (IDLE)
    ... 12396 0.0 0.1 27600 10808 ? SN 21:08 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 14367 0.0 0.1 27600 10808 ? SN 21:09 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 16388 0.0 0.1 27600 10808 ? SN 21:10 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 18604 0.0 0.1 27600 10808 ? SN 21:12 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 19409 0.0 0.1 27600 10812 ? SN 21:13 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 19472 0.0 0.1 27604 11192 ? SN 21:13 0:00 /usr/bin/php /home/.../public_html/.../index.php
    ... 20767 0.0 0.1 27600 10808 ? SN 21:14 0:00 /usr/bin/php /home/.../public_html/.../count.php
    ... 21192 0.0 0.1 27604 11192 ? SN 21:14 0:00 /usr/bin/php /home/.../public_html/.../index.php
    ... 23153 0.0 0.1 27604 11188 ? SN 21:15 0:00 /usr/bin/php /home/.../public_html/.../index.php
    ... 24941 0.2 0.1 27604 11192 ? SN 21:17 0:00 /usr/bin/php /home/.../public_html/.../index.php
    ... 25401 1.2 0.1 27600 10812 ? SN 21:17 0:00 /usr/bin/php /home/.../public_html/.../count.php
    and mysql process :

    xxx.161.213.173 ....com /count.php
    xxx.161.213.173 ....com /count.php
    xxx.48.198.248 ....com /count.php
    xxx.48.198.248 ....com /count.php
    xxx.48.198.248 ....com /count.php
    xxx.51.25.143 ....com /count.php
    xxx.12.236.124 ....com /count.php
    xxx.12.245.160 ....com /count.php
    xxx.14.242.162 ....com /count.php
    xxx.14.242.162 ....com /count.php
    xxx.14.242.162 ....com /count.php
    xxx.200.196.48 ....com /count.php
    xxx.236.228.150 ....com /count.php
    xxx.150.159.212 ....com /count.php
    xxx.71.97.109 ....com /count.php
    xxx.71.97.109 ....com /count.php
    xxx.71.97.109 ....com /count.php
    xxx.71.97.109 ....com /count.php
    xxx.71.97.109 ....com /count.php

    Any Help would be appreciated

    Thank You
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  2. #2
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    function get_total() {
        
    $sqlstr mysql_query("SELECT total FROM total WHERE id = 1");

        foreach (
    mysql_fetch_array($sqlstr) as $row) {
            
    $num number_format($row['total']);
            echo 
    "<h1> $num  </h1>";
        }
    }

    if (
    $_POST['count'] == 1)  { 
        
    get_total();
        
    mysql_close($link);
    } else if (
    $_POST['count'] == 2) {
        
    mysql_query("UPDATE total SET total = total + 1 WHERE id = 1") or die("ERROR: You are Going too Fast !!");
        
    get_total();    
        
    mysql_close($link);
    }
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you think that simple (if & foreach loop) might be causing the trouble ?

    what about when i start a session will that make a new process in php ?
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  4. #4
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by commandos View Post
    do you think that simple (if & foreach loop) might be causing the trouble ?

    what about when i start a session will that make a new process in php ?
    If you have other files included which I expect you do since you're using mysql_close($link) then any one of those could be causing the slow down, from I can see, what you're currently doing wouldn't be a problem.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See if you can replicate the issue at a local/testing machine. If you can, then use xdebug to profile it and find out where the bottleneck is.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The other include is just the db info :
    PHP Code:
    $link mysql_connect($host$user$pass);
    @
    mysql_select_db($db_name); 
    i also had this to prevent external calls :

    PHP Code:

    session_start
    ();

    // Prevent this file from being called Directly

    if ($_SESSION['whatever'] != 'whatever&^%') { echo "ACCESS DENIED" ; die(); }

    $ref  $_SERVER["HTTP_REFERER"];

    if  ((
    $ref != "http://www.domain.ca/") && ($ref != "http://domain.ca/")) { echo "Access Denied"; die();} 

    i removed this code temporarily to see if this was causing the "processes"
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still dident work

    it used 25 of 25 allowed process
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Where you use the foreach loop to fetch the results of the query, try changing it to a while loop.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was a while loop then Ripe Suggested to use the foreach ..

    now i'm not using both of them ... since i only have 1 row and only will have 1 row wrote the following code :

    PHP Code:

    // get the total number
    $sqlstr mysql_query("select total from total where id = 1");


    $row mysql_fetch_array($sqlstr) ;
    echo 
    "<h1>"number_format($row['total']) ."</h1>"
    it's still causing "too much processes" .... anyone know how in the server a process is counted ? or why a process is not stopped directly
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure maybe all this processes are from ajax ?

    because the php page is being called from another page using ajax .

    so whenever a user clicks the button this script is called using ajax or every 15 seconds automatically .

    maybe i should remove the "close mysql connection" since the same user might click many time on a button instead of opening / closing mysql connection every time ?
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  11. #11
    SitePoint Member foogoo's Avatar
    Join Date
    Mar 2010
    Location
    Vancouver Island, BC, Canada
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure about what creates processes, but I know using @ to suppress errors makes it take longer to execute. It has to set error level to none, then restore it after every line with @.

    I would start by creating a test page with only the commands to connect, select the database, run the query and display that value. If that still causes process issues, then it must be something in the web host's configuration.

    If a basic query works without creating too many processes, I would then make it run multiple queries in the same file to test whether that puts too much load on the server. How many queries would depend on the traffic you receive and how much the rest of your web site queries the database. If you run into errors, you might need to reduce the number of queries to see just how many you can do at once. Maybe find a better database server or reduce the demand by increasing the time between refreshes or using caches.

    If it works with many queries at once, then I would add components from the page causing errors until I found what was pushing it too far.

    Hope this helps.

  12. #12
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What is the purpose of the script? Is it to log user activity? Check your log files for any times with lots of activity from a single or small group of IP addresses which could possibly indicate a bot is being used, you mention in the OP in this thread that if a user clicks something, if there is a bot trying to access the site then it might possibly explain the hitting of the script.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


Bookmarks

Posting Permissions

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