Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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');  
    }
    */
    ?>
    Last edited by Rowsdower!; 06-11-2010 at 06:58 PM.
    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

  • #7
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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

  • #10
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •