普通网友 2019-03-04 13:15
浏览 48
已采纳

如何在PHP中使用数据会话?

I'm trying to use data sessions for checking a form, but, when I send user and password, and programme goes to check password, this return to main page.

I write 1234 on password field, but I can't understand why this don't work correctly.

<!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>

Second Page

<!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>
  • 写回答

1条回答 默认 最新

  • dongwei8729 2019-03-04 13:22
    关注

    session_start() must be called before any output to the browser.

    Please update your code on all php pages that use the session to be included first.

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置