McChicken Posted October 6, 2008 Share Posted October 6, 2008 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/ Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 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' Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658098 Share on other sites More sharing options...
McChicken Posted October 6, 2008 Author Share Posted October 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658191 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 Well, you r table is different that what I suggested. It would be easier on you DB if you did what I suggested. Do you want me to suggest how to do it with your current setup, or with my suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658196 Share on other sites More sharing options...
McChicken Posted October 6, 2008 Author Share Posted October 6, 2008 id and ip was in the current script, but I can just rename them Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658201 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658208 Share on other sites More sharing options...
McChicken Posted October 6, 2008 Author Share Posted October 6, 2008 But the id auto is used by the counter that shows on my page. id int(80) Nei auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658227 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 Yes, ID is set to auto increment, but that means that ID increments EVERY time that a row is added, not uniquely incrementing per IP address. Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658231 Share on other sites More sharing options...
McChicken Posted October 6, 2008 Author Share Posted October 6, 2008 Ok...now database look like this ip varchar(255) latin1_swedish_ci No count varchar(255) latin1_swedish_ci No last_visit varchar(255) latin1_swedish_ci No What will the full code be? thanks Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658265 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658327 Share on other sites More sharing options...
McChicken Posted October 7, 2008 Author Share Posted October 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127234-solved-counter-script-add-last-visit-time/#findComment-658911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.