如何在使用 Jquery.load 包含 .php 文件时防止在 php 中直接访问?
How to prevent direct accessing in php while using Jquery.load to include .php files?
这是我的 index.php 文件的一部分:
<body>
<?php include_once 'includes/sidemenu.php' ?>
<div class="container">
<!--Site body!-->
<?php include_once 'pages/index/home.php' ?>
<!--End Site body-->
</div><!--container!-->
</body>
我制作了我的网站,所以一切都通过 index.php(没有 hrefs)
所以在我的侧边菜单中,每当我单击一个项目转到另一个页面时,我都会这样做 (e.x):
<li><a class="gn-icon gn-icon-cog" onclick="$('.container').load('pages/index/about.php'); $('#indextitle').html('About');">About</a></li>
我将 .php 文件加载到我的 index.php 中,它显示正常。
about.php:
<header style="font-size: 20px; padding-left: 200px">
<h1>About myself</h1>
<p>My name is Jelmer, a guy from The Netherlands and currently I am 20 years old.</p>
<p>At the moment I'm studying computer science at Avans Hogeschool University of Applied Sciences</p>
</header>
现在...问题是当我想检查对 about.php 的直接访问时(这不应该)我无法检查它,因为它不包含 php 文件,但通过 JQuery 加载它。我没有什么可以退缩的...
我尝试使用一个变量来检查它是否被定义但是在 about.php 中它找不到变量因为 .load .
我也尝试检查是否计数( get_included_filed) == 1 。但是在 about.php 上,包含文件的数量似乎总是 1(因此包含 0 个文件)。
它似乎没有计算来自 [=37= 的包含文件] 已经存在。我认为这都是因为 Jquery.load 。
我该如何解决这个问题?
检查 http://api.jquery.com/load/ 中 .load()
的 API 引用时,我们发现我们可以将参数设置为目标文件,因此我想出了一个利用此功能的解决方法。
JS:
$( ".container" ).load( "about.php", {access:true});
about.php:
<?php
if(isset($_POST['access'])&&$_POST['access']==true){
echo <<<HTML
<header style="font-size: 20px; padding-left: 200px">
<h1>About myself</h1>
<p>My name is Jelmer, a guy from The Netherlands and currently I am 20 years old.</p>
<p>At the moment I'm studying computer science at Avans Hogeschool University of Applied Sciences</p>
</header>
HTML;
}else{
echo "Access denied!";
}
?>
这是我的 index.php 文件的一部分:
<body>
<?php include_once 'includes/sidemenu.php' ?>
<div class="container">
<!--Site body!-->
<?php include_once 'pages/index/home.php' ?>
<!--End Site body-->
</div><!--container!-->
</body>
我制作了我的网站,所以一切都通过 index.php(没有 hrefs)
所以在我的侧边菜单中,每当我单击一个项目转到另一个页面时,我都会这样做 (e.x):
<li><a class="gn-icon gn-icon-cog" onclick="$('.container').load('pages/index/about.php'); $('#indextitle').html('About');">About</a></li>
我将 .php 文件加载到我的 index.php 中,它显示正常。
about.php:
<header style="font-size: 20px; padding-left: 200px">
<h1>About myself</h1>
<p>My name is Jelmer, a guy from The Netherlands and currently I am 20 years old.</p>
<p>At the moment I'm studying computer science at Avans Hogeschool University of Applied Sciences</p>
</header>
现在...问题是当我想检查对 about.php 的直接访问时(这不应该)我无法检查它,因为它不包含 php 文件,但通过 JQuery 加载它。我没有什么可以退缩的...
我尝试使用一个变量来检查它是否被定义但是在 about.php 中它找不到变量因为 .load .
我也尝试检查是否计数( get_included_filed) == 1 。但是在 about.php 上,包含文件的数量似乎总是 1(因此包含 0 个文件)。
它似乎没有计算来自 [=37= 的包含文件] 已经存在。我认为这都是因为 Jquery.load 。
我该如何解决这个问题?
检查 http://api.jquery.com/load/ 中 .load()
的 API 引用时,我们发现我们可以将参数设置为目标文件,因此我想出了一个利用此功能的解决方法。
JS:
$( ".container" ).load( "about.php", {access:true});
about.php:
<?php
if(isset($_POST['access'])&&$_POST['access']==true){
echo <<<HTML
<header style="font-size: 20px; padding-left: 200px">
<h1>About myself</h1>
<p>My name is Jelmer, a guy from The Netherlands and currently I am 20 years old.</p>
<p>At the moment I'm studying computer science at Avans Hogeschool University of Applied Sciences</p>
</header>
HTML;
}else{
echo "Access denied!";
}
?>