matteo
matteo

Reputation: 147

How to block a php script from being executed directly from address bar?

I have something like this:

$.getJSON('/scripts/commons/theScriptDoTravelBackInTime.php',{
}, function(){
  // etc.. etc...
}
});

Is there a way to stop the php script from being executed simply by a direct call in the address bar like http://www.myserver/scripts/commons/theScriptDoTravelBackInTime.php ?

Maybe outputting an echo "Hey no cheating !"

Upvotes: 1

Views: 671

Answers (4)

Fletch
Fletch

Reputation: 963

You could look at the HTTP-Referer data. If it's blank, then it came from typing or pasting the URL. If it's not blank, then that field will show you where the link came from.

http://en.wikipedia.org/wiki/HTTP_referer

Upvotes: 0

Sumair Zafar
Sumair Zafar

Reputation: 98

If you use zend framework there is a nice way of checking it

if($this->_request->isXmlHttpRequest())
{
  //The request was made with JS XmlHttpRequest
}

Upvotes: 0

wesside
wesside

Reputation: 5740

This is what the kohana framework uses:

   public static function is_ajax()
    {
      return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND 
              strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
    }

Upvotes: 0

user557846
user557846

Reputation:

basic, not 100% safe, but commonly quoted

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
 //your code
}

Upvotes: 2

Related Questions