Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can $_SERVER['SERVER_NAME'] be forged/faked?

Can the PHP variable $_SERVER['SERVER_NAME'] be forged or faked? I was planning on using that as a security measure for form posting. I would check to make sure that variable is my site name (www.example.com). I know HTTP_REFERRER can be faked, but I wasn't sure on this one.

Thanks!

like image 253
Matthew Ebert Avatar asked Jul 09 '12 01:07

Matthew Ebert


People also ask

Can HTTP_HOST be spoofed?

The above $_SERVER['HTTP_HOST'] can be spoofed by headers in the request thus not being trustworthy.

What is $_ server [' server_name ']?

$_SERVER['SERVER_NAME'] Returns the name of the host server (such as www.w3schools.com) $_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as Apache/2.2.24)

What is difference between HTTP_HOST and server_name?

The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config. Which one to use depends on what you need it for.

How can I know my server name in PHP?

PHP: $_SERVER['SERVER_NAME'] States name of the host server. Following php code used $_SERVER['SERVER_NAME'] variable to display name of the host server.


2 Answers

Actually $_SERVER['SERVER_NAME'] can be affected by what the client browser sends over... See http://shiflett.org/blog/2006/mar/server-name-versus-http-host for a through investigation on the issue.

like image 64
iWantSimpleLife Avatar answered Sep 22 '22 12:09

iWantSimpleLife


By a visitor it can't normally be faked out. But I suspect you would want to enforce a certain SERVER_NAME to license scripts so they can only be used by particular domains. In this case the answer is yes, this variable can definitely be faked.

The reason is simple, the server sets this value. In most cases you would have PHP running as an Apache module, but sometimes you have other Apache modules, sometime you have PHP running in CGI mode with NGINX or IIS, sometimes you even have PHP running as CLI forked as a child process by a custom-built server deployed in a cloud. Those servers would be responsible for setting that variable.

Plus, there's always the manual assignment.

 $_SERVER['SERVER_NAME'] = ... // this can go above all your scripts
like image 23
Silviu-Marian Avatar answered Sep 19 '22 12:09

Silviu-Marian