Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Multidimensional Array and Cookies


PHP Multidimensional Array and Cookies

Reply
  #1 (permalink)  
Old 10-15-09, 01:48 PM
indigo001 indigo001 is offline
New Member
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Multidimensional Array and Cookies

Hi everyone,

I'm attempting to store a multidimensional array into a cookie so I can pass the values and change them later.

Code:
$var1[$row]= array(
                            "id" => $arr_clave,
                            "question" => $arr_question,
                            "answerA" => $arr_answerA,
                            "answerB" => $arr_answerB,
                            "answerC" => $arr_answerC,
                            "answerD" => $arr_answerD,
                            "correct" => $arr_correct
);
Next step is serializing the array for cookie storage (if you have another method it would be nice if you post it):
Code:
$var2 = serialize($var1);
setcookie("staff", $var2);
However it doesn't create the cookie unless I use the stripslashes function:
Code:
$var2 = serialize(stripslashes($var1));
setcookie("staff", $var2);
The cookie is now created, but I have no idea whether it really stored anything or how to use it on my next page. I was hoping to use something like this:
Code:
$var = (unserialize($_COOKIE["staff"]));
Reply With Quote
  #2 (permalink)  
Old 10-15-09, 04:15 PM
pyr0t3chnician pyr0t3chnician is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 30
Thanks: 0
Thanked 1 Time in 1 Post
Why not use session variables? Not only is it easier, but cookies can be edited and possibly changed, making it possible for people to cheat (I am assuming you are making a quiz script).
Code:
<?php
session_start();

// makes an array
$colors=array('red', 'yellow', 'blue');
// adds it to our session
$_SESSION['color']=$colors;
$_SESSION['size']='small';
$_SESSION['shape']='round';
print_r($_SESSION);
?>
Reply With Quote
  #3 (permalink)  
Old 10-15-09, 04:32 PM
indigo001 indigo001 is offline
New Member
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Yes it's a quiz script.

Well, it is supposed to recover itself in case the browser crashes or anything happens to the server so I thought on using cookies, so when the user gets back he can continue just where he left. I'm not really sure if sessions are persistent.
Reply With Quote
Reply

Bookmarks

Tags
cookies, multidimensional array, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Loop through cookies - print value only Schecter420 JavaScript 2 05-02-09 04:13 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
unknown problem - probably $session issue clantron PHP 6 03-19-06 01:36 PM
Setting a cooke (time it expires) mcrob PHP 4 04-27-05 12:13 PM
PHP and Cookies Christoff PHP 4 07-03-04 04:05 PM


All times are GMT -5. The time now is 08:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.