PHP Redirection Problem

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
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

PHP Redirection Problem

Post by abcdabcd »

I'm not an experienced php coder, I just understand basic html. I'm working on a script I found and it's supposed to redirect you to a different website if you come from a specific link or have a referer. If for example: you type in the url to WEBSITEA.COM into the web browser directly there wouldn't be a referer so you would see the content of WEBSITEA.COM and you would not be redirected. If there is a referer, example: you came from a link on another website (http://xxxxxx.com/link.php) then insted of seeing WEBSITEA.COM you will be redirected to another website (WEBSITEB.COM) and you will not see WEBSITEA.COM.

The problem is that the php code on WEBSITEA.COM is not redirecting to WEBSITEB.COM even if there is a referer. I tested by creating a test.html page in a different folder. I checked the headers by using a FF plug-in called, "Tamper Data". When it gets to WEBSITEA.COM where it's supposed to redirect in "Temper Data" it says the referer is /Test/Test.htm so there is a referer. Although it's not redirecting to WEBSITEB.COM

Here's the code:

Code: Select all

<?php
 
if ( $_SERVER['HTTP_REFERER'] == "http://xxxxxx.com/link.php" ) {
Header ("Location: http://WEBSITE-B.COM"); exit(0);
 
 
} else {
 
 
}
 
?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

I put in the html tags since this is where the content of WEBSITEA.COM starts if you come to the website with no referer.

I think the problem is with the referer. Since in the above code it's redirecting based on the referer which it's specifying as: http://xxxxxx/link.php. However what I noticed by using, "Tamper Data" is that, the referer is being logged as the site that the link to link.php is placed on. So, if I place a link to http://xxxxx/link.php on Test/Test.htm it's going to specify the referer is Test/Test.htm not http://xxxxx/link.php as in the code above.

I tried modifying the code for testing purposes and changing the server referer line to /Test/Test.htm since this is what it is in "Tamper Data" and it still didn't work.

If I type in the url of WEBSITEA.COM directly into the address bar, I do end up getting the content of WEBSITEA.com so this part is working. It's just not redirecting to WEBSITEB.com if you come through a referer.

If it makes any difference, http://xxxxxx.com/link.php which links to WEBSITEA.COM is always going to be the same link, http://xxxxxx.com/link.php. This link.php is important in determining where the user is redirected. It doesn't matter to me what the referer is since the actual website that links to http://xxxxx.com/link.php will change.


I've been searching for google and trying to set this up for several hours!

Any help would be appreciated,



Thanks!
jothirajan
Forum Commoner
Posts: 69
Joined: Tue Jan 27, 2009 12:06 am

Re: PHP Redirection Problem

Post by jothirajan »

No problem....

Here it may be....

Why you are holding the exit(0); option there.

Please explain me your problem...As your posted content is not clear to me..

Help is always there

Thanks
JOE
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

Re: PHP Redirection Problem

Post by abcdabcd »

jothirajan wrote:No problem....

Here it may be....

Why you are holding the exit(0); option there.

Please explain me your problem...As your posted content is not clear to me..

Help is always there

Thanks
JOE

thanks for your reply. like I said, I found this script online, I'm not a php programmer I only have basic html knowledge. I don't know what exit(0) is for, I'll remove it and test to see if it works.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: PHP Redirection Problem

Post by watson516 »

abcdabcd wrote:

Code: Select all

<?php
 
if ( $_SERVER['HTTP_REFERER'] == "http://xxxxxx.com/link.php" ) {
Header ("Location: http://WEBSITE-B.COM"); exit(0);
 
 
} else {
 
 
}
 
?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
The $_SERVER['HTTP_REFERER'] holds the url from the referring website. Thus, if you check to see if this is equal to "http://xxxxxx.com/link.php", you will only get redirected if that website was the referrer. I don't think (I could be wrong) that $_SERVER['HTTP_REFERER'] contains anything if the address of WebsiteA was typed into the address bar. Therefore, something like this might work.

Code: Select all

<?php
if($_SERVER['HTTP_REFERER'])
{
         header('Location: http://www.websiteB.com/');
         exit();
}
?>
<html>
...
jothirajan
Forum Commoner
Posts: 69
Joined: Tue Jan 27, 2009 12:06 am

Re: PHP Redirection Problem

Post by jothirajan »

Hello abcdabcd

No problem..

Clearly explain me what you want to have.....

I will help you...

Thanks
JOE
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

Re: PHP Redirection Problem

Post by abcdabcd »

watson516 wrote:
abcdabcd wrote:

Code: Select all

<?php
 
if ( $_SERVER['HTTP_REFERER'] == "http://xxxxxx.com/link.php" ) {
Header ("Location: http://WEBSITE-B.COM"); exit(0);
 
 
} else {
 
 
}
 
?>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
The $_SERVER['HTTP_REFERER'] holds the url from the referring website. Thus, if you check to see if this is equal to "http://xxxxxx.com/link.php", you will only get redirected if that website was the referrer. I don't think (I could be wrong) that $_SERVER['HTTP_REFERER'] contains anything if the address of WebsiteA was typed into the address bar. Therefore, something like this might work.

Code: Select all

<?php
if($_SERVER['HTTP_REFERER'])
{
         header('Location: http://www.websiteB.com/');
         exit();
}
?>
<html>
...

THANKS!! THAT FIXED THE PROBLEM!!
jothirajan
Forum Commoner
Posts: 69
Joined: Tue Jan 27, 2009 12:06 am

Re: PHP Redirection Problem

Post by jothirajan »

This is my 2.php


<?php
echo "<a href='1.php'>Click</a>";
?>



and this is my 1.php

<?php

if($_SERVER['HTTP_REFERER']=="http://localhost/test_jo/2.php")
{
header('Location: http://www.websiteB.com/');

}
?>

>>>Remove the exit();
>>> give the full URL http://localhost/test_jo/2.php;
>>> Meantime use this code
print_r($_SERVER['HTTP_REFERER']);

to find what are all the referers that you got....

I am getting the exact output...

Thanks
JOE.... Get me if you want more to have
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

Re: PHP Redirection Problem

Post by abcdabcd »

With this method that works, the original referer where http://xxxxxx.link/php is linked from is stated in "Tamper Data". Is it possible to modify that referer so that it is the referer of WEBSITEA.COM ?

Example:

RANDOMWEBSITE.COM has a link on their site to http://xxxxxx.com/link.php user clicks /link.php link and goes to WEBSITEB.COM based on the php redirect on WEBSITEA.COM. Currently in "Tamper Data" it is displaying the refer as: RANDOMWEBSITE.COM can this be modified so that the refer is WEBSITEA.COM ? or can the referer be modified to ANY other site besides RANDOMWEBSITE.COM ? Even if I have to create another website on a different domain it's ok with me as long as the referer can be modified to not be RANDOMWEBSITE.COM


THANKS!
Post Reply