Fsoft Posted October 11, 2008 Share Posted October 11, 2008 Hey, I am trying to make this simple script but I am really stuck :( The script is to tell that how much users are online at the moment viewing this page; <?php session_start(); $connect = mysql_conntect_db("host","user","pass"); mysql_select_db("test"); if($_SESSION["is_online"] == "i_know") { //do normal script work }else { $query1 = mysql_query("select online_id from sessions"); $query1_data = mysql_fetch_array($query1); $online = $query1_data["online_id"]; $online_now = $online + 1; $query_online_send = mysql_query("insert into sessions (online_id) values (\"$online_now\")); $_SESSION["is_online"] = "i_know"; echo "<meta http-equiv=\"REFRESH\" content=\"0;URL=script.php\">"; } $query3 = mysql_query("select online_id from sessions"); $online_now_at_the_moment = $query3["online_id"]; echo "Total users online : $online_now_at_the_moment"; ?> Finaly, this code is working good enough, but the problem is that it is just increasing the users viewing each time a user views "so it's a kind of hit counter" not "how much online" system.. I want to make it how much online.. It means before showing total number of user_id online, it will do a query and find out all the users online 20 mins before and remove them, only show up the number of users active in last 20 mins.. I know in this manner while writing the query of user becoming online I will have to write some other thing a long with adding users online number into database. But I don't know what I will have to write :( Please help me, by writing a very simple code showing me how I can do it? Please very simpel so that I can also understand.. Please help, Badly waiting for answer, Thanks a lot, FAISAL! Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/ Share on other sites More sharing options...
unrelenting Posted October 11, 2008 Share Posted October 11, 2008 http://www.phpfreaks.com/forums/index.php/topic,151897.0.html Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-662967 Share on other sites More sharing options...
smithmr8 Posted October 11, 2008 Share Posted October 11, 2008 The way which I would do it, to make sure its not just adding the same person to your counter again, if they view the page again would be like below. Create a table called 'online_now'. Fields: ID @ int, IP @ varchar, lastview @ TIMESTAMP <?php //The Viewers IP Address $ip=$_SERVER['REMOTE_ADDR']; //Checks to see whether their IP is already in the table $sql = mysql_query("SELECT * FROM `online_now` WHERE `IP` = '$ip'"); if(mysql_num_rows($sql) == '0'){ //If their IP is not in the table, it will add it, along with the viewing time. mysql_query("INSERT INTO `online_now` (`IP`, `lastview`) VALUES ('$IP', 'NOW()')"); } else { //If their IP is in the table, then it will update their last viewing time. mysql_query("UPDATE `online_now` SET `lastview` = 'NOW()' WHERE `IP` = '$ip'"); } //To display the number of viewers from the last 20 minutes $20mins = mysql_num_rows(mysql_query("SELECT * FROM `online_now` WHERE 'NOW()' - `lastview` <= '1200'")); echo $20mins." different viewers in the last 20 minutes.<br>"; ?> Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-662986 Share on other sites More sharing options...
Fsoft Posted October 11, 2008 Author Share Posted October 11, 2008 The way which I would do it, to make sure its not just adding the same person to your counter again, if they view the page again would be like below. Create a table called 'online_now'. Fields: ID @ int, IP @ varchar, lastview @ TIMESTAMP <?php //The Viewers IP Address $ip=$_SERVER['REMOTE_ADDR']; //Checks to see whether their IP is already in the table $sql = mysql_query("SELECT * FROM `online_now` WHERE `IP` = '$ip'"); if(mysql_num_rows($sql) == '0'){ //If their IP is not in the table, it will add it, along with the viewing time. mysql_query("INSERT INTO `online_now` (`IP`, `lastview`) VALUES ('$IP', 'NOW()')"); } else { //If their IP is in the table, then it will update their last viewing time. mysql_query("UPDATE `online_now` SET `lastview` = 'NOW()' WHERE `IP` = '$ip'"); } //To display the number of viewers from the last 20 minutes $20mins = mysql_num_rows(mysql_query("SELECT * FROM `online_now` WHERE 'NOW()' - `lastview` <= '1200'")); echo $20mins." different viewers in the last 20 minutes.<br>"; ?> Well, Thanks , Thank you very much for such detailed reply with script code, so sweet, Well but I do have some questions again, 1st : $_SERVER['server_addr']; when it's uploaded on server, it shows up the IP of server not the users IP... "for me atleast"... 2nd : NOW() ? what is this? 3rd : $20mins = mysql_num_rows(mysql_query("SELECT * FROM `online_now` WHERE 'NOW()' - `lastview` <= '1200'")); The red part I haven't understood, can you please explain a bit to me??? Thanks a lot, FAISAL! Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-662993 Share on other sites More sharing options...
smithmr8 Posted October 11, 2008 Share Posted October 11, 2008 1) It returns the IP address of the user viewing the page. Try it yourself, by putting it on your website and echo the result. Then confirm your own IP address by using one of the many websites on the internet or check your internet details, and they'll be the same . 2) NOW() is a mysql function which returns a timestamp of the current time/date. 3) If I put it in a different way, its a bit easier to understand. <?php $sql1 = mysql_query("SELECT * FROM `online_now` WHERE 'NOW()' - `lastview` <= '1200'"); $20mins = mysql_num_rows($sql1); ?> Simply put, in the table 'online_now', you have IP address's and the timestamp of when they last visited your website. NOW() = The current timestamp. By taking the 'lastview' timestamp of when they last viewed the page away from the current one, we are going to get the time difference between the two in seconds. So, for the last 20 minutes its (20 x 60Seconds) which is 1200. The query, $sql1, is looking for any visitors where the difference between NOW() and their last visit recorded is less than or equal to 1200 seconds. mysql_num_rows() is another function which tells you how many different rows a query found in a table with your specified information. So by doing mysql_num_rows($sql1) we can find out how many people have been on the site in the last 20 minutes. Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-662998 Share on other sites More sharing options...
Fsoft Posted October 11, 2008 Author Share Posted October 11, 2008 WOW, Great, clear enough... but one more question <?php $time = time(); $time_less = 300; $time_final = $time - $time_less; echo "<br>"; echo $time; echo "<br>"; echo $time_final; ?> in this code, time will echo out the result in seconds? and $time_less = 300; that 300 is 300 seconds?? And in MySQL NOW() will also put out time in same format as php does with time() ? Thanks a lot, FAISAL! Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-663009 Share on other sites More sharing options...
Stooney Posted October 11, 2008 Share Posted October 11, 2008 If you want the users IP, it's $_SERVER['REMOTE_ADDR']; $_SERVER['SERVER_ADDR']; is the IP of the server the script is on. Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-663015 Share on other sites More sharing options...
smithmr8 Posted October 11, 2008 Share Posted October 11, 2008 I would assume so, yes. Link to comment https://forums.phpfreaks.com/topic/128029-solved-simple-script-making/#findComment-663028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.