I am setting $_SERVER variables in an include file as follows –
$_SERVER['BOT'] = isset($_SERVER['BOT']) ? $_SERVER['BOT'] : 0; // assume this is not a bot
$_SERVER['REALPAGE'] = isset($_SERVER['REALPAGE']) ? $_SERVER['REALPAGE'] : 0; // assume this is a real page
if (!isset($_SERVER['HOST'])) {
$_SERVER['HOST'] = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : gethostbyaddr($_SERVER['REMOTE_ADDR']);
}
if (!isset($_SERVER['REFERER'])) {
$_SERVER['REFERER'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
}
if (!isset($_SERVER['AGENT'])) {
$_SERVER['AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
}
These are then used in various other includes to define whether a page is real or the visitor IP address is a BOT.
Now what I have noticed is that whilst there are no problems with $_SERVER[‘BOT’], $_SERVER[‘REALPAGE’], $_SERVER[‘HOST’] and $_SERVER[‘REFERER’], sometimes $_SERVER[‘AGENT’] changes.
Is it because AGENT is a reserved word or something similar?
BTW I have no problems setting these even though research here on Stackoverflow and elsewhere on the web says it is not allowed.
Is there a reason I have no trouble setting these?
3
Answers
Custom
$_SERVER
variableUse the custom variable
php.net documentation
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server, therefore there is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. However, most of these variables are accounted for in the » CGI/1.1 specification, and are likely to be defined.
Note: When running PHP on the command line most of these entries will not be available or have any meaning.
there is no usefull reason for put a var inside $_SERVER since it is reset by the web server every time you reload the page.
so $AGENT=’xxx’ and $_SERVER[‘AGENT’]=’xxx’ have the same lifecycle
the downside is that if your webserver use ‘AGENT’ (or any other key you try to override) as entry,
you may have some sideffect.
you cannot change the super global value, what you did is "change the local value" at "page scope"
if you open another page or reload (the value of $_SERVER’s entries is still the original one)
Use
?=
operator:You can use
??
operator too:The complete code:
Also you can read values with out changing
$_SERVER
values (if it’s acceptable in your code):