1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP referrer redirect script help. just need this tweaked a little

Discussion in 'PHP & Perl' started by turbotec, Nov 10, 2008.

  1. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    so I am currently using a php script to check referrer and then redirect accordingly.

    Code:
    <?php
    	$referer = $_SERVER['HTTP_REFERER'];
    	if($referer == "http://www.mysite.com/test/testreferrerredirect.php")
    	{
    		echo '<meta http-equiv="refresh" content="0;url=http://www.pyromosh.org/images/bbs/have_a_cookie.gif">';
    	}
    	else
    	{
    		echo '<meta http-equiv="refresh" content="0;url=http://www.youfail.org/">';	
    	}
    ?>
    
    


    SO, right now if the referrer is "http://www.mysite.com/test/testreferrerredirect.php" than it will do what I want. Here is my question though. I want it to redirect to the correct site IF the referrer is anywhere from "mysite.com" not just the php page above. does this make sense?? I tried just making the value equal "http://www.mysite.com/" but it didnt work. how can I change the script to redirect if it is coming from anywhere on the mysite.com page??
     
  2. nextgen

    nextgen Newbie

    Joined:
    Oct 16, 2008
    Messages:
    2
    Likes Received:
    1
    PHP:
    <?php
        $referer 
    $_SERVER['HTTP_REFERER'];
        if(
    preg_match("/www.mysite.com/i"$referer))
        {
            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.pyromosh.org/images/bbs/have_a_cookie.gif">';
        }
        else
        {
            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.youfail.org/">';    
        }
    ?>
     
    • Thanks Thanks x 1
  3. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    hey alright that works good. now one more question relating to the same thing. how can i get it to work from only www.mysite.com/test ?


    sorry Im so bad at this PHP stuff.
     
    Last edited: Nov 10, 2008
  4. scubaslick

    scubaslick Regular Member

    Joined:
    Aug 23, 2007
    Messages:
    392
    Likes Received:
    512
    Just change:

    PHP:
    "/www.mysite.com/i"
    to:
    PHP:
    "/www.mysite.com/test/i"
    That's a followed by a /. The is just an escape character telling php to treat the next / as part of the string it's testing for a match instead of the end of the string initiated by the / after the first quote.

    On a side note, my brain hurts. :)

    edit Bleh. the board is interpreting things too.

    Take out all the asterisks: "/ww*w.*mysite.*c*om**/test/i"

    double edit. Dammit. I don't have any idea how to force it to let me give you the code. lol

    I'll have to fight with it a bit. Might be easier just attach a file. Give me a sec.

    edit the last time: try this file. rofl.
     

    Attached Files:

    • Thanks Thanks x 1
    Last edited: Nov 10, 2008
  5. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    ok I get it. thanks a ton man!! this is similar a regex when writing in VB code!!
     
  6. nextgen

    nextgen Newbie

    Joined:
    Oct 16, 2008
    Messages:
    2
    Likes Received:
    1
    yes it is regular expresion. very powerful tool for matching strings.
     
  7. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    ok so now how can I add a list of good referrers? do you just stack them up? for example:

    PHP:
    <?php
        $referer 
    $_SERVER['HTTP_REFERER'];
        if(
    preg_match("/www.mysite.com/i"$referer))
        if(
    preg_match("/www.yoursite.com/i"$referer))
        if(
    preg_match("/www.hissite.com/i"$referer))
        if(
    preg_match("/www.hersite.com/i"$referer))
            {
            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.pyromosh.org/images/bbs/have_a_cookie.gif">';
        }
        else
        {
            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.youfail.org/">';    
        }
    ?>

    would that be an effective way to add multiple good referrers to accept or is there a better way?
     
  8. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    bump.....this code is working great for me with just one referrer....but like i said above, how can I get it to look for 1 of say 4 referrers and then continue on with the code?
     
  9. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    PHP:
    <?

    if((
    preg_match("/www.hersite.com/i"$referer)) OR (preg_match("/www.hissite.com/i"$referer)) OR (preg_match("/www.yoursite.com/i"$referer)) OR (preg_match("/www.mysite.com/i"$referer)))//if referer is one of this 4
            
    {

            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.pyromosh.org/images/bbs/have_a_cookie.gif">';

        }

        else

        {

            echo 
    '<meta http-equiv="refresh" content="0;url=http://www.youfail.org/">';    

        }


        
    ?>
     
    • Thanks Thanks x 1
  10. turbotec

    turbotec Junior Member

    Joined:
    Apr 5, 2008
    Messages:
    181
    Likes Received:
    15
    that did it thanks guys. one day I will be as good at php as you guys.