php 从php表单获取cookie值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6515497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:36:51  来源:igfitidea点击:

getting cookie value from php form

phpformscookies

提问by Lenn

what do I set the cookie value and name to be for a value a user may enter in a form? and what do i use to display that value on my second page? (I can't not use cookies for this, so while there may be a smarter way to do this, I would just like to know how to do it with cookies!!) Thanks!

对于用户可以在表单中输入的值,我应该如何设置 cookie 值和名称?我用什么来在我的第二页上显示该值?(为此我不能不使用 cookie,所以虽然可能有更聪明的方法来做到这一点,但我只想知道如何使用 cookie 来做到这一点!!)谢谢!

<?php
setcookie($color, 'color');
setcookie($name, 'name');
?>

<?php
echo "<form action=\"form_data.php\" method=\"post\">";
echo "favorite color:<input type=\"text\" name=\"color\" size=\"20\"><br/>";
echo "name:<input type=\"text\" name=\"name\" size=\"20\"><br/>";
echo "<input type=\"submit\" value=\"Submit\" />";
echo "<br /><input type=\"hidden\" name=\"submitted\" value=\"true\" />";
?>

data on form_data:

form_data 上的数据:

  <?php
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
  ?>

回答by baraboom

First, you have your form:

首先,你有你的表格:

<?php
echo "<form action=\"form_data.php\" method=\"post\">";
echo "favorite color:<input type=\"text\" name=\"color\" size=\"20\"><br/>";
echo "name:<input type=\"text\" name=\"name\" size=\"20\"><br/>";
echo "<input type=\"submit\" value=\"Submit\" />";
echo "<br /><input type=\"hidden\" name=\"submitted\" value=\"true\" />";
?>

Then in form_data.php:

然后在 form_data.php 中:

<?php
// set the cookie with the submitted user data
setcookie('color',$_POST['color']);
setcookie('name', $_POST['name']);
echo "<b>fav color:</b>".$_COOKIE['color'];
echo "<b>name:</b>".$_COOKIE['name'];
?>

However, you'll notice that the $_COOKIE variables are not available yet... if you reload that page, they will appear.

但是,您会注意到 $_COOKIE 变量尚不可用……如果您重新加载该页面,它们将出现。

In order to accomodate this behavior of cookies, you can setup a redirect in form_data.php:

为了适应 cookie 的这种行为,您可以在 form_data.php 中设置重定向:

<?php 
 if (!empty($_POST)) {
  // set the cookie with the submitted user data
  setcookie('color',$_POST['color']);
  setcookie('name', $_POST['name']);
  // redirect the user to final landing page so cookie info is available
  header("Location:form_data.php");
 } else {
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
 }
?>

You can redirect them anywhere suitable. Hope this helps and good luck!

您可以将它们重定向到任何合适的地方。希望这会有所帮助,祝你好运!

回答by Trieu Nguyen

function setcookie of your question fail. setcookie($name, $value);

您的问题的功能 setcookie 失败。setcookie($name, $value);

Ex: setcookie('color', 'red');

例如: setcookie('color', 'red');

echo $_COOKIE['color']; //outout: red

回声 $_COOKIE['颜色']; //输出:红色

回答by Nickool

I don't know why you want to use cookies.as I know you can use sessions for passing the information of users like this: It is the full example that is working for me

我不知道您为什么要使用 cookie。因为我知道您可以使用会话来传递这样的用户信息:这是对我有用的完整示例

<?php
session_start();
if(array_key_exists('sub',$_POST))
$_SESSION['name']=$_POST['name'];
?>
<html>
<form  method="post">
<input type='text' name="name">
<input type='submit' name='sub' value='send my info'>
</html>

and in another page just use this one:

在另一个页面中只需使用这个:

<?php
session_start();
$r=$_SESSION['name'];
echo $r;
?>

remember to call session_start(); on each page when you want to use sessions

记得调用 session_start(); 当您想使用会话时在每个页面上

回答by beacon

Can anyone explain what baraboom wrote:

谁能解释一下 baraboom 写了什么:

However, you'll notice that the $_COOKIE variables are not available yet... if you reload that page, they will appear.

但是,您会注意到 $_COOKIE 变量尚不可用……如果您重新加载该页面,它们将出现。

<?php 
 if (!empty($_POST)) {
  // set the cookie with the submitted user data
  setcookie('color',$_POST['color']);
  setcookie('name', $_POST['name']);
  // redirect the user to final landing page so cookie info is available
  header("Location:form_data.php");
 } else {
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
 }
?>

Just trying to understand why it is like that since the user is redirected to the page on submit.

只是想了解为什么会这样,因为用户在提交时被重定向到页面。