PHP cookie设置

时间:2014-02-17 01:49:56

标签: php cookies

我不想这么说但是我一直在研究本应该进行了长达6小时的30分钟任务,现在几乎没有任何进展。我试图在表单中捕获名称和电子邮件,并将它们设置为持续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>

2 个答案:

答案 0 :(得分:1)

我使用sessions重写了您的脚本,以便您的数据实际存储在服务器上,而客户端只有一个会话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>

答案 1 :(得分:0)

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

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

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

然后在您的表单之前,检查是否使用

设置了其中任何一个
if(isset($someVar) && isset($someOtherVar))

你知道这笔交易。

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