PHP header()没有正确重定向

问题描述 投票:0回答:1

header()不会重定向到我想要的页面。没有错误消息和标头功能在Xampp中正常工作,但在生产Web服务器中没有。

我想要完成的是,在链接表中,当我点击APR按钮时,STATUS列立即变为“已批准”。这在localhost环境中工作,但在生产服务器中不起作用。

我已经尝试过ob_start和ob_end_flush,我还检查过是否有空格或行。

我还删除了index.php之前的“./”。

// this is the reservation.php
if (isset($_GET['apr'])) {
  $rsID = $_GET['apr'];
  $query = $conn->query("UPDATE reservations SET reservStatus = True WHERE 
  reservID = $rsID");
  header("location: ./index.php");
}

// this is the index.php
<?php
   require 'header.php';

   if (isset($_SESSION['user_info'])) {
     include 'reservation.php';
   } else {
     include 'login.form.php';
   }

   include 'footer.php';

我希望它会重定向到index.php,并从那里,如果会话已经启动,它会将页面指向booking.php

php redirect header
1个回答
1
投票

即使在重定向标题之后PHP脚本也不会停止,你必须通过调用die();函数来阻止PHP脚本继续加载

// this is the reservation.php
if (isset($_GET['apr'])) {
  $rsID = $_GET['apr'];
  $query = $conn->query("UPDATE reservations SET reservStatus = True WHERE 
  reservID = $rsID");
  header("location: ./index.php")

  // Add these two lines
  die();
  exit();
}

您也可以在脚本中的每个重定向标题下面添加这些行,谢谢....

update

我想我得到了你的问题,到目前为止我得到的是你在显示网站标题部分的欢迎信息后重定向页面,

所以实际上你不能在任何类型的输出(通过PHP)发送到浏览器之后调用头函数(在你的情况下是标题部分中的欢迎消息)因为PHP在你第一次在浏览器上显示任何东西之前最初将Content-type头发送到浏览器来自PHP

现在,你有两种方法

1)包括redirection的JavaScript

用这个替换你在reservation.php中的标题行

echo ("<script> window.location = 'http://cdnj-nas.synology.me/cdnj/index.php'; </script>");

或者,如果这不起作用

echo ("<script> location.replace('http://cdnj-nas.synology.me/cdnj/index.php');  </script>");

2)您可以在header.php文件的顶部检查重定向的这种情况

© www.soinside.com 2019 - 2024. All rights reserved.