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

RE: Site changes based on referrer ...

Discussion in 'HTML & JavaScript' started by OTrap, Sep 17, 2012.

  1. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    If this is the wrong place to ask this, then my apologies, but I was curious:

    I'm looking to make a webpage render one way if it is accessed through a very specific redirect, but another way if it is accessed in any other way.

    For example, if I set up http://www.website.com/redirect/ to redirect to my page, I want my page to render a certain way. But if someone accessed my page any other way, it would show up a totally different way.

    Is this doable?
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    Certainly, it 's called cloacking :)

    You need to use a server side language to check the referrer. If it is from your redirect page it will serve content A else content B etc.

    In PHP, it the $_SERVER['HTTP_REFERER'] (one R!) variable.
     
  3. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    Thanks, jazz.

    Here's what I have so far:
    Code:
    <?php if ($_SERVER['HTTP_REFERER']=="http://www.somewebsite.com/redirect.php") { ?>
    
    
    
    
    
    
    //INSERT HTML FOR IF REDIRECT IS TRUE
    
    
    
    
    
    <?php } else { ?>
    
    
    
    
    
    //INSERT HTML FOR IF REDIRECT IS FALSE
    
    
    
    
    
    <?php } 
     ?>
    Now, can I just put the entire chunk of HTML in there, from <html> to </html>?
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    Yup :)
     
    • Thanks Thanks x 1
  5. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    It actually appears as though it's not tracking the 'HTTP_REFERER'.

    As such, even when I get there via the redirect link, it still shows what happens if I don't.

    Is there some reason the above wouldn't track the referrer?


    If so, is it possible to track a session ID from Page A to Page B?
     
    Last edited: Sep 19, 2012
  6. sapo

    sapo Power Member

    Joined:
    Feb 25, 2008
    Messages:
    510
    Likes Received:
    281
    Yeah I used this to show different sites to different referrers, but if you replace the $site= to the html should work

    Code:
    <?php
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("/site1.info/",$referrer)) {
          $site="http://google.com/";
    } elseif (preg_match("/www.site1.info/",$referrer)) {
          $site="http://google.com/";
    } else {
          $site="http://yahoo.com/";
    };
    ?>
    so your code should look something like this

    Code:
    <?php
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("/site1.info/",$referrer)) {
          echo "HTML here";
    } elseif (preg_match("/www.site1.info/",$referrer)) {
          echo "HTML here";
    } else {
          echo "HTML here";
    };
    ?>
    remember with echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

    Code:
    echo '<input type="text">';
    Or you can escape them like so:

    Code:
    echo "<input type=\"text\">";
    hope this is what you were looking for
     
    • Thanks Thanks x 1
  7. m00j99

    m00j99 Registered Member

    Joined:
    Oct 8, 2009
    Messages:
    85
    Likes Received:
    52
    You dont have to check for referrer, actually im not sure if the referrer would be your 'website.com/redirect/' at all...

    all you have to do is pass an additional GET variable to your page...
    so for example you redirect to yourdomain.com/yourfile.php?blub=true

    and then check in php if the GET variable 'blub' is 'true' (which of course should be only the case when visitor clicked the redirect ;))
     
    • Thanks Thanks x 1
  8. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,286
    Likes Received:
    1,052
    Actually, I decided to use sessions by determining the session in the redirect page and then using the if/else script on the latter to determine whether or not the session active is the session that was set on the redirect page.

    Thanks for your help, guys! Reps and thanks all around!