Prevent visitors from leaving the domain

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
max46
Forum Newbie
Posts: 1
Joined: Mon Nov 24, 2008 11:28 pm

Prevent visitors from leaving the domain

Post by max46 »

Hi,

I want to prevent visitors from leaving the website when they're on the page called 'count.php' only.
I mean if a visitor clicks on an external link on that page, nothing will happen. Only links with the domain as base url will be active.


tried with a javascript:
----------------------------------------------------------
function stopEvent(e) {
if(!e) var e = window.event;

//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = false;

//e.stopPropagation works only in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
-----------------------------
but that's not enough.


Any idea???

Thanks!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Prevent visitors from leaving the domain

Post by aceconcepts »

Why do you want to do this?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Prevent visitors from leaving the domain

Post by Mark Baker »

modify count.php so that any "external" links aren't written as links but as plain text.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Prevent visitors from leaving the domain

Post by Eran »

To stop a link from redirecting you should be concerned only with preventDefault and not bubbling. Your script only runs preventDefault for Gecko based browsers (firefox), it should be something like this:

Code: Select all

 
function stopEvent(e) {
   if(!e) var e = window.event;
   e.preventDefault();
}
 
You still need to attach the event to all the links (A tags), which you can do by using the onclick handler or with DOM methods. For the latter I'd recommend a javascript library such as jQuery as there are many cross-browser differences in event handling.
Post Reply