Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cookie into a string

Tags:

php

cookies

I read that in PHP, you can view all the cookies by using print_r, according to http://www.w3schools.com/php/php_cookies.asp .

<?php 
  print_r($_COOKIE); 
?>

But I want to do more with the content of the cookie.

Is there a way to concatenate the cookie names and values into, something like a string variable, without knowing the cookie names, instead of relying on print_r?

I couldn't find answers online. Thank you in advance.

like image 578
Ji Mun Avatar asked Aug 31 '26 17:08

Ji Mun


2 Answers

Either of these may work depending on the complexity of your cookie array

implode(',',$_COOKIE)

json_encode($_COOKIE)

serialize($_COOKIE)

I'd advise against this, and just rely on traversing the array

simple example: $_COOKIE['name']

like image 141
Scuzzy Avatar answered Sep 02 '26 07:09

Scuzzy


You can get access to all coookie names, without knowing them in advance, with code like this:

foreach ($_COOKIE as $name => $value) {
   print "Variable " . $name . " has value " . $value . "<br />\n";
}
like image 44
amindfv Avatar answered Sep 02 '26 06:09

amindfv