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