0

Lets say I want to return an object with information about the client who requests a page. I take PHP as an example. Something like this:

public function getClientInformation(){
    return [
        "ip" => $_SERVER["REMOTE_ADDR"],
        "request_uri" => $_SERVER["REQUEST_URI"],
        "refferer" => $_SERVER["HTTP_REFERER"]
    ];
}

The problem is: I don't know if the referrer is set. How do I check properly if it is set and return false if it doesn't? This is something I came up with but I don't like it:

public function getClientInformation(){
    $referrer = false;
    if(array_key_exists("HTTP_REFERER", $_SERVER)){
        $referrer = $_SERVER["HTTP_REFERER"];
    }

    return [
        "ip" => $_SERVER["REMOTE_ADDR"],
        "request_uri" => $_SERVER["REQUEST_URI"],
        "refferer" => $referrer
    ];
}

I would appreciate your help

  • 1
    I'd suggest `array_key_exists()` as alternative to `isset()`. They are similar, but there are cases where they differ. Also, why not elide the `refferer` key from the result array? After all, the value isn't `false`, there is no value! – Ulrich Eckhardt May 11 '18 at 20:29
  • 1
    you can look into https://stackoverflow.com/questions/18603250/php-shorthand-for-isset – Khem May 11 '18 at 20:30
  • @UlrichEckhardt Good point but I get an error when I do that. – natwaalf twaalf May 11 '18 at 20:32
  • 1
    In your situation I usually just use : "refferer" => (isset($_SERVER["HTTP_REFERER"])) ? $_SERVER["HTTP_REFERER"] : false, I find it easier to read. Altough I don't think what you've got is wrong. – Lou May 11 '18 at 20:33

2 Answers2

1
"refferer" => $_SERVER["HTTP_REFERER"] ?? false; // PHP 7 
"refferer" => isset($_SERVER["HTTP_REFERER"]) ?: $_SERVER["HTTP_REFERER"] : false; // < PHP 7
Khem
  • 184
  • 7
0

In php you may use the Error Control Operators, something like this:

public function getClientInformation(){
    return [
        "ip" => $_SERVER["REMOTE_ADDR"],
        "request_uri" => $_SERVER["REQUEST_URI"],
        "refferer" => @$_SERVER["HTTP_REFERER"]
    ];
}

Which gonna return false if there is a warning (in case calling undefined index) or an error, but be sure where to use it.

Or you could use the inline ? operator:

public function getClientInformation(){
    return [
        "ip" => $_SERVER["REMOTE_ADDR"],
        "request_uri" => $_SERVER["REQUEST_URI"],
        "refferer" => isset($_SERVER["HTTP_REFERER"])? $_SERVER["HTTP_REFERER"] : false
    ];
}

In php 7.0 there is the null coalesce operator ??:

public function getClientInformation(){
    return [
        "ip" => $_SERVER["REMOTE_ADDR"],
        "request_uri" => $_SERVER["REQUEST_URI"],
        "refferer" => $_SERVER["HTTP_REFERER"] ?? false
    ];
}

Which is a shorthand for the above code.

MAZux
  • 911
  • 1
  • 15
  • 28
  • The general preference is to NOT EVER use a Error Control Operator. Suppressing errors is not the correct way of fixing the error. Code with suppressed errors is still code with errors, which can and will give unexpected results. And according to the provided link the null coalesce operator is `??` and not `?:` – Jelmergu May 11 '18 at 21:09