【发布时间】:2016-09-25 03:30:24
【问题描述】:
我正在尝试创建一个允许用户输入用户名和密码并将其存储为 cookie 的启动 php,因此当他们尝试再次登录时,它将存储密码,因此我正在启动会话,因为最终我想将它作为帖子传递给另一个 php(以验证这是否是正确的密码),但我在开始会话时有点困惑,因为我对会话不太熟悉。 这是我到目前为止所拥有的
<?php
session_start();
$_SESSION["name"] = "";
$_SESSION["password"] = "";
?>
<!DOCTYPE html>
<form action="start.php" method="post">
<head>
<meta charset="utf-8" />
<title>Remember the Cow</title>
<link href="https://webster.cs.washington.edu/css/cow-provided.css" type="text/css" rel="stylesheet" />
<link href="cow.css" type="text/css" rel="stylesheet" />
<link href="https://webster.cs.washington.edu/images/todolist/favicon.ico" type="image/ico" rel="shortcut icon" />
</head>
<body>
<div class="headfoot">
<h1>
<img src="https://webster.cs.washington.edu/images/todolist/logo.gif" alt="logo" />
Remember<br />the Cow
</h1>
</div>
<div id="main">
<p>
The best way to manage your tasks. <br />
Never forget the cow (or anything else) again!
</p>
<p>
Log in now to manage your to-do list. <br />
If you do not have an account, one will be created for you.
</p>
<form id="loginform" action="login.php" method="post">
<div><input name="name" type="text" size="8" autofocus="autofocus" /> <strong>User Name</strong></div>
<div><input name="password" type="password" size="8" /> <strong>Password</strong></div>
<div><input type="submit" value="Log in" /></div>
</form>
<p>
<em>(last login from this computer was ???)</em>
</p>
</div>
<div class="headfoot">
<p>
<q>Remember The Cow is nice, but it's a total copy of another site.</q> - PCWorld<br />
All pages and content © Copyright CowPie Inc.
</p>
<div id="w3c">
<a href="https://webster.cs.washington.edu/validate-html.php">
<img src="https://webster.cs.washington.edu/images/w3c-html.png" alt="Valid HTML" /></a>
<a href="https://webster.cs.washington.edu/validate-css.php">
<img src="https://webster.cs.washington.edu/images/w3c-css.png" alt="Valid CSS" /></a>
</div>
</div>
</body>
</html>
【问题讨论】:
-
cookies 和会话是两种不同的机制。当用户关闭浏览器窗口或一段时间不活动后,会话将被清除。 Cookie 会在浏览器中保留一段预定义的时间。当有人登录时,如果他们要记住他们的登录信息,他们应该选择一个复选框。然后,您可以为 cookie 设置加密值,请参阅 php 文档中的 setcookie,然后每次有人访问该站点时,您都可以检查 cookie 是否存在 $_COOKIES['cookie name'] 并在它是有效值时登录它们
标签: php session post session-cookies