PHP Session and HTTP_Referrer

Discussion in 'PHP' started by nickharper, Feb 7, 2010.

  1. #1
    Hi,

    I am trying to get it so that when a user signs up on my site it shows in a field in the db the page that refered them. So I have used this code:

    <?php
    session_start(); 
    $_SESSION['referer'] = $_SERVER['HTTP_REFERER'] ;
    ?>
    PHP:
    The thing is, I need it to catch referers on every page in case they enter the site on register.php instead of index.php but the issue I have is if somebody goes from index.php to register.php the http_referer then becomes index.php so is pointless.

    How can I get it so that when people fill in the form on register.php, it shows how they came to the site in the first place?

    Thanks
     
    nickharper, Feb 7, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    If you just want to check how they accessed register.php, (and make sure the referer is not your own site).

    Simply add the following @ register.php:
    <?php
    $domain1 = str_replace("www.", "", $_SERVER['HTTP_HOST']);
    $domain2 = str_replace("www.", "", parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
    
    if(!$domain1 == $domain2){
    $referer = $_SERVER['HTTP_REFERER'];
    } else {
    //if referer is your site or their is no refer
    $referer = "None";
    }
    ?>
    PHP:
    Then use the variable $referer wherever you want to place/include it.
     
    danx10, Feb 7, 2010 IP
  3. nickharper

    nickharper Active Member

    Messages:
    732
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Thanks for that, I will try it
    I managed to do it this way, does it look ok?

    <?php
    session_start(); 
    if(isset($_SESSION['views']))
    $_SESSION['views']=$_SESSION['views']+1;
    else {
    $_SESSION['views']=1; }
    if ($_SESSION['views']=="1") {
    $_SESSION['referer'] = $_SERVER['HTTP_REFERER'] ; }
    else
    { }
    ?>
    PHP:
     
    nickharper, Feb 7, 2010 IP
  4. 0x00

    0x00 Well-Known Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #4
    I recommend you sanitize the $_SERVER['HTTP_REFERER'], I am guessing you are going to save that somewhere, probably a MySQL DB.

    ex:
    mysql_real_escape_string($_SERVER['HTTP_REFERER']);
     
    0x00, Feb 7, 2010 IP