I'm having a problem using $SERVER['HTTP_REFERER']; I have a website and I want to know where new members are coming from before they click on my home page. The home page of my site does have a .php extension. I am using $page = $SERVER['HTTP_REFERER']; but for some reason it will not register the information I need. I have tried other variables such as $SERVER['REMOTE_ADDR'] and $SERVER['HTTP_USER_AGENT'] and these work perfectly. Any ideas on why $_SERVER['HTTP_REFERER'] won't work. Yes, I have tried going to the home page from a link outside of my site. I've tried this function using javascript and it works, only problem is I don't know how to change a javascript variable into a php variable. Any ideas on that one too?

Thanks in advance for any help.

    Pretty much any of the $SERVER['HTTP<something>'] values can not be depended upon to be set and/or to be correct. They are dependent upon what information the user's browser sends to your web server in the HTTP headers. Depending on the user's browser, the security settings being used on that browser, possibly depending on whether their request is being sent through a proxy server, or if a malicious user is intentionally sending incorrect headers, you simply cannot count on any of those variables.

      When I use javascript in place of $_SERVER['HTTP_REFERER'] it works everytime. Is there a way to convert a javascript variable to a php variable so that I can then use the information?

        Remember that JavaScript is running on the user's machine once the page is received, while PHP is running on the server before the page contents get sent to the user.

        If you can control the pages which are linking to your page, then you can add a query string variable to the link:

        <a href="http://www.yoursite.com/page.php[color=red]?referer=www.someothersite.com[/color]">click me</a>
        

        You can then look for the referer value in the $GET array:

        $page = (!empty($_GET['referer'])) ? $_GET['referer'] : 'unknown';
        

        (Most advertisement referal programs work this way, giving each participating site specific values to be included in links to the seller's site.)

          I appreciate the help. Is it at all possible to convert the javascript variable to php on the same page without having to go another page and use $_GET ?

            LordRogaine wrote:

            I appreciate the help. Is it at all possible to convert the javascript variable to php on the same page without having to go another page and use $_GET ?

            I may not be entirely understanding what you mean, but normally: no. PHP runs on the server, then sends the page to the client (browser) which is when the JavaScript is run. So if you're depending on JavaScript to get some information for you, then for PHP to do anything with it, you have to submit something back to the server. This could be via a form submission, a link as discussed above, or using the AJAX techniques to send requests to the server without re-writing the whole page. (I'm a complete n00b when it comes to AJAX -- since I generally avoid using JavaScript as you can't depend on it being available on all clients -- so I'll leave that to others to discuss.)

              Write a Reply...