Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJackie Jen
2,723 Pointshow to allow 1 user to view an php page
let said i have 10 user in my database. I have example.php page. i wan to set a rule that if A user is view the example.php page the others user cannot view the example.php page.
how is the logic flow and how does it work? any idea?
Christian Andersson
8,712 PointsDo you want to set it so that only 1 user can view the page at a time?
Jackie Jen
2,723 PointsHi Robert & Andersson
Yes, I would like to set it so that only 1 user can view the page at a time
2 Answers
Christian Andersson
8,712 PointsWhat you are trying to do is not really possible to do with just plain convensional php. The problem is that php-code is run on the server just before data is sent to the user - so you cannot check if someone is on a page in real-time.
Another problem is also what you define as "someone viewing the page". For example, if I load the page and then just leave it there for 24 hours without doing anything, does that still count as viewing it?
So unless you absolutely have to have a code that checks this in real-time, you can do this quite easily. I would add in the database the date and time that your page was last accessed, then, when someone tries to open the page compare the current time with the time the page was last accessed. If the the difference in time is X minutes or more, allow the page to be viewed - otherwise don't allow.
Here is a pseudo-code explaining what I would do:
<?php
$lastAccess = get_last_accesstime_from_db();
$currentTime = time();
if ($currentTime - $lastAccess <= XXXX) {
//don't allow page to be viewed
} else {
//show page
}
?>
Also, if you want, you could add some javascript to redirect the user to another page once the X minutes of "activity" has ran out. Almost like a "kick" feature.
Though, I really can't imagine why you would want something like this. Perhaps you are trying to do something that can be approached in a better fashion? Do you want to explain why you want only 1 user to view the page at a time?
Best of luck.
Jackie Jen
2,723 PointsHi Andersson,
Thanks for great concept. kicking feature is great. i would like to have a 1st come 1st serve feeling. but i will set a timer so that it won't just leave it there for 24 hours without doing anything
Zahscha Gonzalez
27,775 PointsAre you doing object oriented? or are you running Mysql statements in your code?
That would be a whole new column on your table, where you are able to assign permissions to the users, or levels depending on what you want your users to see and interact with. To answer your question you should: prompt the user to log in. Once they log in check if they have the permissions to view that page if they don't then throw an error or redirect them to another page. Hope this helps!
Robert Walker
17,146 PointsRobert Walker
17,146 PointsSo other users are allowed to view this page, just not when another user is viewing it first?