Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-11-2010, 03:37 PM   PM User | #1
ironking
New to the CF scene

 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ironking is an unknown quantity at this point
Redirect based on Referrer URL - not working correctly...

Hi,

I'm very very very new to PHP, just got my "learn php" ebook a few days ago.

I'm trying to redirect visitors to a page on my website based on the referrer url.

I have this:

PHP Code:
<?php
$domain
='blankds.com';
$referrer $_SERVER['HTTP_REFERER'];
echo 
$referrer;
if (@
preg_match("/$domain/",$referrer)) {
    
header('Location: [url]http://www.blackisgreen.org/page_1.php');[/url]
    } else {
    
header('Location: [url]http://www.blackisgreen.org/page_2.php');[/url]
};
?>
Which works... however I get the "Warning: Cannot modify headers..." error because I am echoing $referrer. Now before you say - take out the echo - I have tried. The script simply does not work if I take out the echo.

Any suggestions please?

Thanks

Last edited by ironking; 06-11-2010 at 03:51 PM..
ironking is offline   Reply With Quote
Old 06-11-2010, 04:07 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Quote:
Originally Posted by ironking View Post
Hi,

I'm very very very new to PHP, just got my "learn php" ebook a few days ago.

I'm trying to redirect visitors to a page on my website based on the referrer url.

I have this:

PHP Code:
<?php
$domain
='blankds.com';
$referrer $_SERVER['HTTP_REFERER'];
echo 
$referrer;
if (@
preg_match("/$domain/",$referrer)) {
    
header('Location: [url]http://www.blackisgreen.org/page_1.php');[/url]
    } else {
    
header('Location: [url]http://www.blackisgreen.org/page_2.php');[/url]
};
?>
Which works... however I get the "Warning: Cannot modify headers..." error because I am echoing $referrer. Now before you say - take out the echo - I have tried. The script simply does not work if I take out the echo.

Any suggestions please?

Thanks
It means that you have already sent headers to the user's browser when you echo'ed the http referrer. You cannot use any output before sending a specific PHP header unless you buffer your output (which is more advanced than what you need to know at this stage).

Basically, just remove the echo statement and it should work fine. Have you checked your preg statement?

You shouldn't need quotes around the variable.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 06-11-2010, 04:16 PM   PM User | #3
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
I should probably explain more. Take a look at this sample from php.net's page on preg_match:

PHP Code:
<?php
$subject 
"abcdef";
$pattern '/^def/';
preg_match($pattern$subject$matchesPREG_OFFSET_CAPTURE3);
print_r($matches);
?>
To simplify this to look more like your code it would be something like this:
PHP Code:
<?php
$domain
='blankds.com';
$subject $_SERVER['HTTP_REFERER'];
$pattern '/'.$domain.'/';
preg_match($pattern$subject$matches);
print_r($matches);
?>
Notice that the pattern string actually contains the regex boundaries.

So, you have two options:
PHP Code:
<?php 
$domain
='blankds.com'
$referrer $_SERVER['HTTP_REFERER']; 
echo 
$referrer
if (@
preg_match("/".$domain."/",$referrer)) { 
    
header('Location: [url]http://www.blackisgreen.org/page_1.php');[/url
    } else { 
    
header('Location: [url]http://www.blackisgreen.org/page_2.php');[/url
}; 
?>
OR:
PHP Code:
<?php 
$domain
='/blankds.com/'
$referrer $_SERVER['HTTP_REFERER']; 
echo 
$referrer
if (@
preg_match($domain,$referrer)) { 
    
header('Location: [url]http://www.blackisgreen.org/page_1.php');[/url
    } else { 
    
header('Location: [url]http://www.blackisgreen.org/page_2.php');[/url
}; 
?>
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 06-11-2010, 05:06 PM   PM User | #4
ironking
New to the CF scene

 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ironking is an unknown quantity at this point
Hey Rows,

Thanks for the comments, I understood about 10% of what you said. It didn't work. For some reason removing the echo ends the script without redirecting and I get a browser error.

I tried both permutations of what you posted, with and without the echo.

I understand that I can't echo before sending headers... so what is the work around?
ironking is offline   Reply With Quote
Old 06-11-2010, 06:05 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
White screen with no redirect would be indicating a parser error; however, since the modify warning showed so should this.
Try this
PHP Code:
<?php
$domain
='blankds.com';
$referrer $_SERVER['HTTP_REFERER'];
if(@
preg_match("/$domain/",$referrer))
{
    
header('Location:http://www.blackisgreen.org/page_1.php');
    exit(); 
}
else
{
    
header('Location:http://www.blackisgreen.org/page_2.php');
    exit(); 
}
?>
Also, be aware that HTTP_REFERER is not guaranteed to exist.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 06-11-2010, 06:55 PM   PM User | #6
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Quote:
Originally Posted by ironking View Post
Hey Rows,

Thanks for the comments, I understood about 10% of what you said. It didn't work. For some reason removing the echo ends the script without redirecting and I get a browser error.

I tried both permutations of what you posted, with and without the echo.

I understand that I can't echo before sending headers... so what is the work around?
Whoops! Yeah, I forgot to delete the echo. Also, when I wrote my reply the forum here parsed your URL's as links and put in some [url] tags that are not doing you any favors. Lastly, I'll warn you that I am garbage with regex statements but I think any remaining problem is probably right there.

I think that there needs to be a carat "^" in there like so, and without the [url] tags:

PHP Code:
<?php
$domain
='/^blankds.com/';
$referrer $_SERVER['HTTP_REFERER'];
if (@
preg_match($domain,$referrer)) {
    
header('Location: http://www.blackisgreen.org/page_1.php');
    } else {
    
header('Location: http://www.blackisgreen.org/page_2.php');
}
?>
Try that. If removing the [url][/url] tags from the code I first pasted doesn't work either then read on...


--------------

Anyway, like I said, I am garbage at making regex patterns. Try removing the "@" symbol in front of the preg_match and see if we are generating any errors. If so, paste them verbatim in a reply here.

Also, try running this code once and see what prints to screen:
PHP Code:
<?php  
$domain
='/^blankds.com/';  
$referrer $_SERVER['HTTP_REFERER'];  
preg_match($domain,$referrer,$test);
print_r$test );
/*
if (@preg_match($domain,$referrer)) {  
    header('Location: http://www.blackisgreen.org/page_1.php');  
    } else {  
    header('Location: http://www.blackisgreen.org/page_2.php');  
}
*/
?>
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

Last edited by Rowsdower!; 06-11-2010 at 06:58 PM..
Rowsdower! is offline   Reply With Quote
Old 06-11-2010, 07:15 PM   PM User | #7
ironking
New to the CF scene

 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ironking is an unknown quantity at this point
Big whoops - we must have posted at the same time Rowsdower - trying that now!

Thanks folks.

Neither option is working.

Perhaps Im going about this the wrong way. Essentially I am trying to restrict access to a page - lets call it the "member special page" - to a certain link.

Here's what happens. As a member of "blankds" I login and then get access to a link which takes me to "blackisgreen". If I have logged into "blankds" and clicked the link I am allowed to see "member special page" on "blackisgreen".

If I have not come from the link on blankds I am redirected to the standard page on blackisgreen.

Essentially the if / else statement should work, but without echoing the $referrer value it simply doesn't. I have even tried .htaccess to turn on output buffering, tried php.ini files etc. those dont work to get rid of the header warning either.

Any suggestions of a better way to accomplish what I need are welcome.

Thanks.

Last edited by ironking; 06-11-2010 at 07:18 PM..
ironking is offline   Reply With Quote
Old 06-11-2010, 07:21 PM   PM User | #8
ironking
New to the CF scene

 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ironking is an unknown quantity at this point
Rowsdower:

Option 1: no error message - but redirects me to page_2.

Option 2: prints out Array() - but also stays on page_1 no matter where I redirect from.

Option 3: remove @ =
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
* This problem can sometimes be caused by disabling or refusing to accept cookies.

thanks

Last edited by ironking; 06-11-2010 at 07:24 PM..
ironking is offline   Reply With Quote
Old 06-12-2010, 09:33 PM   PM User | #9
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
If you're getting a never-ending loop then this redirect script must be referring to itself or to another page with the redirect script, which then redirects to another page with the redirect script, etc.

Now when you printed out "Array()" (which is the retult of a blank array being printed with print_r) and didn't redirect that is the anticipated behavior. What this is telling us is that the preg_match is not finding a match - which means you have a problem with your regex (regular expression). If it had found a match, there would have been something in the array.

To make matters simple, just switch the preg_match() search to a strpos() search - you can find this function on php.net. It's simpler to use but less powerful. For your purposes here, however, it will be just fine.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 06-14-2010, 11:05 AM   PM User | #10
ironking
New to the CF scene

 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ironking is an unknown quantity at this point
Yeh still had major issues... then a friend pointed out something:

Code:
<?php
$domain='blankds.com';
$referrer=$_SERVER['HTTP_REFERER'];
if (strstr($referrer, $domain) === false) {
	header('Location: http://www.blackisgreen.org/page_2.php');
	exit();
	} else {
	echo "Welcome Referred visitor.";
};
?>
The second redirect was redirecting to the same page that had the code - ie. endless loop. Remove it, place the "special" content on the page with that at the top of the code and we have a working model.

thanks to all.

Last edited by ironking; 06-14-2010 at 11:44 AM..
ironking is offline   Reply With Quote
Reply

Bookmarks

Tags
http_referer, redirect, referrer

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:38 PM.


Advertisement
Log in to turn off these ads.