SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Move to new server-- now php script fails

    Hi There

    We have a script/process that we use to display popularity
    (i.e. Top Ten Articles) of various content groups.

    Essentially, we're tracking pageviews to a MySQL db,
    and then weekly, we create static pages of the totals.
    Then, we DELETE the table and start over for the next week.

    The script has run trouble free for years. Then, we cutover to
    a new server and the script no longer works.


    Specifically, the 'counter' does not increment the count
    in the MySQL table. No errors.

    I'm wondering if there might be a php configuration/compatibility
    issue on the new server?

    I'd appreciate feedback. Thanks.

    This is how it works:
    The code below is placed in the page to be tracked.
    Code:
    <img src="counter.php">

    And the script below increments the count

    Code:
    <? 
    
    /* 
    
    Author: Erik Boeters <erik@westland.ath.cx> 
    Website: http://westland.ath.cx/ 
    
    */ 
    
    // Database settings 
    DEFINE("SERVER", "server"); 
    DEFINE("DATABASE", "db"); 
    DEFINE("USERNAME", "user"); 
    DEFINE("DBPASSWORD", "pass"); 
    
    // IP's you want to ban seperated by spaces 
    DEFINE("DENYIPS", ""); 
    
    // Administrator password for the admin interface, example: stats.php?a=vs&password=**** 
    // If no password is given the admin interface will be available to anyone. 
    DEFINE("ADMINPASSWORD", "image0823"); 
    
    
    // Not neccesary to modify. 
    
    $db = mysql_connect(SERVER, USERNAME, DBPASSWORD); 
    
    function checkPageExits($page) { 
        $queryGetPages = "SELECT * FROM pagecounter3"; 
        $resultGetPages = mysql_db_query(DATABASE, $queryGetPages) or die ("Query failed: error was ".mysql_error()); 
        while($row=mysql_fetch_array($resultGetPages)) { 
            if (stristr($row["page"], $page)) { 
                $r = true; 
            } 
        } 
        if ($r) { 
            return true; 
        } else { 
            return  false; 
        } 
    } 
    
    function createPageEntry($page) { 
        if (isset($page)) { 
            $queryGetPages = "INSERT INTO `pagecounter3` (`pageID`, `page`, `visits`) VALUES ('', '$page', '1')"; 
            $resultGetPages = mysql_db_query(DATABASE, $queryGetPages) or die ("Query failed: error was ".mysql_error()); 
            return true; 
        } else { 
            return false; 
        } 
    } 
    
    function incrementVisits($page,$ip) { 
        $denyips = explode(" ", DENYIPS); 
        if (!in_array($ip, $denyips)) { 
            if (isset($page)) { 
                if (checkPageExits($page)) { 
                    $queryIncrement = "UPDATE pagecounter3 SET visits = visits + 1 WHERE page='$page'"; 
                    $resultIncrement = mysql_db_query(DATABASE, $queryIncrement) or die ("Query failed: error was ".mysql_error()); 
                    return true; 
                } elseif(createPageEntry($page)) { 
                    return true; 
                } else { 
                    return false; 
                } 
            } else { 
                return false; 
            } 
        } else { 
            return false; 
        } 
    } 
    
    switch ($a) { 
    default: 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
    header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
    header ("Pragma: no-cache"); // HTTP/1.0 
    header ("Content-type: image/png"); 
    $im = @ImageCreate (30, 10) 
    or die ("Cannot Initialize new GD image stream"); 
    $white = ImageColorAllocate ($im, 255, 255, 255); 
    $trans = imagecolortransparent($im,$white); 
    ImagePng ($im); 
    incrementVisits ($HTTP_REFERER,$REMOTE_ADDR); 
    break; 
    
    } 
    mysql_close($db); 
    ?> 
    ?>

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Id suggest this line

    incrementVisits ($HTTP_REFERER,$REMOTE_ADDR);


    needs updating, where are you setting these variables, I suspect you had register_globals switched on with your old server. Try

    incrementVisits ($_SERVER['HTTP_REFERER'], $_SERVER['REMOTE_ADDR']);
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes, thank you.
    No-- that didn't resolve it...

    Other ideas?

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmueller0823 View Post
    Mandes, thank you.
    No-- that didn't resolve it...

    Other ideas?
    Like I said before, where are you getting the $HTTP_REFERER and $REMOTE_ADDR values from then. My money is still on this line.

    Oh, just seen another thing, mysql_db_query is depreciated now you should use mysql_query with mysql_select_db to select the database to use
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where are you getting the $HTTP_REFERER and $REMOTE_ADDR values from
    I'm not a php developer... but my guess is the values
    are passed by the page that calls the script.

    The script is called from a page with this code:
    <img src="counter.php">

    Does that make sense?

    ther thing, mysql_db_query is depreciated now you should use mysql_query with mysql_select_db to select the database to use
    So... replace mysql_db_query with mysql_query... correct?
    Where should I use mysql_select_db ?

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've got a few issues with your script. There's an extra ?> at the end which is may be breaking the image output. As Mandes pointed out you've got a few problems here and there with deprecated functions, and you're using the GD library (used for creating images) which probably isn't available on your new server. Try this code:
    PHP Code:
    <?php
    /*
    Author: Erik Boeters <erik@westland.ath.cx> 
    Website: http://westland.ath.cx/ 
    */

    // Database settings
    DEFINE("SERVER""server");
    DEFINE("DATABASE""db");
    DEFINE("USERNAME""user");
    DEFINE("DBPASSWORD""pass");

    // IP's you want to ban seperated by spaces
    DEFINE("DENYIPS""");

    // Administrator password for the admin interface, example: stats.php?a=vs&password=****
    // If no password is given the admin interface will be available to anyone.
    DEFINE("ADMINPASSWORD""image0823");

    // Not neccesary to modify.
    $db mysql_connect(SERVERUSERNAMEDBPASSWORD);

    function 
    checkPageExits($page)
    {
        global 
    $db;
        
    $queryGetPages "SELECT * FROM pagecounter3";
        
    $resultGetPages mysql_query($queryGetPages$db) or die ("Query failed: error was ".mysql_error());
        while(
    $row=mysql_fetch_array($resultGetPages))
        {
            if (
    stristr($row["page"], $page))
            {
                
    $r true;
            }
        }
        if (
    $r)
        {
            return 
    true;
        } else
        {
            return  
    false;
        }
    }

    function 
    createPageEntry($page)
    {
        global 
    $db;
        if (isset(
    $page))
        {
            
    $queryGetPages "INSERT INTO `pagecounter3` (`pageID`, `page`, `visits`) VALUES ('', '$page', '1')";
            
    $resultGetPages mysql_query($queryGetPages$db) or die ("Query failed: error was ".mysql_error());
            return 
    true;
        }
        else
        {
            return 
    false;
        }
    }

    function 
    incrementVisits($page,$ip)
    {
        global 
    $db;
        
    $denyips explode(" "DENYIPS);
        if (!
    in_array($ip$denyips))
        {
            if (isset(
    $page))
            {
                if (
    checkPageExits($page))
                {
                    
    $queryIncrement "UPDATE pagecounter3 SET visits = visits + 1 WHERE page='$page'";
                    
    $resultIncrement mysql_query($queryIncrement$db) or die ("Query failed: error was ".mysql_error());
                    return 
    true;
                }
                elseif(
    createPageEntry($page))
                {
                    return 
    true;
                }
                else
                {
                    return 
    false;
                }
            }
            else
            {
                return 
    false;
            }
        }
        else
        {
            return 
    false;
        }
    }

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header ("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    header ("Cache-Control: no-cache, must-revalidate");
    header ("Pragma: no-cache");
    header ("Content-type: image/png");
    $im = @ImageCreate (3010) or die ("Cannot Initialize new GD image stream");
    $white ImageColorAllocate ($im255255255);
    $trans imagecolortransparent($im,$white);
    ImagePng ($im);
    incrementVisits ($_SERVER['HTTP_REFERER'],$_SERVER['REMOTE_ADDR']);
    mysql_close($db);
    ?>
    If that doesn't work, I think you may not have GD enabled in your new PHP configuration. Run phpinfo() and see if there is an entry for GD.

  7. #7
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Akash, thank you.

    I copied your code and tested. Still, not working.
    It won't increment existing records-- for non-existing, it won't
    Insert a new record.

    GD appears to be enabled...
    GD Support = enabled
    GD Version =bundled (2.0.28 compatible)
    FreeType Support= enabled
    FreeType Linkage =with freetype
    T1Lib Support = enabled
    GIF Read Support = enabled
    GIF Create Support = enabled
    JPG Support = enabled
    PNG Support = enabled
    WBMP Support = enabled
    XBM Support = enabled
    JIS-mapped Japanese Font Support = enabled

    Other ideas? I very much appreciate your help guys.

  8. #8
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you checked to see if the value of $page is what it should be or if it is even being set at all. And if your functions return a boolean value you could also use that to check if the query was even ran. That is my suggestion. Because your code only runs the query if the variable of $page is set.
    James
    New links coming soon....

  9. #9
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you checked to see if the value of $page is what it should be or if it is even being set at all. And if your functions return a boolean value you could also use that to check if the query was even ran. That is my suggestion. Because your code only runs the query if the variable of $page is set.
    Exactly.

    I'd like to look at those values.

    How would I do that?

    Something like:
    <?php echo $page; ?> or print($page);

    Tried both and no display. Thanks again.

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Put this line at the top of your script, then report back what you get.

    PHP Code:
    error_reporting(E_ALL); 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put this line at the top of your script, then report back what you get.
    error_reporting(E_ALL);
    Okay, tried that. Nothing displays.

    I tried launching the script directly with a url
    and also by refreshing a page containing the image (<img src="counter.php">)

    Should "error_reporting" only display if there are errors?
    Or will it display all variables every time?

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmueller0823 View Post
    Exactly.

    I'd like to look at those values.

    How would I do that?

    Something like:
    <?php echo $page; ?> or print($page);

    Tried both and no display. Thanks again.
    PHP Code:
    var_dump($var); 
    Will tell you about any variable and all the variables in it as well (if it's a array or object).


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
  •