【问题标题】:How can I use data sessions in PHP?如何在 PHP 中使用数据会话?
【发布时间】:2019-03-04 13:15:48
【问题描述】:

我正在尝试使用数据会话来检查表单,但是,当我发送用户和密码,并且程序去检查密码时,这会返回到主页。

我在密码字段上写了 1234,但我不明白为什么这不能正常工作。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Check</title>
</head>
<body>
    <?php
        $usuario = trim(htmlspecialchars($_REQUEST['username'], ENT_QUOTES, "UTF-8"));
        $clave = trim(htmlspecialchars($_REQUEST['password'], ENT_QUOTES, "UTF-8"));
        setcookie("usuario", $usuario, time()+60*60*24*365);
        session_start();
        $_SESSION['nom_user'] = $usuario;
        $_SESSION['pass_user'] = $clave;
        header('Location: nacimiento.php');
    ?>
    <a href="index.php">Volver</a>
</body>
</html>

第二页

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Fecha de Nacimiento</title>
</head>
<body>
    <?php
        session_start();
        if (($_SESSION['pass_user'])=='1234'){
                echo '<form action="check.php" method="POST">';
                echo '<label for="fecha_nac">Fecha de Nacimiento</label><input type="date" name="fnacim" id="fnacim" />';
                echo '<input type="submit" name="Enviar" />';
                echo '</form>';
        } else {`enter code here`
            header('Location: index.php');
        }
    ?>
    <a href="index.php">Volver></a>
</body>

【问题讨论】:

  • 写这个代码页乞求并检查输出 ”; print_r($_SESSION);死();
  • 好的...表单未正确发送密码或无法使用密码建立会话。
  • nacimiento.phpcheck.php(或 index.php)这些代码中的哪一个?你有任何错误显示吗?因为session_start()应该在任何其他PHP代码之前被首先调用,并且你不应该在HTML内容输出之后使用setcookieheader
  • 另外,我希望`enter code here` 不在您的真实代码中,它应该会产生语法错误..
  • @ParvejAlam 我发现了主要错误。在 index.php 中,密码的变量名有一个额外的字母。感谢大家,不便之处敬请见谅

标签: php session php-7.0


【解决方案1】:

session_start() 必须在任何输出到浏览器之前调用。

请首先更新所有使用会话的 php 页面上的代码。

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Fecha de Nacimiento</title>
</head>
<body>
    <?php

        if (($_SESSION['pass_user'])=='1234'){
                echo '<form action="check.php" method="POST">';
                echo '<label for="fecha_nac">Fecha de Nacimiento</label><input type="date" name="fnacim" id="fnacim" />';
                echo '<input type="submit" name="Enviar" />';
                echo '</form>';
        } else {`enter code here`
            header('Location: index.php');
        }
    ?>
    <a href="index.php">Volver></a>
</body>

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2016-05-26
    相关资源
    最近更新 更多