标题('location:..')不起作用

For*_*imo 13 php apache header http-headers

(1)我正在将我的网站上传到远程Web服务器.

(2)网站的模板系统的设置方式是通过发送url编码的get请求来形成所有页面 index.php

(3)加载初始页面工作.此页面通过评估其表单的值来确定下一页的位置.

(4)重定向到下一页是通过执行以下操作来执行的: header('location: next_page')

(5)由于某种原因,不执行重定向.这是代码的样子:

$error = "";
if(isset($_POST['index_choice'])){
    $path_choice = isset($_POST['path']) ? $_POST['path'] : NULL;

    //echo $path_choice;
    //echo $page_inc;

    //nothing after this

    if($path_choice != null){

        if($form->is_connected()){

            //if($path_choice != "" || $path_choice != NULL){
                if($path_choice == "new"){

                    //header('location: /login.php');
                    //header('location: page/login');
                    header('location: /index.php?page=login');
                    exit();

                }
                else{

                    //header('location: /amend.php');
                    //header('location: page/amend');
                    header('location: /index.php?page=amend');
                    exit();
                }
            //}
            /**
            else{
                //destroy_session();
                $error = "You haven't selected a path. Please choose a path";
            }
             *
             */
        }
        else{
            //destroy_session();
            $error = "Problems with connecting to the database";
        }
    }else{
        //destroy_session();
        $error = "You have not indicated your choice";
    }

}
Run Code Online (Sandbox Code Playgroud)

解决了

这是?>在代码中的其他地方之后有空格的问题.在代码顶部放置以下命令后向我显示:

 error_reporting(E_ALL); ini_set('display_errors', 'On'); 
Run Code Online (Sandbox Code Playgroud)

我要感谢所有试图提供帮助的人.

小智 16

在发送标题("位置")之前,您似乎正在向浏览器回显文本.在执行header()之前,您无法将内容发送到浏览器,因为echo将强制发送标头.注释这些行并查看它是否有效:

// echo $path_choice;
// echo $page_inc;
Run Code Online (Sandbox Code Playgroud)

现在您的标题将被发送,您将被重定向.

  • 您的代码中有哪些更高的内容?在打开`<?php`之前是否有空格?你的错误日志中有什么? (4认同)

Erf*_*oor 13

将此代码添加一个代码:

<?php
ob_start();
?>
Run Code Online (Sandbox Code Playgroud)


sha*_*nth 10

您可以尝试重定向页面的javascript方式:

echo '<script>window.location = "'.$url.'";</script>';
Run Code Online (Sandbox Code Playgroud)