Store array in cookie [duplicate] - php

This question already has answers here:
Storing PHP arrays in cookies
(9 answers)
Closed 1 year ago.
I am converting the array into cookie by php serialize function
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", serialize($Promotedcart), time()+604800,'/');
And when the cookie is created then i am using the unserialize php function.
print_r(unserialize($_COOKIE['Promotedcart']));
it does not work.
When I print_R($_COOKIE) then it show me the value.

Cookies separated by semicolon. Serialized strings with arrays contain them inside. Maybe this is a problem. You can use base64 to avoid all possible escape issues.

You can use json_encode, json_decode functions to achieve this as an alternative.
$PromoteProductArray = array("PromoteuserId"=>$PromoteuserId,
"PromoteProductId"=>$PromoteProductId,
"PromoteBrandId"=>$PromoteBrandId);
$Promotedcart[] = $PromoteProductArray;
setcookie("Promotedcart", json_encode($Promotedcart), time()+604800,'/');
$result = json_decode($_COOKIE['Promotedcart'], true);
print_r($result);
Give it a try, this should work.

Related

Access to object property php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm trying to acces to range but is tnot working
$json = '{"range":[[1,2,20],[3,4.5]]}';
var_dump(json_decode($json->'range'));//doesn't work
var_dump(json_decode($json['range']));//doesn't work
Whay is the way to acces to range?
You need to json_decode() your JSON string first to turn it into an object. After that you can access the object properties with the -> operator.
So the correct way is this
var_dump(json_decode($json)->range));
But it is more readable if you split it into multiple statements:
$decoded = json_decode($json);
var_dump($decoded->range);
you must use this one.
json_decode($json)->range;

Extract the values from the array which is inside the object [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
if i solve this i will have my desired result for any expert this will take few minutes to guess and i am stuck in this from last 2 hours searching and googling but couldn't found the one i want. Okay so here is the thing ...
I am sending data through ajax to my php by doing JSON.stringify and receving that that to my php in the form of this
{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}
All i want to do is to get the values and insert them to the separate variables.
You need to use json_decode to convert the string to array. You can pass the second parameter to this method to convert this to array instead of object. Then you can retrieve the data like
$string = '{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}';
$array = json_decode($string, true);
print_r($array[0][0]);
Here's the example of it https://repl.it/HhI8/1

unserialize.com how they do it [duplicate]

This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 6 years ago.
I got the following string:
a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}
I can't use
var_dump(json_decode($json))
because it doesn't return anything. I found 2 websites that where a bit usefull:
http://jsonlint.com/ for checking if the object is valid (what it ain't) and http://www.unserialize.com/ wich can 'unserialize' the json string back to an array.
Now I wonder what unserialize does to the json string. So I can use it in my script as well.
Looks like this is in serialized form. You need to use unserialize() function here.
$input = 'a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}';
var_dump(unserialize($input));
Use unserialize function in php
<?php
echo '<pre>';
print_r(unserialize('a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}'));
?>
https://eval.in/637790

What kind of string is this? How do I unserialize this string? [duplicate]

This question already has answers here:
What is the type this string? a:1:{s:2:"en";}
(3 answers)
Closed 8 years ago.
What kind of string is this? How can I unserialize it and get the array out of it?
a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}
This is a serialized string. You can unserialize it with this function: unserialize(), like this:
$str = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
print_r(unserialize($str));
Output:
Array ( [0] => Abogado [1] => Notario )
Side Note:
A quote from the manual:
Warning:
FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.
Warning:
Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
I used this:
$argument = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
$arr = unserialize(urldecode($argument));
print_r($arr);

Convert Query String to Array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];

Categories