PHP饼干设置



我不想这么说,但我一直在做一个应该是一个 30 分钟的作业,现在已经有 6 个小时了,几乎没有进展。我正在尝试在表单中捕获名称和电子邮件,并将它们设置为将持续 10 分钟的 cookie。当 Cookie 处于活动状态时,页面应跳过表单并仅显示输入。我已经用 cookie 和会话都尝试过这个,但无法让它工作。在这一点上,我已经编写并删除了至少一百行代码,只是无法真正看到问题所在。这是我第一次使用PHP。任何帮助将不胜感激。目前,此代码创建表单,获取信息并将其正确发布到页面。当我返回页面时,它再次显示表单。我认为这意味着 cookie 没有设置/粘贴。

<?php 
 if (!empty($_POST)) {
  setcookie('Cname',$_POST['name'], time()+600);
  setcookie('Cemail', $_POST['email'], time()+600);
//  header("Location:HW2.php");
 } 
?>
<html>
<head>
<title> Assignment 2 Alcausin </title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$visibleForm = True;
if(isset($_COOKIE['name'])){
    $visibleForm = False;
}
if(isset($_POST['submit'])){
    $visibleForm = False;
    echo "Your Name: ";
    echo $_COOKIE['Cname'];
    echo "<br>";
    echo "Your Email: ";
    echo $_COOKIE['Cemail'];
}
if($visibleForm){ // close php if form is displayed
    ?>      
<form action ="HW2.php" method="post">
    Name:<font color = red>*</font> <input type="text" name="name"><br>
    E-mail:<font color = red>*</font> <input type="text" name="email"><br>
    <input type="submit" name="submit" value="Submit">
</form>
<?php   // back to php
}
?>
</body>
</html>

我使用会话重写了您的脚本,因此您的数据实际上存储在服务器上,并且客户端只有一个会话cookie,它是对服务器端数据的引用,因此客户端无法篡改该数据。

虽然这对您的家庭作业可能并不重要,但当您处理用户帐户和权限时,这绝对很重要(想象一下"管理员"cookie,它告诉用户是否是管理员 - 任何人都可以手动设置该cookie,仅此而已,他是您网站的管理员)。

这没有经过测试,可能根本不起作用 - 如果是这种情况,请随时对我的答案投反对票。

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set("session.cookie_lifetime","600"); // sets the session cookie's lifetime to 10 minutes / 600 seconds
session_start(); // starts the session, this will create a new session cookie on the client if there's not one already
if (isset($_POST["name"]) && isset($_POST["email"])) { // if there's POST data
    $_SESSION["name"] = $_POST["name"]; // this saves your values to the session so you can retrieve them later
    $_SESSION["email"] = $_POST["email"]; // same here
};
?>
<html>
<head>
<title> Assignment 2 Alcausin </title>
</head>
<body>
<?php
$visibleForm = !isset($_SESSION["name"]); // visibleForm will be the opposite of isset, so if there's a "name" in the session then the form will be invisible
if ($visibleForm) { // if there's no session data, we display the form
    echo '<form action ="HW2.php" method="post">Name:<font color = red>*</font> <input type="text" name="name"><br>E-mail:<font color = red>*</font> <input type="text" name="email"><br><input type="submit" name="submit" value="Submit"></form>';
} else { // this means there is some data in the session and we display that instead of the form
    echo "Your Name: ";
    echo $_SESSION["name"];
    echo "<br>";
    echo "Your Email: ";
    echo $_SESSION["email"];
};
?>
</body>
</html>

首先,您必须在代码的最高级别添加 session_start(),因为它对于任何工作都是必不可少的。 session_start() 实际上生成 PHPSESSID cookie,也是会话标识符; 如果您使用 session_start(),则无需使用 setcookie() 为 PHPSESSID cookie 设置任何内容。

对于执行您要实现的目标的基本方法,我会尝试在页面加载时设置会话,如果有当前会话,那么它将像您所说的那样跳过表单。

$_SESSION['SESSID'] = $someVar;
$_SESSION['SESSNAME'] = "someOtherVar";

然后,在表单之前,检查是否

使用
if(isset($someVar) && isset($someOtherVar))

你知道这笔交易。

然后创建一个执行 session_destroy() 的按钮,以便它结束当前会话。

最新更新