Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php/jquery-仅在外部加载时显示页面内容_Php_Jquery_Ajax - Fatal编程技术网

php/jquery-仅在外部加载时显示页面内容

php/jquery-仅在外部加载时显示页面内容,php,jquery,ajax,Php,Jquery,Ajax,我有一个索引页,在这里我使用jquery的.load()函数在div中加载外部页面内容 我的“问题”是,如果用户访问文件本身,他也会看到页面内容,但我不希望这样。我只希望在整个.load()函数中在索引页内加载页面时加载该页面 如何应用此规则?第一个建议是通过将其作为index.PHP的一部分包含在PHP中来加载它 第二种方法是使用$\u SESSION。从index.php设置会话密钥,然后在正在加载的文件中进行检查: index.php 你的HTML在这里 $(文档).ready(函数(

我有一个索引页,在这里我使用jquery的
.load()
函数在
div
中加载外部页面内容

我的“问题”是,如果用户访问文件本身,他也会看到页面内容,但我不希望这样。我只希望在整个
.load()
函数中在索引页内加载页面时加载该页面


如何应用此规则?

第一个建议是通过将其作为
index.PHP
的一部分包含在PHP中来加载它

第二种方法是使用
$\u SESSION
。从index.php设置会话密钥,然后在正在加载的文件中进行检查:

index.php

你的HTML在这里
$(文档).ready(函数(){
$('#foo').load('bar.php');
});
因为
.load()
方法使用的是ajax,所以您应该能够使用
$\u服务器['HTTP\u X\u REQUESTED\u WITH']
。我可能会创建一个可重用的函数来检查

/functions/isAjaxRequest.php

function isAjaxRequest($type = 'HTTP_X_REQUESTED_WITH')
{
    return (!empty($_SERVER[$type]) && strtolower($_SERVER[$type]) == 'xmlhttprequest');
}
<?php
# Add the function
require_once(__DIR__.'/functions/isAjaxRequest.php');
# Stop if not ajax
$isAjax = isAjaxRequest();
if(!$isAjax)
    die('Invalid Request.');
?>
<p>This is a valid request!</p>
/ajax\u load\u page.php

function isAjaxRequest($type = 'HTTP_X_REQUESTED_WITH')
{
    return (!empty($_SERVER[$type]) && strtolower($_SERVER[$type]) == 'xmlhttprequest');
}
<?php
# Add the function
require_once(__DIR__.'/functions/isAjaxRequest.php');
# Stop if not ajax
$isAjax = isAjaxRequest();
if(!$isAjax)
    die('Invalid Request.');
?>
<p>This is a valid request!</p>

这是一个有效的请求


尽管客户端可以伪造标题,但普通用户不会试图绕过它。

工作得很好!非常感谢@rasclatt非常感谢您的帮助@蒂姆·莫顿