Cookie property/values to variable/values with var var?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Cookie property/values to variable/values with var var?

Post by JAB Creations »

So I've really been working hard on revising my PHP class file and successfully managed to merge all my individual cookies in to a single cookie.

$_COOKIE['settings'];

Code: Select all

audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic
When I explode the cookie...

Code: Select all

$pieces = explode('_', $_COOKIE['settings']);
I'll end up with an array such as...

Code: Select all

audio.0
backgroundimages.
browserpatch.1
chatroom.0
connection.0
css3.0
cursors.0
dhtmleffects.
dtd.1
ieccss.1
initialfocus.content
keyboardlayout.designer
mediatype.
personality.0
powerkeys.0
sounds.0
theme.classic
What I want to in effect is have that array be converted in to a series of $variable = value;

So the PHP directly above would when working as desired would end up looking like...

Code: Select all

$audio = '0';
$backgroundimages = '';
$browserpatch = '1';
$chatroom = '0';
$connection = '0';
$css3 = '0';
$cursors = '0';
$dhtmleffects = '';
$dtd = '1';
$ieccss = '1';
$initialfocus = 'content';
$keyboardlayout = 'designer';
$mediatype = '';
$personality = '0';
$powerkeys = '0';
$sounds = '0';
$theme = 'classic';
So in effect how do I turn array $key and $values in to $variables with those associated values? Would I use variable variables? A foreach loop most likely perhaps? I've been testing things out though I'm not sure the best way to go about it.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

I think we've discussed it - the simplest way is to use (un)serialize() ... You are trying to rewrite these functions in another way ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

Thanks ... uh could you post some examples or links to pages with examples of serialize and unserialize that aren't super complicated? The site tizag.com has a lot of good simple examples. I'm not having much luck on Google right now finding any thing relevant.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

I'm talking about this discussion with you ;)
viewtopic.php?f=1&t=81833&p=456506&hili ... ie#p456506
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

Code: Select all

<?php 
$a = array(
    'a' => 'aaa',
    'b' => 'bbb',
    );
 
$s = serialize($a);
$b = unserialize($s);
 
echo "<pre>";
 
var_dump($a);
var_dump($s);
var_dump($b);
 
?>
output:

Code: Select all

array
  'a' => string 'aaa' (length=3)
  'b' => string 'bbb' (length=3)
string 'a:2:{s:1:"a";s:3:"aaa";s:1:"b";s:3:"bbb";}' (length=42)
array
  'a' => string 'aaa' (length=3)
  'b' => string 'bbb' (length=3)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

Ok now I'm even more confused...it's spitting out 'string(42) "a:2:{s:1:"a";s:3:"aaa";s:1:"b";s:3:"bbb";}"' and this is throwing me way off. Can we work with something static? If you throw me a single curve ball several times I'll eventually hit it but if you always throw two or more curve balls simultaneously I'll never hit either.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

I can't understand what bothers you (please, excuse my English)

Code: Select all

$a = array(
    'a' => 'aaa',
    'b' => 'bbb',
    );
 
$s = serialize($a);
$b = unserialize($s);
$c = ($a === $b);
 
echo "<pre>";
 
var_dump($a);
var_dump($s);
var_dump($b);
var_dump($c);
 
=>

Code: Select all

array
  'a' => string 'aaa' (length=3)
  'b' => string 'bbb' (length=3)
string 'a:2:{s:1:"a";s:3:"aaa";s:1:"b";s:3:"bbb";}' (length=42)
array
  'a' => string 'aaa' (length=3)
  'b' => string 'bbb' (length=3)
boolean true
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

Don't worry about the language, I understand what you mean. The part that I'm confused (not bothered) about is ...
a:2:{s:1:"a";s:3:"aaa";s:1:"b";s:3:"bbb";}

I don't know how or where all of it came from.

So let me try to start creating the bridge across my current gap of ignorance. :mrgreen:

Below is some PHP code that is incomplete....my variables are all initially part of the variable $cookie. So for example I want to use PHP to suck out...
audio.0
and change it to...
$audio = 0;

backgroundimages.
...and change it to...
$backgroundimages = ;

...etc.

So how do I take $pieces[0] and turn it in to $audio = 0...
How do I take $pieces[1] and turn it in to $backgroundimages = ...

Code: Select all

<?php
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
$pieces = explode('_', $cookie);
echo $pieces[0].'<br />';
echo $pieces[1].'<br />';
echo $pieces[2].'<br />';
echo $pieces[3].'<br />';
echo $pieces[4].'<br />';
echo $pieces[5].'<br />';
echo $pieces[6].'<br /><br />';
 
$s = serialize($pieces);
$b = unserialize($s);
$c = ($pieces === $b);
 
// How do I make $pieces[0] in to $audio variable and $pieces[0]'s value in to $audio's value?
 
// Below audio should spit out '0'...
echo 'The $audio value = <b>'.$audio.'</b>.';
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

What I am trying to say is:
- forget about your "serialize" implementation ;) i.e. the function which returns the "audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic"
string
- reimplement it by using only PHP function serialize();
- use unserialize() to get back the array;
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

JAB Creations wrote:The part that I'm confused (not bothered) about is ...
a:2:{s:1:"a";s:3:"aaa";s:1:"b";s:3:"bbb";}

I don't know how or where all of it came from.
PHP's Serialization Format
Type Serialized Example
NULL N; N;
Integer i:$data; i:123;
Double d:$data; d:1.23;
Float d:$data; d:1.23;
Boolean b:$bool_value; b:1;
String s:$data_length:"$data"; s:5:"Hello"
Array a:$key_count:{$key;$value} a:1:{i:1;i:2}
$value can be any data type
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

Also, I remember you worried about the cookie size - use gzdeflate/gzinflate function after/before serialize/unserialize
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

You're totally lost me, I completely do not understand what you're doing with unserialize nor do I comprehend what it can be used for in any of the examples because none of the examples have made any sense to me in how they can be applied (and ultimately have something useful echoed out). The strings make no sense to me to begin with so there isn't any connection that I can make with in my mind. Remember I don't learn unless I have a working example that if it doesn't directly make sense needs to have some clear effect, so far nill.

When I echo [#] for a variable that equals an unserialized string ($unseruialized = ....) it echos the character in the string at that position. I don't see what any of that has to do with what I'm trying to do. I never want to be rude and trying to help me so let me try to put it in a step by step procedure...

So...
1.)
$cookie = audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0;

2.)
$pieces = explode('_', $cookie);

3.)
$pieces[0] = audio.0
$pieces[1] = backgroundimages.
$pieces[2] = browserpatch.1
$pieces[3] = chatroom.0
$pieces[4] = connection.0

4.)
???????????????????????????????????

5.)
$audio = '0';
$backgroundimages = '';
$browserpatch = '1';
$chatroom = '0';
$connection = '0';

What do we have to do at step 4 to get PHP to step 5?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Cookie property/values to variable/values with var var?

Post by VladSun »

I suggested that you should use a standard way to serialize/unserialize data - by using PHP native functions. So, I can't explain you how it should work with your implementation, but rather I want you to reimplement them.
JAB Creations wrote: So...
1.)
$cookie = audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0;
Where did you generate this string? Obviously in another PHP file, which saves the settings into a cookie. So, go to this file and rewrite your save-function by using a simple call to serialize(). After you do it, you can use the unserialize() function to "explode" the saved cookie string into associative array like you want in the OP.

I feel like you are reinventing the wheel ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

What about list?

As suggested on this page...
http://forums.nexcess.net/archive/index.php?t-99.html
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cookie property/values to variable/values with var var?

Post by JAB Creations »

Oh! extract looks promising too!
Post Reply