【问题标题】:Why my page redirection does't work?为什么我的页面重定向不起作用?
【发布时间】:2014-09-24 14:35:48
【问题描述】:

我不知道我的代码有什么问题。我正在尝试进行简单的页面重定向。但它不起作用。如果我访问 login.php 页面,然后单击登录,它将留在 login_process.php 中并且什么也不显示,它应该重定向到 books.php。如果我已经登录并再次访问 login.php,它会询问用户名和密码,但它不应该发生

//登录.php

    <?php

        include_once ('autoload.php');

        open_page($configs['web_name']);
    ?>

    <form action="login_process.php" method="post">
        <table border="1">
            <tr>
                <td>Username</td>
                <td>:</td>
                <td><input type="text" name="username"></td>
            </tr>

            <tr>
                <td>Password</td>
                <td>:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="3"><input type="submit" name="action" value="login"></td>
            </tr>
        </table>
    </form>

    <?php
    close_page();
    ?>

//登录进程.php

    <?php

        include_once ('autoload.php');

        $is_logged_in = get_session('is_logged_in');
        if(!$is_logged_in){
            $username = get_form_post('username');
            $password = get_form_post('password');

            if($configs['username'] == $username && $configs['password'] == $password){
                set_session('is_logged_in',TRUE);
            }else{
                redirect('login.php');
            }
        }
            redirect($configs['main_page']);

而重定向功能是:

function redirect($_location){
        header('Location : ' .$_location);
    }

【问题讨论】:

  • print_r($configs) in login_process.php 并向我们展示结果。另外,我发现您在分离编程逻辑和页面表示方面做得很好,但更进一步:使用模板引擎!我建议你 Smarty,因为它是我使用的。

标签: php redirect


【解决方案1】:

去掉空格:

header('Location : ' .$_location);
                ^ space

所以它读作:

header('Location: ' .$_location);

Location和冒号:之间不能有空格

【讨论】:

  • @DanielBoyMarpaung 不客气。请接受已解决,干杯
  • @DanielBoyMarpaung 欢迎来到 Stack。如何接受答案meta.stackexchange.com/a/5235
猜你喜欢
  • 2014-11-27
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多