googlexx Posted January 5, 2010 Share Posted January 5, 2010 Hey guys, My question is simple. Is there a way to only allow one person on a page? If someone is viewing the page and then another user tries to view it. It then just redirects them or something? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 What scenario are you wishing to achieve this? Is the redirect permanent? What I'd recommend is creating a table, and when the page is viewed it enters 'viewed = 1' for example and if another user comes on it'll have that value and you can appropriately redirect them, A cron job can set 'viewed' back to false if you wish it to reset. Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988694 Share on other sites More sharing options...
googlexx Posted January 5, 2010 Author Share Posted January 5, 2010 alright that makes sense. Now when they leave the page how do i set it back to 0? I don't know what a cron job is. Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988699 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 Well, essentially there is no method of doing this unless you: A) Make them click an exit link, which will expire the session (if the exit this will not happen) B) Create a cron job and set the table to refresh at a set time if no one is 'logged' to the page. Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988700 Share on other sites More sharing options...
Deoctor Posted January 5, 2010 Share Posted January 5, 2010 start a session when they visit the page and when session is there make it to 1 and when the session got killed make it as 0 Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988701 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 start a session when they visit the page and when session is there make it to 1 and when the session got killed make it as 0 That could work as well, but it'd be a timed session, meaning the person cannot view the page after a certain amount of time (albeit an easier solution). Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988704 Share on other sites More sharing options...
googlexx Posted January 5, 2010 Author Share Posted January 5, 2010 alright this is what i have so far a couple problems im having: it's not logging the ip's and i am not able to be on the page 2 times on the same ip. i'm also trying to make it so when someone is online and they are the only person on it lets them. But when someone else tries to connect I would like it to start like a 60 second timer. When that timer ends it redirects the person that waited the 60 seconds back to the site and removes the person already on the site. I know this is possible but how hard it is? I don't know php very well but I'll try my best with any help! <? //online $server = "localhost"; $db_user = ""; $db_pass = ""; $database = ""; $timeoutseconds = 20; // length of gaps in the count //get the time $timestamp = time(); $timeout = $timestamp-$timeoutseconds; //connect to database mysql_connect($server, $db_user, $db_pass); //delete values when they leave $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout"); if(!($delete)) { print ""; } //grab the results $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'"); if(!($result)) { print ""; } //number of rows = the number of people online $user = mysql_num_rows($result); //check if someone is online if($user >= 1) { ob_start(); header("Location: http://www.google.com"); ob_flush(); } else { //no1 is online - add them $insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')"); if(!($insert)) { print ""; } } mysql_close(); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988707 Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 There is no solution for what you want to do. There are work arounds but the fact is that there is no way to know exactly when a user exits a page. I can click a link that says goodbye but I could also walk away and leave the browser window open all night long. Maybe I just close the window, the heck with your exit link or button. It might help if you explained exactly what you are doing and why. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988789 Share on other sites More sharing options...
crabfinger Posted January 5, 2010 Share Posted January 5, 2010 Well you could have the user click a link to "Check out" the file and then it is in their name/ip address until they click a link to "Check in" the file, like in Joomla file edit. And as a fail safe, if they don't check out within a certain period of time or they don't refresh the page within a certain period of time (you can set the page to refresh with javascript if they are still viewing the page and therefore if they aren't viewing the page then the page won't refresh and the file will be checked back in)... or something like that Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988795 Share on other sites More sharing options...
googlexx Posted January 5, 2010 Author Share Posted January 5, 2010 let me try to explain it better: 1. A user joins a page. (User A) 2. He stays on the page for 10 minutes. 3. Another user joins that same page. (User B) 4. It tells (UserB) someone else is already on the page and starts a 60 second timer for them. 5. It goes back to the page (User A) is on and shows him that someone else is trying to go on the page. It then shows him the 60 seconds timer. 6. When the 60 second timer finishes it redirects (User A) off the page and redirects (User B) onto the page. I hope that makes sense. So far i have everything done except the part with the timer and redirecting. Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988800 Share on other sites More sharing options...
ignace Posted January 5, 2010 Share Posted January 5, 2010 You need Ajax to create the dynamic bit both for user A and B. However creating a table just specifically for this functionality is overkill but without a table it's hard to implement. The obvious choice would be if you store your sessions in a db table add the message to the session record that is viewing page X user A who is viewing the page then just retrieves in regular intervals it's session data. Then if you find the predefined variable(s) for page messages in the json response display it to the user viewing the page. Start an internal clock using setTimeout() (JS) that will redirect the user to the next page when the 60 seconds clock stops. Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-988974 Share on other sites More sharing options...
crabfinger Posted January 5, 2010 Share Posted January 5, 2010 creating a table just specifically for this functionality is overkill but without a table it's hard to implement. Wouldn't it be a lot easier to just use a file name 'lock.txt' next to the file with a value of 1 or 0? Quote Link to comment https://forums.phpfreaks.com/topic/187225-allow-only-1-user-on-site/#findComment-989231 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.