为什么这个会话形式不起作用?

时间:2015-01-13 05:16:50

标签: php forms session post

我已经设置了这个登录表单,它将$ _POST数据转换为$ _SESSION数据。然后我使用此数据来确定用户是否应该登录,但它无法正常工作。我做错了什么?

***我检查了浏览器的cookie,并且存储了会话令牌(如果有帮助)

的login.php:

<?php session_start(); ?>

<form action="page.php" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
        }

        ?>

</form>

page.php文件:

<?php session_start(); ?>

<?php

        $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
        $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

        $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


        if(($username == "username") && ($password == "password") && ($submit == "Submit")) {
            echo "You are logged in";
        } else {
            echo "Denied access";
        }
?>

我在包含session_start();

的网页上收到此错误

enter image description here

根据this post,这是由Chrome引起的错误报告,但不会影响任何内容。

5 个答案:

答案 0 :(得分:2)

<form action="page.php" method="post">

    Username: <input type="text" name="username" value="" /><br />
    Password: <input type="password" name="password" value="" /><br />
    <br />
    <input type="submit" name="submit" value="Submit" />


 </form>

page.php文件

 <?php
    session_start();
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $submit = $_POST['submit'];

        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['submit'] = $submit;
    }
    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password") && ($submit == "Submit"))        {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }
?>

答案 1 :(得分:1)

当您将表单submit上的操作发送到另一个页面page.php时。那么,为什么你在同一页面上获得表单数据呢?

请尝试此操作:

HTML表格:

<form action=page.php method="post">

    Username: <input type="text" name="username" value="" /><br />
    Password: <input type="password" name="password" value="" /><br />
    <br />
    <input type="submit" name="submit" value="Submit" />


 </form>

page.php:

 <?php
    session_start();
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $submit = $_POST['submit'];

        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['submit'] = $submit;
    }
    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password") && ($submit == "Submit"))        {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }
?>

答案 2 :(得分:0)

在页面顶部添加session_start();并修复操作。

<?php session_start();?>
<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

        ?>

</form>

<?php
        session_start();
        $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
        $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

        $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


        if(($username == "username") && ($password == "password") && ($submit == "Submit")) {
            echo "You are logged in";
        } else {
            echo "Denied access";
        }
?>

答案 3 :(得分:0)

首先,您需要在表单页面中使用session_start()启动会话。此外,您还需要使用标头将会话发送到login.php。使用以下代码

<?php session_start();?>
<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

        ?>

</form>

希望这有助于你

答案 4 :(得分:0)

您的表单会向page.php发送帖子。因此,您需要将会话条件移至page.php。

另外,您可以将表格发送给自己并在之后重定向:

<?php session_start();?>

<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password")) {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }

        ?>

</form>