I have been working with the jQuery UI Tab component, and have successfully implemented loading the content for each tab via ajax. Each tab calls a common php page with a get variable, which is then parsed by the common page to identify and echo the 开发者_运维百科page content.
The problem I face is preventing a user from linking directly to this common php page, which will load the page content in a blank browser window... outside of the tab. I have tried detecting the $_SERVER["HTTP_REFERER"] to only load content when coming from a remote site, but this has several drawbacks, namely the back button and local page links. There must certainly be a cleaner way.
Hopefully I'm missing something simple, but how can I force ajax content to be viewed in a tab on a given page, rather than sidestepping the tab?
Thanks
You won't be able to stop it 100% of the time, but you could stop most people from non-ajax access with:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
// it's an ajax request
}
Keep in mind that headers can be spoofed easily so this is by no means secure but it will take care of most requests just fine.
精彩评论