简体   繁体   中英

How to know or ensure that a php page is only called by localhost?

我们应该检查$_SERVER['REMOTE_SERVER']还是什么?

This will do the trick:

if($_SERVER['REMOTE_ADDR'] === '127.0.0.1') {
    // do something
}

Be careful you don't rely on X_FORWARDED_FOR as this header can be easily (and accidentally) spoofed.

The correct way to do this would be to set an environmental variable in your server configuration and then check that. This will also allow you to toggle states between a local environment, staging and production.

Check

$_SERVER['REMOTE_ADDR']=='127.0.0.1'

This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.

refered from : How to check if the php script is running on a local server?

This code will help you.

<?php
if($_SERVER['SERVER_NAME'] == 'localhost')
{
    echo 'localhost';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM