【问题标题】:PHP define not working after XMLHttpRequest()在 XMLHttpRequest() 之后 PHP 定义不起作用
【发布时间】: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


    【解决方案1】:

    只需在包含它的页面上添加define("protectedf", TRUE);:page.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2018-04-29
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多