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

php - save the referring site's url

Discussion in 'PHP & Perl' started by bizbez, Feb 8, 2012.

  1. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    I?d like to save the referring page to my site ? only external sources and then pass it in a session to another page where I log this url.
    My site uses the index.php file to connect to all pages of the site (like a global include). So my problem is that I do record the referring url, but it is erased if a user passes from page to page ? my script writes the previous page, instead of just saving the entrance url and that?s it.

    Anyone can help here?
     
  2. nytrolic

    nytrolic Registered Member

    Joined:
    Oct 7, 2008
    Messages:
    86
    Likes Received:
    35
    something like

    if(strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])){
    echo "It's same domain";
    } else {
    echo "Different domain";
    }

    ?
     
  3. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    index.php:
    PHP:
    <?php
    session_start
    ();
    if (!
    strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])){
         
    $_SESSION['ref']=$_SERVER['HTTP_REFERER'];
    }
    anotherpage.php:
    PHP:
    <?php
    session_start
    ();
    if (!empty(
    $_SESSION['ref'])){
         
    log_referer($_SESSION['ref']); //your logging mechanism
    }
     
  4. misulicus

    misulicus Regular Member

    Joined:
    Jan 31, 2010
    Messages:
    251
    Likes Received:
    118
    Home Page:
    Code:
    $_SESSION['ref'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    
    if there is a refferer and not direct access $_SESSION['ref'] will hold the value if not will be empty
     
  5. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    as usual BHW members do the trick, thanks guys.

    here's the final code i've used - checks for 2 things:
    1. if the domain is the same - thanks to @nytrolic!!
    2. if there is already a session that holds the http referer - thanks to @misulicus

    @tripper_john_md thanks to you too - same solution.

    here's the final code
    Code:
    if(strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])) {
           if(!isset($_SESSION['ref']) && isset($_SERVER['HTTP_REFERER'])) $_SESSION['ref'] = $_SERVER['HTTP_REFERER'];
    
    		echo "It's same domain   ".$_SESSION['ref'];
    		} 
    else {
    	if(!isset($_SESSION['ref']) && isset($_SERVER['HTTP_REFERER'])) $_SESSION['ref'] = $_SERVER['HTTP_REFERER'];
           echo "Different domain   ".$_SESSION['ref'];
    }