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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
It doesn't seem you should use
$_SERVER['HTTP_HOST']
, because this is the value in http header, easily faked.You may use
$_SERVER["REMOTE_ADDR"]
too, this is the more secure value, but it is also possible to fake. Thisremote_addr
is the address where Apache returns result to.If you want to have a whitelist / allowlist that supports static IPs and dynamic names.
For example:
This way you could set a list of names/IPs that will be able (for sure) to be detected. Dynamic names add more flexibility for accessing from different points.
You have two common options here, you could set a name in your local hosts file or you could just use one dynamic name provider that could be found anywhere.
This function CACHES results because gethostbyname is a very slow function.
For this pupose I've implemented this function:
For better reliability you could replace the $_SERVER['REMOTE_ADDR'] for the get_ip_address() that @Pekka mentioned in his post as "this bounty question"
$_SERVER["REMOTE_ADDR"]
should tell you the user's IP. It's spoofable, though.Check this bounty question for a very detailed discussion.
I think what you remember with PHPMyAdmin is something different: Many MySQL Servers are configured so that they can only be accessed from localhost for security reasons.
How about to compare
$_SERVER['SERVER_ADDR'] === $_SERVER['REMOTE_ADDR']
to determine if client is on the same machine as server?I found a easy answer.
Because all local drives have C: or D: or F: ... etc.
Just detect if the second character is a :
You can also use
$_SERVER['REMOTE_ADDR']
for which IP address of the client requesting is given by the web server.