Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if the user is on localhost in PHP?

In other words, how can I tell if the person using my web application is on the server it resides on? If I remember correctly, PHPMyAdmin does something like this for security reasons.

like image 362
Richie Marquez Avatar asked Jan 12 '10 23:01

Richie Marquez


People also ask

What is local host in PHP?

Localhost is often used in Web scripting languages like PHP and ASP when defining what server the code should run from or where a database is located.

What is host and localhost?

What does localhost mean? In a computer network, localhost is a hostname that refers to the computer that is executing a program — you can think of it as meaning “this computer.” The term is used when making a loopback request to one's own device.


1 Answers

You can also use $_SERVER['REMOTE_ADDR'] for which IP address of the client requesting is given by the web server.

$whitelist = array(     '127.0.0.1',     '::1' );  if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){     // not valid } 
like image 155
mauris Avatar answered Oct 23 '22 14:10

mauris