Very new at PHP so please excuse my ignorance. I started a script that basically will write the referer URL on to a page. I am stuck on the script because I can't seem to write the referer URL on to the page. Not sure whether I should use printf() or echo() command in PHP. It is not writing the referer URL to the page due to errors that I made. Please help!

The second part of my question is how do I use PHP to pass the referer URL information to the next page? Example, users surf to my site then goes to the next page, but I want to be able to pass the referer URL capture in the first page to the second page.

Thanks ahead!

<?
// set your domain
$mydomain = "mypersonalpage.com";

// only log if the referer isn't this domain
if( strstr(!$_SERVER['HTTP_REFER'],$mydomain)
{
  // insert on to page
  echo ('" . $_SERVER['HTTP_REFERER'] . "','" . $_SERVER['PHP_SELF'] . "')";
}
?> 

    Missing a closing ) on this line:

    if( strstr(!$_SERVER['HTTP_REFER'],$mydomain) 

    Echo will do fine if you're just printing the values so it's viewable from the browser.

    And this:

    echo ('" . $_SERVER['HTTP_REFERER'] . "','" . $_SERVER['PHP_SELF'] . "')";

    should be:

    echo "'".$_SERVER['HTTP_REFERER']."','".$_SERVER['PHP_SELF']."'";

    That will output:

    'the_referrer','the_php_script'

    where "the_referrer" and "the_php_script" are replaced by their respective variables.

    ~Brett

      Thanks for the tip Brett! Now, how do I pass the URL to the next page?

        Then you have an error somewhere.

        What are you trying to do with this?

        if( strstr(!$_SERVER['HTTP_REFER'],$mydomain))

        ~Brett

          Thanks Brett, I got it to work. The other thing is how to pass parameters to the next.

          I just want to capture the referer URL and pass it from page to page. At the very last page or the contact page I want to insert it into a hidden field, so I will know where the users came from. Hope that make sense.

            use the URL. Send it as a URL addittive:

            somedomain.com/page1.php?r=XXXXXXXXXXXX

            Just remember to use urlencode() on the referer.

            ~Brett

              Not 100% clear, but I'll read up on the urlencode() function. This is very helpful. Thank you very much. Wonderful forum for us beginners.

                Just another thought. Once you got the referer URL, you can store the URL in $_session vars.

                  The reason I didn't suggest sessions is that they're a little tougher to code, especially for a novice. Best bet, use the URL first, then expand and try sessions later as you become more experienced.

                  But it's a good though though.

                  ~Brett

                    10 months later

                    JennAshton,

                    did you get it to work? I'm trying to get it to work too.

                      Write a Reply...