Click to See Complete Forum and Search --> : Conditional Redirect Script?


lsample20
06-11-2008, 04:28 PM
Hey guys, I'm not a programmer so please excuse the newbie language I may use. I have very slight php experience and can def. copy and paste code if you guys can help me out!

Here is what I want to accomplish:

I need a redirect script that works conditionally. I need it to be able to redirect web visitors only if they came from a certain page. Otherwise, I don't want it to redirect...I want the visitor to stay there.

For an example, lets assume that I want to redirect the visitor if they came from page1 and arrived at page2. If they got to page2 by any other means, I don't want to redirect.

Again, if they got to page2 through page1, then redirect them.

Also, I don't want it to be conditional on a particular link from page1, because page1 is going to have a standard redirect on it. If that makes sense. The visitor will never actually see page 1 anyway because there is a redirect there, so the code on page2 should just recognize ANY visitor coming from page1 will be redirected.

How would you suggest that I pull this off?

Thanks a ton guys!

Declan1991
06-11-2008, 06:12 PM
This can be spoofed, and will not work for absolutely everyone, just most people.if (isset($_SERVER['HTTP_REFERER'])){
if (!$_SERVER['HTTP_REFERER'] == 'webpage full url'){
header('location: http://www.whatever.com/newurl.html');
}
}

lsample20
06-11-2008, 10:36 PM
Do I just copy this and paste it onto the page? In the body section?

I couldn't seem to get it to work.

Declan1991
06-12-2008, 06:58 AM
It's the very top of the page.<?php if (isset($_SERVER['HTTP_REFERER'])){
if (!$_SERVER['HTTP_REFERER'] == 'webpage full url'){
header('location: http://www.whatever.com/newurl.html');
}
} ?>
<html>
<head>
...
</body>
</html>

ayvegh
06-15-2008, 10:03 AM
I would suggest this:

<?php
if (isset($_SERVER['HTTP_REFERER']))
{
if (!$_SERVER['HTTP_REFERER'] == 'webpage full url')
{
header('location: http://www.whatever.com/newurl.html');
exit;
}
}
?>
<html>
<head>
...
</body>
</html>

It prevents any other code from running, so you don't get errors with sending headers after output has already started.

Cheers,
ayvegh

(sorry about the structuring - pet peeve ;))

Declan1991
06-15-2008, 11:35 AM
Good idea from ayvegh there; I fully agree with him (or her).

ayvegh
06-16-2008, 07:35 AM
I'm a "him" ;)

Thanks,
ayvegh