在同一页面上的标头重定向

时间:2015-02-17 21:38:55

标签: php forms mysqli

我的部分剧本存在小问题:

<?php 

session_start();

include_once('../includes/connection.php');

if(isset($_SESSION['logged_in'])) {
?>


    <html>
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body>
       <h1>Markup 1</h1>
    </body>
    </html>

<?php
} else {

    if(isset($_POST['email'], $_POST['password'])) {

        $email = $_POST['email'];
        $password = $_POST['password'];         

        $query = $db->prepare("SELECT * FROM user WHERE user_email = ?");
        $query->bind_param('s',$email);
        $query->execute();

     $query->bind_result($user_id,$user_name,$user_email,$user_password);

        $query->fetch();

        $user = array("user_id"=>$user_id, "user_name"=>$user_name, "user_email"=>$user_email, "user_password"=>$user_password);


        if($user['user_id'] != 0) {

            $_SESSION['logged_in'] = true;
            header("Location: index.php");
            die();

        } else {


            $error = "Incorrect details!";
        }
    }
}
 ?>

<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="../css/style.css">
</head>
<body>
   <h1>Markup 2</h1>

<div class="container">
    <h3>Please login</h3>
    <?php if(isset($error)) { ?>
        <h4><?php echo $error; ?></h4>
    <?php } ?>
    <form action="index.php" method="post" autocomplete="off">

                <input type="text" name="email" placeholder="E-mail">
                <input type="password" name="password" placeholder="Password">
                <input type="submit" value="Login">

    </form>

</div>
</body>
</html>

问题是刷新后的脚本(调用header()方法)不执行die()语句,并且成功设置会话变量并使用&#34;标记1&#34;它还将呈现&#34;标记2&#34;部分但不应该。

我在这里找到了这个例子:https://www.youtube.com/watch?v=UNTvU--o2q8

2 个答案:

答案 0 :(得分:0)

你可以尝试在else块中包含第二个标记部分,这是一个相当hackish修复,但它应该完成你的目标。我建议重新构建这一部分,并将一些标记拉出来分开包含的文件。

    <?php 

session_start();

include_once('../includes/connection.php');

if(isset($_SESSION['logged_in'])) {
?>


    <html>
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body>
       <h1>Markup 1</h1>
    </body>
    </html>

<?php
} else {

    if(isset($_POST['email'], $_POST['password'])) {

        $email = $_POST['email'];
        $password = $_POST['password'];         

        $query = $db->prepare("SELECT * FROM user WHERE user_email = ?");
        $query->bind_param('s',$email);
        $query->execute();

     $query->bind_result($user_id,$user_name,$user_email,$user_password);

        $query->fetch();

        $user = array("user_id"=>$user_id, "user_name"=>$user_name, "user_email"=>$user_email, "user_password"=>$user_password);


        if($user['user_id'] != 0) {

            $_SESSION['logged_in'] = true;
            header("Location: index.php");
            die();

        } else {


            $error = "Incorrect details!";
        }
    } ?>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body>
       <h1>Markup 2</h1>

        <div class="container">
            <h3>Please login</h3>

            <?php if(isset($error)) { ?>

                <h4>

                    <?php echo $error; ?>

                </h4>

            <?php } ?>

            <form action="index.php" method="post" autocomplete="off">
                <input type="text" name="email" placeholder="E-mail">
                <input type="password" name="password" placeholder="Password">
                <input type="submit" value="Login">
            </form>

        </div>
    </body>
    </html>

<?php } ?>

答案 1 :(得分:-1)

将内容写入浏览器后,您无法呼叫header()。你可以使用输出缓冲区在PHP中解决这个问题(它已经很长时间了),但实际上你应该移动代码来处理所有标记之上的标题。

请参阅:http://php.net/manual/en/function.header.php