php 如何获取cookie值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30570523/
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
How to get cookie value
提问by Hamza Javed
Creating cookie
创建 cookie
session_start();
$params = session_get_cookie_params();
setcookie(session_name('USERNAME'),'HAMZA',1,
isset($params['path']),
isset($params['domain']),
isset($params['secure']),
isset($params['httponly']));
session_regenerate_id(true);
echo "COOKIE IS CREATED SUCCESSFULLY !";
Now fetching cookie value
现在获取 cookie 值
session_start();
$NAME=$_COOKIE['USERNAME'];
echo $_COOKIE["USERNAME"];
if(isset($NAME))
{
if($NAME=='USERNAME')
{
echo "success";
}
else
{
echo "error";
}
}
Please Help Me !
请帮我 !
Result
结果
Why they create Auto random Value Like: u8omuum6c9pkngrg4843b3q9m3). But i want to get my Original COOKIE value Which is "HAMZA" ?????
为什么他们创建自动随机值,例如:u8omuum6c9pkngrg4843b3q9m3)。但我想获得我的原始 COOKIE 值,即“HAMZA”????
回答by
This the the format of cookie creation
这是 cookie 创建的格式
Syntax
句法
setcookie(name, value, expire, path, domain, secure, httponly);
so first variable is you cookie name
所以第一个变量是你的 cookie 名称
you can read this by using
你可以通过使用阅读这个
$_COOKIE['YOUR COOKIE NAME'];
回答by Djengobarm
Function session_name will give you hash which atucally is you session identifier. It seems like you want to get USERNAME stored in session, don't you? In that case you should use $_SESSION array.
函数 session_name 会给你哈希,这实际上是你的会话标识符。您似乎想将 USERNAME 存储在会话中,不是吗?在这种情况下,您应该使用 $_SESSION 数组。
Code example:
代码示例:
setcookie($_SESSION['USERNAME'],'HAMZA',1,
isset($params['path']),
isset($params['domain']),
isset($params['secure']),
isset($params['httponly']));
And you can get it like this:
你可以这样得到它:
$myCookie = $_COOKIE[$_SESSION['USERNAME']];
But from your second code it's not quite clear what you want to get. If you want to ask for $_COOKIE['USERNAME'] and get 'HAMZA' then you should set it like this:
但是从你的第二个代码来看,你想得到什么还不是很清楚。如果你想询问 $_COOKIE['USERNAME'] 并得到 'HAMZA' 那么你应该这样设置:
setcookie('USERNAME','HAMZA',1,
isset($params['path']),
isset($params['domain']),
isset($params['secure']),
isset($params['httponly']));
And when you retrieve it $NAME=='USERNAME' makes no sense, because it will be like $NAME=='HAMZA':
当你检索它时 $NAME=='USERNAME' 没有意义,因为它会像 $NAME=='HAMZA':
$NAME=$_COOKIE['USERNAME'];
echo $_COOKIE['USERNAME'];
if(isset($NAME))
{
if($NAME=='HAMZA')
{
echo "success";
}
else
{
echo "error";
}
}