jblue
jblue

Reputation: 4450

making action for ajax only, and blocking direct url visit

I have an action that's meant to be accessed only through 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?

Upvotes: 1

Views: 513

Answers (2)

GordonM
GordonM

Reputation: 31740

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.

Upvotes: 4

fabrik
fabrik

Reputation: 14365

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.');
}

Upvotes: 1

Related Questions