restricting users for opening multiple pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
urboyfriend
Forum Newbie
Posts: 11
Joined: Tue Sep 06, 2011 6:29 pm

restricting users for opening multiple pages

Post by urboyfriend »

is it possible to restrict users from opening multiple instance of a similar page?

for example, a user opened home.php, if he/she tried to open another instance of the said page, a warning will be given that he/she is already browsing a similar page.

If so, may i know any link for learning? ty for the reply! :D
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: restricting users for opening multiple pages

Post by greip »

This is hard to implement well on the web and goes against the basic design of the web.

There is only a very loose coupling between the web browser and the server. There is no way for the server to know for sure when the user stop looking at a web page. Yes, you can trap the unload event, but there is no unload event when the browser crashes, or if the user chose to move to a different PC without closing down his current session.

Read a bit about the HTTP protocol and try to find a design for your project which is more aligned with the loose coupling between web browsers and the web server.
doubleprime
Forum Newbie
Posts: 1
Joined: Sun Sep 11, 2011 5:59 pm

Re: restricting users for opening multiple pages

Post by doubleprime »

There are ways to do this, but I wouldn't recommend it. I'm trying to imagine a scenario where this might sound like a good idea, and I honestly can't think of a reason to prevent a user from having the same page open in multiple windows.

For the sake of education, however, I'll play along and brainstorm a bit. If you're doing this to try and be "helpful" to the user (and please, just don't do this. It's not helpful. It's annoying and users will probably never return to your site because of it.), then you probably don't need server-side code at all. You could probably write a bit of Javascript that writes a cookie when a user opens a page and pops up a warning if the cookie is already written. Set it to unwrite the cookie when the page is closed and you're finished. Just don't get "clever" and make it "help" the user by auto-closing the page or something dumb like that, in case of a scenario like greip describes.

If the point of your code is security of some form, I have a major caveat. If you have a content-oriented site and you're trying to keep visitors from scraping all the content automatically, this just isn't going to work. You could hypothetically get really evil and load your content in an unconventional way (read it from a database via Ajax so the content isn't in the source), but those sorts of things are trivial for a determined scraper to overcome. More importantly, for users who might have a legitimate reason to open multiple copies of a page (I don't know why, but I'm sure there are people who do), it's just going to greatly inconvenience them and make them dislike your site, probably losing your visitors. If that doesn't daunt you (and please, let it. Actually doing this would be an act of war against good web design standards everywhere.), I can imagine a system like this working using Ajax and PHP. Essentially, at the server side, you could have a Javascript that pings a PHP script for a unique ID and then stores it in a local variable. Then, the Javascript could constantly update the PHP server with a list of pages the user has open. The PHP script could check the list of open pages against each incoming page request and, if it detects that a page is already open, respond with an error, which could cause the Javascript to refuse to load the page. Again, this can still be overcome and would greatly inconvenience regular users who may, for example, accidentally open two copies of the same page in different tabs and then be (relatively) harshly admonished for a simple mistake.

For the sake of education, I've answered your question, but please don't actually use any of this information to do what I've described. While it may seem useful to you, it will inevitably anger your users and make them stay away from your site. There is nothing convenient about being told that you already have another copy of the page open. If I accidentally open two copies of the same page, I don't care. I'll close both of them eventually. It's the same with most users. If this is some attempt at preventing content scraping, you'll also anger users and it'll be even worse because it just rewards the people who access your content elsewhere while punishing the people who visit your site directly. If you really need a way to keep people from scraping content en masse I would simply recommend throttling page download speeds for excessive users and leaving it at that.
Post Reply