【发布时间】:2021-06-04 17:53:59
【问题描述】:
我需要使用 PHP 常量变量保护文件,并且只能通过 index.php 访问文件。我需要通过 XMLHttpRequest 请求获取此页面,但是当我使用 Javascript 的 XMLHttpRequest 在 index.php 的页面容器 DIV 中发送 page.php 的内容时,它不起作用并且总是重定向到主页。
我在同一个文件夹中有 4 个文件
1. index.php
2. page.php
3. get-page.js
4. protectedf.php
index.php 代码
<?php define("protectedf", TRUE); ?>
<div class="page-container"></div>
page.php 代码
<?php require_once("protectedf.php"); ob_start(); ?>
// some html inside page.php
get-page.js 代码
const pageContainer = document.querySelector('.page-container');
function mySettings(){
const x = new XMLHttpRequest();
const url = 'page.php';
x.open('GET',url);
x.send();
x.addEventListener('readystatechange',function(){
if (this.readyState == 4 && this.status == 200) {
const xResult = this.responseText;
pageContainer.innerHTML = xResult;
this.abort();
}
});
}
protectedf.php 代码
<?php
if (!defined("protectedf")) {
header("Location:http://localhost/homepage/");
exit();
}
?>
【问题讨论】:
标签: javascript php html ajax xmlhttprequest