Page 1 of 1
What's wrong with this $_SERVER['HTTP_REFERER'] code?
Posted: Mon Mar 02, 2015 3:22 am
by simonmlewis
Code: Select all
if (strstr($_SERVER['HTTP_REFERER'], 'http://www.site.com/cart.asp') !== false)
{
echo "<div class='downarrowdiv' id='addtobasket'>
Added to Basket
</div>";
}
I'm being told:
[text]Notice: Undefined index: HTTP_REFERER in C:\xampp\phpMyAdmin\site\index.php on line 233[/text]
I've not had this before. It's meant to find out where the page has come from, and then show the Div on screen fora short time.
Re: What's wrong with this $_SERVER['HTTP_REFERER'] code?
Posted: Mon Mar 02, 2015 6:09 am
by Celauran
Same as every other undefined index notice; you're not checking if $_SERVER['HTTP_REFERER'] is set before trying to use it. Sometimes it's not, so you get that notice.
Re: What's wrong with this $_SERVER['HTTP_REFERER'] code?
Posted: Mon Mar 02, 2015 6:14 am
by twinedev
you need to also check to see if 'HTTP_REFERER' actually exists, there are some times when it doesn't exist. (Also side note, if it does exist, this is one of those items you cannot trust as it is supplied by the visitor of the site, so treat it like you would something passed via $_POST/$_GET)
Also note, that some people block (either on purpose or some typ eof AV software) the browser from sending the referrer.
for your code:
Code: Select all
if ( isset($_SERVER['HTTP_REFERER']) && strstr($_SERVER['HTTP_REFERER'], 'http://www.site.com/cart.asp') !== false)