PHP Live Counter problem!!

Discussion in 'QnA (read only)' started by ╬Switch╬, Jan 1, 2007.

Thread Status:
Not open for further replies.
  1. ╬Switch╬

    ╬Switch╬ New Member

    Joined:
    Nov 19, 2006
    Messages:
    287
    Likes Received:
    0
    Trophy Points:
    0
    I got this code from webdesign.org it does work,
    Code:
    <? 
    //DBase Name: 
    $opt_connected_cfgbase = "base"; 
    //Username : 
    $opt_connected_cfguser = "user"; 
    //PassWord : 
    $opt_connected_cfgpass = "pass"; 
    //Path : 
    $opt_connected_cfghote = "localhost"; 
    // Amout of time to consider a user active 
    $nb_connected_connexion=300; // 5 minutes 
     
    // Connexion to the database 
    $base_connected=mysql_connect($opt_connected_cfghote, $opt_connected_cfguser,$opt_connected_cfgpass); 
    if (!$base_connected) 
    { 
    ("<center>Error connecting to the Database </center>"); 
    exit(); 
    } 
     
    // Database selecting 
    if (! mysql_select_db("$opt_connected_cfgbase",$base_connected)) 
    { 
    mysql_close($base_connected); 
    print("<center>Error connecting to the Database</center>"); 
    exit(); 
    } 
     
    // time 
    $time_connected=date("U"); 
     
    // Get IP 
    [B][COLOR=red]$ip_connected=$REMOTE_ADDR[/COLOR][/B]; 
     
    // Search IP in the DB 
    $query_connected="select * from nb_connected where ip='$ip_connected'"; 
    $result_connected=@mysql_query($query_connected,$base_connected); 
     
    if (!$result_connected) 
    { 
    mysql_close($base_connected); 
    print("<center>Could not execute request </center>"); 
    exit(); 
    } 
     
    $nb_connected=@mysql_num_rows($result_connected); 
    if ($nb_connected) 
    { 
    // Update the connexion 
    $query_connected="update nb_connected set time='$time_connected' where ip='$ip_connected'"; 
    $result_connected=@mysql_query($query_connected,$base_connected); 
    } 
    else 
    { 
    // Create connexion 
    $query_connected="insert into nb_connected (ip, time) values ('"; 
    $query_connected.=$ip_connected; 
    $query_connected.="', '"; 
    $query_connected.=$time_connected; 
    $query_connected.="')"; 
    $result_connected=@mysql_query($query_connected,$base_connected); 
    } 
     
    // Calculate maximum time of connexion 
    $time_max_connected=$time_connected-$nb_connected_connexion; 
     
    // Deleting Maximum time connexion from DB 
    $query_connected="delete from nb_connected where time<'$time_max_connected'"; 
    $result_connected=@mysql_query($query_connected,$base_connected); 
     
    // Close connexion to DB 
    mysql_close($base_connected); 
    ?>
    But it is displayed along with the following error:
    Notice: Undefined variable: REMOTE_ADDR in d:\web\tools\easyphp\www\counter\index.php on line 35

    Can anyone help me get it right:?:
     
    Last edited: Jan 1, 2007
  2. utsavhanda

    utsavhanda New Member

    Joined:
    May 14, 2005
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    INDIA
    Hello there,

    Use,

    $_SERVER['REMOTE_ADDR']

    instead of

    REMOTE_ADDR

    Hope this helps you out.

    Regards
    Utsav HAnda
     
  3. tuxfan

    tuxfan New Member

    Joined:
    Feb 4, 2004
    Messages:
    2,653
    Likes Received:
    9
    Trophy Points:
    0
    Location:
    Mumbai
    This needs a MySQL database. Hope you have created one and connected with this! However, I have seen simpler PHP counters that don't need a DB. They are lighter on the system.
     
  4. OP
    OP
    ╬Switch╬

    ╬Switch╬ New Member

    Joined:
    Nov 19, 2006
    Messages:
    287
    Likes Received:
    0
    Trophy Points:
    0
    @utsav handa:
    Ill try that.Thanks.
    __________
    @tuxfan
    The couneter I have above counts the number of people online at that instant.
    The ones you mention (ones which are made using text files) count number of visits to a page.
     
    Last edited: Jan 7, 2007
  5. tuxfan

    tuxfan New Member

    Joined:
    Feb 4, 2004
    Messages:
    2,653
    Likes Received:
    9
    Trophy Points:
    0
    Location:
    Mumbai
    Ah ok :) That one is also a useful indicator.
     
  6. OP
    OP
    ╬Switch╬

    ╬Switch╬ New Member

    Joined:
    Nov 19, 2006
    Messages:
    287
    Likes Received:
    0
    Trophy Points:
    0
    @utsav handa:
    Thankx
    $_SERVER['REMOTE_ADDR'] worked!!:)
     
Thread Status:
Not open for further replies.

Share This Page