如果我有下面的功能,在您输入(必须)您的名字和姓氏后,然后选择一个单选按钮,但我想设置一个cookie,让我们说一分钟或其他什么,如果那个人再次回来,然后自动让'说会有一条欢迎回来的消息,而不是说输入你的名字并选择一个单选按钮......我该怎么做?
我将此作为我的 1.php 用于询问姓名
echo "<form action='lab17a.php' method='get'>";
echo "First Name: ";
echo "<input type='text' name='fname' /><br/>";
echo "Last Name: ";
echo "<input type='text' name='lname' /><br/>";
echo "Red<input type='radio' name='bgcolor' value='red'><br/>";
echo "Blue<input type='radio' name='bgcolor' value='blue'><br/>";
echo "Yellow<input type='radio' name='bgcolor' value='yellow'><br/>";
echo "Green<input type='radio' name='bgcolor' value='green'><br/>";
echo "<input type='submit'/>";
echo "</form>";
这是提交后的 2.php,将测试是否输入了字段...
if(empty($_GET["fname"]) || (empty($_GET["lname"])))
{
echo "Press the browser's BACK button and enter both names.<br/>You must enter a first name last name";
}
elseif(!(empty($_GET["fname"])) && (!(empty($_GET["lname"]))) && (empty($_GET["bgcolor"])))
{
echo "Hello " . $_GET["fname"] . " ". $_GET["lname"] . "!<br/>" . "Leaving the background color of the Web page default color (white)";
}
elseif(!(empty($_GET["fname"])) && (!(empty($_GET["lname"]))) && (!empty($_GET["bgcolor"])))
{
echo "<body bgcolor='" . $_GET["bgcolor"] . "'>" . "Hello " . $_GET["fname"] . " ". $_GET["lname"] . "!<br/>" . "Background color changed to the selected color";
}
提前致谢
最佳答案
使用setcookie方法。它有一个属性,您可以在其中设置过期时间。
更多信息在这里:
http://php.net/manual/en/function.setcookie.php
假设您的表单检查在 2.php 中已完成,并且没有错误(我在此处使用带有 formErrors 的字符串,如果它为空,则设置我的 cookie)。
if ($formErrors == "") {
setcookie("name", $nameValue, time() + 60); // Say you want the cookie to contain the filled in name and you want it to expire in 60 secs.
}
然后在您的 1.php 中检查 cookie 是否已设置,如果是,则执行您想要的操作(在您的情况下,显示一条消息)。
if (isset($_COOKIE['name'])) {
// show your message
}
在 if 语句中,您可以使用 $_COOKIE['name']
来获取值。
希望这有帮助
关于php - 对于这种情况我应该如何使用php设置cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084591/