Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is $_SERVER['SERVER_ADDR'] safe to rely on?

Tags:

php

My site relies heavily on $_SERVER['SERVER_ADDR']. Can I trust the data retrieved? There is a possibility of finding an empty string?

like image 965
Federkun Avatar asked Apr 18 '11 15:04

Federkun


People also ask

Is $_ SERVER safe?

This is called a "tainted" variable, and is unsafe. When using $_SERVER , many of the variables can be controlled. PHP_SELF , HTTP_USER_AGENT , HTTP_X_FORWARDED_FOR , HTTP_ACCEPT_LANGUAGE and many others are a part of the HTTP request header sent by the client.

Can $_ SERVER Remote_addr be spoofed?

Any $_SERVER variable can be spoofed - e.g. curl_setopt( $ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip")); So it depends entirely on the context: if the attacker is expecting a response, it will go back to $ip. If they don't care about the response, they can certainly spoof the header.

What is the $_ SERVER variable?

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

What is $_ SERVER Document_root in PHP?

What is $_ server DOCUMENT_ROOT in PHP? $_SERVER['DOCUMENT_ROOT'] returns. The document root directory under which the current script is executing, as defined in the server's configuration file.15-Nov-2012.


2 Answers

From the php reference I quoted some

It seems that it depends on the server hosting PHP (especially apache , IIS ...)

http://php.net/manual/en/reserved.variables.server.php

On Windows IIS 7 you must use $_SERVER['LOCAL_ADDR'] rather than $_SERVER['SERVER_ADDR'] to get the server's IP address.

And another.

Windows running IIS v6 does not include $_SERVER['SERVER_ADDR']

If you need to get the IP addresse, use this instead:

<?php $ipAddress =
 gethostbyname($_SERVER['SERVER_NAME']);
?>
like image 183
Marc Bouvier Avatar answered Sep 28 '22 09:09

Marc Bouvier


From Marc, this might work in a cross-platform situation:

array_key_exists('SERVER_ADDR',$_SERVER) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
like image 20
rasx Avatar answered Sep 28 '22 09:09

rasx