1

I want to get cookie's name in PHP. Not cookie's value! My code like below:

<?php
session_start();
ob_start();

$user_id = $_GET["user_id"];
$timing_clock = "";

if(isset($_COOKIE["timing_type"])){
  // cookie's value  
  $timing_clock = setcookie("timing_type");
 // how to get cookie's name?
} else {
   echo("0");
}
?>

How can i do that? I want to set cookie's name to a variable. Because cookie's name are very important for me.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Jsawyer
  • 73
  • 1
  • 3
  • 11

6 Answers6

6

it will help you, use var_dump($_COOKIE) and get all COOKIE name.

$cookie_name = "user";
$cookie_value = "Dave";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); 
if(isset($_COOKIE)) {
    var_dump($_COOKIE);
    foreach($_COOKIE as  $key => $val)
    {
      echo "cookie name = ".$key.", and value = ".$val;
    }
} 
Dave
  • 3,073
  • 7
  • 20
  • 33
1

This will print all with respective name

<pre>
<?php print_r($_COOKIE); ?>
</pre>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
1

In this example

setcookie("timing_type");

the cookie name would be $_COOKIE['timing_type']

When you use the setcookie function you are applying the cookie name(key).

Stevie G
  • 5,638
  • 1
  • 10
  • 16
0

All cookies names are stored in $_COOKIE as "Name" => "value". Simply output the keys of the $_COOKIE array and you'll have your names :)

Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62
0

Simply read $_COOKIE["name"] to get the name set by setcookie

Dave
  • 3,073
  • 7
  • 20
  • 33
Alex Odenthal
  • 211
  • 1
  • 11
0

The first paramater of setcookie() is the name of the cookie

<?php
session_start();
ob_start();

$user_id = $_GET["user_id"];
$timing_clock = "";

if(isset($_COOKIE["timing_type"])){
  // cookie's value  
  $timing_clock = setcookie("timing_type");
  // how to get cookie's name?
  $cookie_name = $_COOKIE['timing_type'];

} else {
   echo("0");
}
?>
Luke Bradley
  • 326
  • 5
  • 16