Jump to content

Recommended Posts

Hey guys.

I have a simple php script, a counter that register unique visitors to my page. I would like to add "last visit time" to the database. I would aslo like a counter in the db that counts how many time a Ip adress has visited my page. How can this be done?

 

here is the script:

<?php

include('includes/config.php'); // include the db conection file

 

$ip = $_SERVER['REMOTE_ADDR']; //Get user IP

$check = mysql_query("SELECT * FROM users_online WHERE ip = '$ip'"); //Check the database for the IP

$check_ip = MYSQL_NUM_ROWS($check);

if($check_ip == 0) //If the Query shows 0 results

{

mysql_query("INSERT INTO users_online (ip) VALUES ('$ip')"); //Add the IP to the Database

}

 

$show = mysql_query("SELECT * FROM users_online"); //Get all the data from the database for showing the count

$show2 = MYSQL_NUM_ROWS($show); //Count the rows

echo "Unike sidetreff: $show2";  //Echo the # of unique hits

?>

It looks pretty close. I think that you should have a table with three columns:

1) ip (unique, make sure it is a constraint in the DB)

2) count (number of visits by user)

2) last_visit (time stamp of last visit)

 

Then to set the count and time stamp, do something like this:

INSERT INTO users_online (ip, count, last_visit) VALUES ('$ip', 1, now()) ON DUPLICATE KEY UPDATE users_online SET count = (count+1), last_visit = now() WHERE ip = '$ip'

 

Then one little select will get you all the data you need:

SELECT count, last_visit FROM users_online WHERE ip = '$ip'

Thank you for helping. I'm pretty fresh to php coding.

What will the full code be??

 

My db looks like this:

 

id          int(80) No                 auto_increment

ip          varchar(255)         latin1_swedish_ci No

last_visit varchar(255)         latin1_swedish_ci No

You would need to remove the auto-increment and make ip unique. The way your current setup would work, it would insert a new row EVERY time ANYONE visits your site. That will add up. My suggestion will only add a row for each unique IP address.

<?php
$ip = $_SERVER['REMOTE_ADDR']; //Get user IP

/* to get user info... */
$check = mysql_query("SELECT count, last_visit FROM users_online WHERE ip = '$ip'"); //Check the database for the IP
$row = mysql_fetch_assoc($check);
$uservisits = $row['count']; // this is the user's visit count
$userlaston = $row['last_visit']; // this is the timestamp of the last time the user logged on

/* to update user info */
$query = "INSERT INTO users_online (ip, count, last_visit) VALUES ('$ip', 1, now()) ON DUPLICATE KEY UPDATE users_online SET count = (count+1), last_visit = now() WHERE ip = '$ip'";
$result = mysql_query($query);
?>

 

That should basically do it. You may have to play with it a little, but you should get the idea.

I could't get it to work, but i found some scripts and put them together and got them to work.

 

<?php
include("config.php");
$sql = mysql_query("SELECT * FROM `ip_hits`");
while ($hit = mysql_fetch_array($sql)) {

}

$date = gmdate(j);
$month = gmdate(F);
$sup = gmdate(S);
$year = gmdate(Y);
$datestamp = $date. '' .$sup. ' ' .$month. ' ' .$year. '';
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'])
      ?  $_SERVER['HTTP_X_FORWARDED_FOR']
      :  $_SERVER['REMOTE_ADDR'];

$hit_sql = mysql_query("SELECT * FROM `ip_hits` WHERE `ip` = '" .$ip. "'");
if (mysql_num_rows($hit_sql) == "0") {
  mysql_query("INSERT INTO `ip_hits` ( `ip` , `clicks` , `date` ) VALUES ('" .$ip. "', '1', '" .$date. "')");
}
else {
  $hit = mysql_fetch_array($hit_sql);
  $new_hits = $hit["clicks"] + 1;
  mysql_query("UPDATE `ip_hits` SET `clicks` = '" .$new_hits. "', `date` = '" .$datestamp. "' WHERE `ip` = '" .$ip. "'");
}


$show = mysql_query("SELECT * FROM ip_hits"); //Get all the data from the database for showing the count
$show2 = MYSQL_NUM_ROWS($show); //Count the rows
echo "Unike sidetreff: $show2";  //Echo the # of unique hits

?>

 

It shows Unique Ip's, Total clicks, and last date online

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.