making action for ajax only, and blocking direct url visit
I have an action that's meant to be accessed only thr开发者_StackOverflow社区ough ajax. How can I make it give blank output when someone visits the url directly as http://site.com/controller/action? Is there a way that Zend can tell if it's an ajax call or direct url visit?
Edit: I found out about Zend's $this->getRequest()->isXmlHttpRequest()
, but I wonder if this can be trusted enough?
There's no way of reliably telling an AJAX request and any other kind of request apart, so no you can't block non-AJAX access.
If you're using jQuery, you can check it like:
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
return die('No direct access allowed.');
}
精彩评论