标题('位置:..')不工作

发布于 2024-09-15 17:04:57 字数 1772 浏览 3 评论 0原文

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

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

(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";
    }

}

已解决

这是代码中其他位置的 ?> 后面有一个空格的问题。在将以下命令放在代码顶部后,我发现了这一点:

 error_reporting(E_ALL); ini_set('display_errors', 'On'); 

我想对所有试图提供帮助的人表示感谢。

(1)I'm in the process of uploading my website to a remote web server.

(2)The site's template system is set up in a way that all of the pages are formed by sending url-encoded get requests to index.php

(3)Loading up the initial page works. This page determines the location of the next page by evaluating the value of its form.

(4)The redirection to the next page is performed by doing a: header('location: next_page')

(5)For some reason, the redirection is not performed. Here's what the code looks like:

$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";
    }

}

SOLVED

It was a matter of having a blank space after a ?> somewhere else in the code. This was revealed to me after placing the following commands at the top of the code:

 error_reporting(E_ALL); ini_set('display_errors', 'On'); 

I'd like to say thanks to all of the people that have tried to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

烂人 2024-09-22 17:04:57

看起来您在发送标头(“位置”)之前正在将文本回显到浏览器。
在执行 header() 之前,您无法将内容发送到浏览器,因为您的 echo 将强制发送标头。注释掉这些行并查看它是否有效:

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

现在您的标头将被发送,并且您将被重定向。

Looks like you're echo-ing text to the browser before sending the header('location').
You can not send content to your browser before executing a header(), as your echo will force a header to be sent. Comment these lines out and see if it works:

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

Now your header will be sent and you will be redirected.

墨小墨 2024-09-22 17:04:57

添加此代码到代码的第一个

<?php
ob_start();
?>

add this code to first of code:

<?php
ob_start();
?>
心如狂蝶 2024-09-22 17:04:57

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

echo '<script>window.location = "'.$url.'";</script>';

You can try the javascript way of redirecting the page:

echo '<script>window.location = "'.$url.'";</script>';
牵你的手,一向走下去 2024-09-22 17:04:57

试试这个吧。它将

echo "<script type='text/javascript'>  window.location='index.php'; </script>";

代替 header("location:index.php");

Just try this. it will work

echo "<script type='text/javascript'>  window.location='index.php'; </script>";

instead of header("location: index.php");

慵挽 2024-09-22 17:04:57

像下面这样的简单 PHP 脚本可以工作吗?

<?php header('Location: /index.php?page=login'); ?>

您还必须确保实际采用 $path_choice != null$form->is_connected() === true 的代码路径,因为您的错误状态根本不设置 HTTP 标头。

Does simple PHP script like the following work?

<?php header('Location: /index.php?page=login'); ?>

You also have to make sure that the code path with $path_choice != null and $form->is_connected() === true is actually taken because your error states don't set HTTP headers at all.

余罪 2024-09-22 17:04:57

我看到“destroy_session()”,您是否在某处使用 session_start() ?
确保尚未发送任何内容(包括(会话)cookie)。

调试提示:设置error_reporting(E_ALL)(包括E_NOTICE)。
这将为您提供发送标头的行。

其他麻烦来源:
- BOM
- 包含文件中之前的额外行

I see a 'destroy_session()', are you using session_start() somewhere?
Make sure that NO content (including (session)cookies) is already sent.

Tips for debugging: set error_reporting(E_ALL) (including E_NOTICE).
That'll give you the line on which the headers are sent.

Other sources of trouble:
- BOM
- extra lines before in an included file

蓝眼睛不忧郁 2024-09-22 17:04:57

我相信您需要检查您的 if 堆栈并确保您进入每个循环。在每个 if 语句后放置一个 echo 以确保 header() 甚至被评估,您的循环可能会比您预期的更早被切断。

我的怀疑与 null 和 $path_choice != null 的使用有关。您是否尝试过 is_null() 或 isset(),因为它们是专门为检查变量是否为 null 而设计的。另外, !== 操作数在这种情况下也可以工作,但我不确定 PHP 是否有这样的功能。

I believe you need to check your if-stack and make sure you are getting into each of your loops. Place an echo after each if statement to make sure the header() is even being evaluated, your loop is prolly being cut earlier than you expect.

My suspect has to do with the usage of null and $path_choice != null. Have you tried is_null() or isset() since they were designed specifically to check if a variable is null. Also, the !== operand may work in this case too, but i'm not sure offhand if PHP has that.

柠檬色的秋千 2024-09-22 17:04:57

确保 php 文件的 之前没有空格。如果您在 之前将文件编码类型更改为不带 BOM 和退格键的 UTS_8,并确保在 中的任何内容之后都没有 session start(),这可能会有所帮助。文件。应该就在之后

Make sure before <?php of the php file you dont have a space. It might help if you changed the file encoding type to UTS_8 without BOM and backspace just before <?php and make sure you dont have session start() after anything in the file. It should be just after

遗忘曾经 2024-09-22 17:04:57

只需确保您的 php 代码之前没有空格,因为这很重要。

例如

<?php
some code this is the correct way
?>

--------------

<?php
some code this is the incorrect way the space above matters
?>

Just make sure before your php code you do not have a space because this does matter.

For example

<?php
some code this is the correct way
?>

--------------

<?php
some code this is the incorrect way the space above matters
?>
天煞孤星 2024-09-22 17:04:57

好吧,如果你尝试了一切,这个例子可以帮助你(HTML/PHP):

<?php
    header("HTTP/1.0 404 Not Found");
    echo '<html>
            <head>
                <meta http-equiv="Refresh" content="0;url=http://www.url.com/index.php?code=404" />
            </head><body></body>
          </html>';
?>

Well, if you tried everything, this example can help (HTML/PHP):

<?php
    header("HTTP/1.0 404 Not Found");
    echo '<html>
            <head>
                <meta http-equiv="Refresh" content="0;url=http://www.url.com/index.php?code=404" />
            </head><body></body>
          </html>';
?>
倾其所爱 2024-09-22 17:04:57

确保代码开头不应该有空格

例如:

------whitespace----
<?php
.
.
.
. ?>

从第一行删除空格,这样 必须位于第一行。

Make sure there should be no whitespace at the beginning of your code

Eg:

------whitespace----
<?php
.
.
.
. ?>

Remove space from first line such that <?php must be in first line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文