简体   繁体   English

如何获取cookie值

[英]How to get cookie value

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).为什么他们创建自动随机值,例如:u8omuum6c9pkngrg4843b3q9m3)。 But i want to get my Original COOKIE value Which is "HAMZA" ?????但我想获得我的原始 COOKIE 值,即“HAMZA”????

This is the PHP syntax for cookie creation:这是创建 cookie 的 PHP 语法:

setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);

The first variable is your cookie name, which you can use to read the value like this:第一个变量是您的 cookie 名称,您可以使用它来读取值,如下所示:

$_COOKIE['YOUR COOKIE NAME'];

Note: Like other headers, cookies must be sent before any output from your script.注意:与其他标头一样,cookie 必须在脚本的任何输出之前发送。 This requires that you place calls to this function prior to any output, including <html> and any whitespace.这要求您在任何输出(包括<html>和任何空格)之前调用此函数。
Also note that dots and spaces ( . /还要注意点和空格( . / ) in cookie names are replaced with underscores ( _ ). ) 中的 cookie 名称替换为下划线 ( _ )。

Documentation: setcookie() , $_COOKIE[]文档: setcookie() , $_COOKIE[]

Function session_name will give you hash which atucally is you session identifier.函数 session_name 会给你哈希,这实际上是你的会话标识符。 It seems like you want to get USERNAME stored in session, don't you?您似乎想将 USERNAME 存储在会话中,不是吗? In that case you should use $_SESSION array.在这种情况下,您应该使用 $_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";       
     }    
 }

try this one ...试试这个...

setcookie($cookie_name, $cookie_value, 1800, "/");

change expires time with:更改过期时间为:

setcookie($cookie_name, $cookie_value, time()+ 1800, "/");

try this one ...试试这个...

<?
   $yummy = json_decode(json_encode($_COOKIE));

   if(isset($yummy->yourvar)) echo $yummy->yourvar;
?>

Why using encode and decode ?, it use to convert type Array to JSON originally type $_COOKIE is Array为什么使用编码和解码?,它用于将类型 Array 转换为 JSON 最初类型 $_COOKIE 是 Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM