Cookie Keys

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

Post Reply
durbs
Forum Newbie
Posts: 3
Joined: Fri Sep 30, 2005 8:45 am

Cookie Keys

Post by durbs »

I'm an ASP developer by trade and am not familiar with the nuances of PHP and am struggling with turning a cookie value into a session var in PHP.

I've got a cookie set using keys in ASP:

Response.Cookies ("SUPPORTFORUM")("SLUserGUID") = Session("SL_UserGUID")
Response.Cookies ("SUPPORTFORUM")("SLUserNAME") = Session("SL_Username")

I need to write this cookie to a session var on a PHP page, problem is i can work out the syntax to call the cookie as it uses key pairs, in asp, it'd be simply

session("blahblah") = Request.Cookies ("SUPPORTFORUM")("SLUserNAME")

But in PHP i'm trying stuff like:

$_SESSION['SL_Username'] = $_COOKIE[$SL_UserNAME];

or

$_SESSION['SL_Username'] = $_COOKIE[$SLUserGUID][$SL_UserNAME];

but obviously it wont work as the SL_UserNAME value is stored within the cookies SUPPORTFORUM key.

Anyone know how to do this?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

print_r() $_COOKIE, it'll tell you how they have been stored for retrieval.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Should that be

Code: Select all

print_r($_COOKIE);
:p
durbs
Forum Newbie
Posts: 3
Joined: Fri Sep 30, 2005 8:45 am

Post by durbs »

That gives me:

Array ( [SUPPORTFORUM] => SLUserID=28&LastLogin=9/30/2005 5:53:01 AM&SLUserNAME=someuser&SLUserGUID=D4FEE5564ADC34870EE8406CB473D [ASPSESSIONIDQSRSACAQ] => EKBPKGGBFFFPALICFACGAAM [360Forum_data] => a:0:{} [360Forum_sid] => af9b65f46vvv0dc45e9dbe551bfcef81 [360Forum_t] => a:1:{i:2;i:1133689492;} )

So how do i load the SLUserNAME value into a session var called 'SL_Username'?

Ta
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

session_start():
$arr = explode('&', $_COOKIE['SUPPORTFORUM']);
$val = explode('=', $arr);

$_SESSION['SL_Username'] = $val[1];
that should work
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Jenk wrote:Should that be

Code: Select all

print_r($_COOKIE);
:p
nope, you didn't get it. he used it as a verb.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

n00b Saibot wrote:
Jenk wrote:Should that be

Code: Select all

print_r($_COOKIE);
:p
nope, you didn't get it. he used it as a verb.
Yes, I got that thanks.

But just incase durbs was to think that is how it is used, I decided to post what I did.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Um, n00b saibot, that's the way print_r() is working, lol.
durbs
Forum Newbie
Posts: 3
Joined: Fri Sep 30, 2005 8:45 am

Post by durbs »

lol, i did use 'print_r() $_COOKIE' at first but a quick Googling put me right afetr it errored!

I've tried:

Code: Select all

session_start(): 
$arr = explode('&', $_COOKIE['SUPPORTFORUM']); 
$val = explode('=', $arr); 

$_SESSION['SL_Username'] = $val[1];

Echo $_SESSION['SL_Username'];
But the session var doesnt appear to be there (is echo the PHP equivalent of response.write?)

It also doesnt appear in the textbox on my page, basically its for a bit of PHPBB where i need to pre-fill the username textbox with a user name that was set in this cookie. The code is:

Code: Select all

include_once("setsitelockcookie.php");
if ($_SESSION['SL_Username'] == '')
		{
		$username = ($userdata['session_logged_in']) ? $userdata['username'] : '';
		}
		else
		{
		$username = $_SESSION['SL_Username'];
		}
the code for setsitelockcookie.php is the code shown at the start of this post.

Cheers,

Durbs
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

may like to use parse_str() since the cookie has a url encoded string in it..
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

pilau wrote:Um, n00b saibot, that's the way print_r() is working, lol.
izzzaaat so ... o_O I gonna take care of this :lol:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

shiznatix wrote:

Code: Select all

session_start():
$arr = explode('&', $_COOKIE['SUPPORTFORUM']);
$val = explode('=', $arr);

$_SESSION['SL_Username'] = $val[1];
that should work
I think you made an error :)

Code: Select all

<?php
session_start();

  $arr = explode('&', $_COOKIE['SUPPORTFORUM']);
  $val = explode('=', $arr[2]);

  $_SESSION['SLUserNAME'] = $val[1]; 

?>
Note to use $arr[2] :)

But as feyd has suggested, parse_str() would be better (can you believe I forgot the name of that function? I have been search php.net for uritostr or uritoarr etc.. /me slaps forehead)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

haha yes i did make a error, my bad. im quite hung over so thats my excuse :lol:

ya that other function would work a lot better it seams. i never even heard of it, gotta remember that one.
Post Reply