user4951
user4951

Reputation: 33090

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

Should we check $_SERVER['REMOTE_SERVER'] or what?

Upvotes: 2

Views: 1160

Answers (3)

Ankit Agrawal
Ankit Agrawal

Reputation: 6124

This code will help you.

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

Upvotes: 1

Dean
Dean

Reputation: 6354

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.

Upvotes: 4

William Buttlicker
William Buttlicker

Reputation: 6000

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?

Upvotes: 0

Related Questions