skip to Main Content

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


  1. Custom $_SERVER variable

    $_SERVER['AGENT'] = 'MyValue';
    

    Use the custom variable

    echo $_SERVER['AGENT'];
    
    Login or Signup to reply.
  2. 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.

    These are then used in various other includes to define whether a page
    is real or the visitor IP address is a BOT.

    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.

    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?

    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)

    Login or Signup to reply.
  3. Use ?= operator:

    $_SERVER['BOT'] ?= 0; // if not set, set it to 0
    

    You can use ?? operator too:

    $_SERVER['AGENT'] ?= $_SERVER['HTTP_USER_AGENT'] ?? ""; // ... and if none set, set it to ""
    

    The complete code:

    $_SERVER['BOT'] ?= 0;
    
    $_SERVER['REALPAGE'] ?= 0;
    
    $_SERVER['HOST'] ?= $_SERVER['REMOTE_HOST'] ?? gethostbyaddr($_SERVER['REMOTE_ADDR']);
    
    $_SERVER['REFERER'] ?= $_SERVER['REFERER'] ?? $_SERVER['HTTP_REFERER'] ?? "";
    
    $_SERVER['AGENT'] ?= $_SERVER['AGENT'] ?? $_SERVER['HTTP_USER_AGENT'] ?? "";
    

    Also you can read values with out changing $_SERVER values (if it’s acceptable in your code):

    $bot = $_SERVER['BOT'] ?? 0;
    
    $page = $_SERVER['REALPAGE'] ?? 0;
    
    $host = $_SERVER['HOST'] ?? $_SERVER['REMOTE_HOST'] ?? gethostbyaddr($_SERVER['REMOTE_ADDR']);
    
    $ref = $_SERVER['REFERER'] ?? $_SERVER['HTTP_REFERER'] ?? "";
    
    $agent = $_SERVER['AGENT'] ?? $_SERVER['HTTP_USER_AGENT'] ?? "";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search