traffic to a page first but another upon back button

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
jasonst78
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 12:01 pm

traffic to a page first but another upon back button

Post by jasonst78 »

Hi everyone,

I am wondering if there is a way to implement something into this code that; (1) if the referer is set properly from google.com, it will always send the initial traffic to http://mysiteA.com; (2) but when the back button is pressed it will send them to the other links besides http://mysiteA.com.

Code: Select all

<?php
$ref = $_SERVER["HTTP_REFERER"];
if (strpos($ref,'google.com')!==FALSE) {
$urls = array
("http://mysiteA.com",
"http://mysiteB.com",
"http://mysiteC.com",
"http://mysiteD.com",
"http://mysiteE.com");
 
$url = $urls[array_rand($urls)];
 
header("Location: $url");
}
?>
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: traffic to a page first but another upon back button

Post by Griven »

Just so I understand, let me recap...

If a user visits your website from Google, and presses the back button to leave your website, you don't want them to go back to Google.com? If I'm right, then this sounds like a surefire way to p!ss off your visitors. I know I hate it when sites do that--I tend to never go back.

However, if you REALLY want to do that...

Make the landing page of your site an instant redirect to another page. That way, when they click the back button, they'll just be redirected again.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: traffic to a page first but another upon back button

Post by jackpf »

I think browsers cache the redirects...so I'm not sure if that'd work. But yeah, this sounds like a rather annoying thing to do tbh.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: traffic to a page first but another upon back button

Post by Mirge »

jackpf wrote:But yeah, this sounds like a rather annoying thing to do tbh.
+1... I wouldn't be back.
jasonst78
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 12:01 pm

Re: traffic to a page first but another upon back button

Post by jasonst78 »

Well, actually the example I posted is just that, an example. The visitors will not necessarily be coming from Google. The reason I would like the visitors to be directed to another page upon pushing the back button is because I am receiving the initial traffic via a redirect. When the visitor makes an attempt to push the back button it simply refreshes the main page over and over and I agree with everyone this can be very annoying. Therefore, instead of the visitor continuing to see the same page when hitting the back button, I was hoping that I could redirect them to various other pages that I have listed in the array, with priority given to a chosen link. After the prioritized link is viewed once I was hoping to place a cookie that would prevent it from being viewed again.

@ jackpf - to prevent a cache from the redirect I have placed into the code:

Code: Select all

    header("cache-control: no-store, no-cache, must-revalidate"); 
    header("Pragma: no-cache");
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: traffic to a page first but another upon back button

Post by Griven »

I'm going to go out on a limb here and say that you can't do this. The reason being is that PHP is server-side code and only executes when a web client requests a page containing that code. By pressing the back button, the client is actually accessing a completely different page, the address of which is cached inside the browser. To do what you are describing, you would need to essentially hijack the user's browser by inserting what could only be described as malicious code (even if your intentions aren't malicious).

I believe the code that you displayed there controls whether or not the content of the page is cached--not the address.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: traffic to a page first but another upon back button

Post by jackpf »

Sorry for being stupid, but I still don't understand the point of doing this 8O

And yeah, I agree...it doesn't sound possible, since when the user hits the back button, the page is loaded from cache. Although, with your no-cache headers, I'm not sure what would happen...I'm not sure what kind of referrer the browser would send. You could give it a go though...by checking $_SERVER['HTTP_REFERER'].
Post Reply