JesseToxik Posted April 26, 2013 Share Posted April 26, 2013 I have been googling for this solution with no luck. I want to create a page on my site that a user goes to when they click a link that will take them off of my site. The way Facebook does. How can I use PHP to detect if a link is taking someone off of my site, and if so redirect them to a page where they can confirm they wish to leave the site?I know Facebook uses a URL like so to keep track of the URL. http://site.com/exit.php?url=http://othersite.com Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/ Share on other sites More sharing options...
yomanny Posted April 26, 2013 Share Posted April 26, 2013 You could send them to a page called exit.php and along with it send a parameter with the new URL, like this: confirm.php?url=http://yahoo.com Then, in exit.php you fetch the url, something like: $url = $_GET['url']; echo '<a href="' . $url . '">Click here to leave my website.</a>'; Hope it helps! - W Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426706 Share on other sites More sharing options...
ignace Posted April 26, 2013 Share Posted April 26, 2013 (edited) Something like this in jQuery (assumes all your internal URL's are relative) $('[href^="http"]').click(function(e) { e.preventDefault(); window.location = 'http://yoururl.com/exit.php?url=' . encodeURIComponent(this.href); }); Edited April 26, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426770 Share on other sites More sharing options...
JesseToxik Posted April 26, 2013 Author Share Posted April 26, 2013 @yomanny thats basically what I am looking for, but how can I add to the script to detect if its a link on my site or an external link? @ignance I like your idea but I am trying to use PHP so if it is on mobile(the mobile portion of my site) or a javascript disabled browser it will still work. Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426782 Share on other sites More sharing options...
JesseToxik Posted April 26, 2013 Author Share Posted April 26, 2013 This is what I have setup.http://vampirenews.byethost12.com/exit.php?url=http://test.com Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426786 Share on other sites More sharing options...
Psycho Posted April 26, 2013 Share Posted April 26, 2013 @yomanny thats basically what I am looking for, but how can I add to the script to detect if its a link on my site or an external link? You need to be more specific when you say "on my site". If the user is clicking a favorite for example, you have no way to add the function that you are asking. As for links that are displayed within your site that point to URLs outside your site, YOU have control of the content on your site. You will need to change all those external links from <a href="http://www.anothersite.com">http://www.anothersite.com</a> To <a href="exit.php?url=http://www.anothersite.com">http://www.anothersite.com</a> Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426787 Share on other sites More sharing options...
JesseToxik Posted April 26, 2013 Author Share Posted April 26, 2013 @Psycho I understand that, but let's say I add a forum to my site.If a user posts a link to an external site, how can PHP detect the link does not link to my site, and automatically converts it from <a href="http://www.anothersite.com">http://www.anothersite.com</a> to <a href="exit.php?url=http://www.anothersite.com">http://www.anothersite.com</a> Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426788 Share on other sites More sharing options...
jcbones Posted April 27, 2013 Share Posted April 27, 2013 You might be able to find if your site name exists in the string with strpos. Quote Link to comment https://forums.phpfreaks.com/topic/277328-confirm-site-leave/#findComment-1426832 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.