Jump to content

Visitors counter


greekscientist

Recommended Posts

I found a script at the net but it have a small mistake. I tried 3 hours but i can`t get it work.

 

The problem is i believe at those lines

$get_ip = mysql_query("SELECT * FROM ".$db_table." WHERE vis_ip=".$vis_ip." LIMIT 1");
     while ($row=mysql_fetch_object($get_ip))

It doesn`t get inside the while to execute the next lines

 

the whole script is that

<?php

// config variables
$tframe = 1000;    // time frame (minutes) to count active users

// database connection details
$db_host = "localhost";                 // hostname of your MySQL server. You most likely don't have to change this
$db_name = "**_fruits";        // database name
$db_user = "**_apples";        // database user
$db_pass = "**";    // database password
$db_table= "visits";                    // table name

// Lets open up a connection to the database
$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");

// On to the counter processing...
$vis_ip = ip2long($_SERVER['REMOTE_ADDR']);

$time = time();
$new_vis = 1;

// update database for returning visitor
$get_ip = mysql_query("SELECT * FROM ".$db_table." WHERE vis_ip=".$vis_ip." LIMIT 1");
     while ($row=mysql_fetch_object($get_ip))
         {
         mysql_query("UPDATE ".$db_table." SET vis_time='$time' WHERE vis_ip='$vis_ip'") 
            or die (mysql_error());
        $new_vis = 0;
        }
// add to database for new visitor
if ($new_vis == 1)
    {
    mysql_query("INSERT INTO ".$db_table." (vis_ip, vis_time) VALUES ('$vis_ip','$time')") 
        or die (mysql_error());
    }

// done processing the visit, now lets see how many total visitors are online
$tcheck = time() - (60 * $tframe);
$query = mysql_query("SELECT * FROM ".$db_table." WHERE vis_time > $tcheck");
$onlinenow = mysql_num_rows($query);

// show number of visitors on screen
if($onlinenow == 1)
    {
    echo"currently $onlinenow visitor online";
    }
else
    {
    echo"currently $onlinenow visitors online";
    }
?>  

Link to comment
Share on other sites

Basically the script should show the unique visitors.

 

I upload it and tested it. O refresh the page and it count 1,2,3 + unique visitors.

 

It should only show 1 unique visitors at that situation.

 

The thing is that it doesn`t execute those code after the while loop. Is something like ... search sql table for a record. If its exist update it, if not insert new record. That script only enters new records.

Link to comment
Share on other sites

your code needs alot of cleaning up and right now i'm too lazy to do it, like for instance you don't need a while loop if you are expecting only one output row for your query, makes no sense, and you can most certainly learn from someone elses script that works.. i never said copy and past it...i asked you a question 3 times and you just answered it now..pfff

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.