Undefined constant

The code above works fine.
The code below produces " Use of undefined constant HTTP_REFERER - assumed ‘HTTP_REFERER’ "

How can I remove undefined constant?

Try enclosing HTTP_REFERER in either single or double quotation marks.



The above produces the notice below.

As I test it,
when it has a real value, it produces the previous URL.
but, when It has NULL, it produces the notice above.

How can I get the information of previous URL without the notice whether it is a real value or NULL?

I think that if you go straight to a web page then there is no referrer.

Try the following to see what variables have been passed.

echo '<pre>'; // add line feeds to make it easier to read
  print_r( $_SERVER );
echo '</pre>';
die('halt browser rendering');

I like to insert the information about $_SERVER[‘HTTP_REFERER’] into a database table.

In my think, there is no way to insert the value of $_SERVER[‘HTTP_REFERER’] with strict_type.
Should I give up strict_type?
Or is there anyway to insert into a db table with strict_type?

Did you try inserting the script in my previous post and see what variables are being passed?

As mentioned, if you type the URL directly into the browser then you are the referrer.

Do not give up strict, learn how to copy, paste error messages and to search for solutions.

Here is a case when you would create a variable;

<?php declare(strict_types=1);
$referrer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'No Referrer';

Now, using a Prepared Statement you would use $referrer for your insert.

Thank you, benannamen. your code works fine.

[?php declare(strict_types=1);

$referer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'No Referrer';

The code works which suggested by bananamen works fine.
But if I add err_reporting like the below, it, I am afraid, produces Notice : Undefined index: HTTP_REFERER.


$referer = $_SERVER['HTTP_REFERER'-] ? $_SERVER[-'HTTP_REFERER'] : 'No Referrer';

Should I give up “error_reporting”?
Is there anyway it works with error_reporting?

That is not the code I posted.

(Q1) Do you mean the code1 below without “error_reporting(-1);” is not you posted?
(Q2) Do you mean the code2 below with “error_reporting(-1);” is not you posted?
(Q3) Do you mean both code1 and code2 below are not you posted?


[?php declare(strict_types=1);

$referer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'No Referrer';

[?php declare(strict_types=1);

$referer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : 'No Referrer';

Where did the dashes come from?

Oh, I am sorry, my editor use “[-” for “[”
It is “[” in real php file.
I should change it before I post here.

I fix it at the moment.

Thank you for motivating me.

With strick_type:

The code below produces Notice : Undefined variable: SERVER


$referer="No referer";
} else {

$SERVER[‘HTTP_REFERER’] cannot be inserted into a database table directly.
I should assign a local variable to the array element $SERVER[‘HTTP_REFERER’] inserting it to a database table.

If assign it to a lacal variable.is impossible,
Does it mean we cannot insert it to a database table?

You might need to take a little break. You are making careless mistakes.

What is the difference? :thinking:


Difference between $_SERVER[‘HTTP_REFERER’] and what?

Your previous post #15 that didn’t work.

I fix it like the following.




$referer="No referer";

} else {



echo $referer;

But the code above still says Notice : Undefined variable: SERVER when it has NULL.
How can I remove the Stupid “Notice”?.

I gave you the simplest, cleanest working code. Why are you trying to do something else?

1 Like

There is a difference between a variable being NULL and a variable not being set. This will still give you a notice because when there is no key HTTP_REFERER in $_SERVER. If it’s not there it doesn’t make sense to compare it to NULL, because we don’t know what we should compare to NULL.