Jump to content

PHP Notice: Undefined index: HTTP REFERER


phpnew

Recommended Posts

Hi,

I have a script that grabs the page where a certain action happens. If the http_referer is empty, I get the notice in my log file: PHP Notice: Undefined index: HTTP_REFERER. The code is below:
 

$urlpx=parse_url(strtok($_SERVER["HTTP_REFERER"],'?'), PHP_URL_PATH);

if(empty($urlpx)) {
$urlp="NOREF";
}
else {
$urlp=$urlpx;
}


The rest after the first line is what I added in my attempt to make referer stop being undefined, but I still get the notices. I realized that the IF statement works fine, but notice is still there as no referer still gets reported from the first line of the code.

Is it possible to prevent logging it from within the script? Something that would be added to the first line of the code?

Please note that I do want all logging to be ON as it is now.

Thank you

Link to comment
Share on other sites

Simple!

 

Check and make sure that the index isn't undefined first!  Make sure that the variable is set.

 

If a variable is not set, then it is not defined; it is undefined.

 

PHP has a built-in function for this called: isset()

Formed from the words "is" and "set".  It's rather intuitive!

$urlpx = "";
if( isset($_SERVER['HTTP_REFERRER']) ) {
   $urlpx=parse_url(strtok($_SERVER["HTTP_REFERER"],'?'), PHP_URL_PATH);
}
Link to comment
Share on other sites

Thanks very much.

Unfortunately, while trying it, I got way too many empty referrers in transaction reports. For reference, for the whole month of January, the percentage of empties was 0.8%. For the part of yesterday and today, more than 50%. This watches outgoing clicks on my own website and therefore there should be a referring page in the very most cases.

Link to comment
Share on other sites

Thank you. The script is included into a redirect HTML file (for the purpose of click conversion tracking), which calls a PHP redirect which finally takes a visitor out to a website of our affiliate partner. The steps are:

 

- a click onto a link to HTML which includes the upper code in getpageurl.php.

- the HTML itself has this code (m is a varibale that determines which link is to be used from PHP redirect, v5 is a variable to get populated by the internal referring page):

<?php
include "getpageurl.php";
$m = isset($_GET['m']) ? $_GET['m'] : null;
$targetUrl = htmlentities('redirect.php?m='.urlencode($m) . '&v5='.urlencode($urlp));
?>

and

<meta http-equiv="refresh" content="0;url=<?=$targetUrl?>">

The original part of getpageurl script was this (there's more in that script, to manipulate referring page and to write it into a flat file):

$urlp=parse_url(strtok($_SERVER["HTTP_REFERER"],'?'), PHP_URL_PATH);

The sales reporting for the last month showed less than 1% of empty referrers. More clicks happen (many do not convert to sales), and all of the empty ones get recorded in PHP error log file I maintain. This was an attempt to stop logging this particular event.

 

Everything is HTTPS on my site. the affiliate redirect links are HTTP, but all is working in regards of that. I see HTTPS can be used now as well, so thanks for mentioning it, I'll update the links. :)

 

Thank you

Link to comment
Share on other sites

That's a Refresh redirect. Can't do that.

 

There's basically no reason ever to do an immediate redirect. You're sending the browser an HTML page that then redirects them to another page that then redirects them to another page. Two pages that do nothing but redirect.

Get rid of the first one and link to redirect.php directly. Or something like that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.