Page 1 of 1

Cookie Keys

Posted: Fri Sep 30, 2005 8:53 am
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

Posted: Fri Sep 30, 2005 9:17 am
by feyd
print_r() $_COOKIE, it'll tell you how they have been stored for retrieval.

Posted: Fri Sep 30, 2005 9:30 am
by Jenk
Should that be

Code: Select all

print_r($_COOKIE);
:p

Posted: Fri Sep 30, 2005 9:35 am
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

Posted: Fri Sep 30, 2005 9:38 am
by shiznatix

Code: Select all

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

$_SESSION['SL_Username'] = $val[1];
that should work

Posted: Fri Sep 30, 2005 9:41 am
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.

Posted: Fri Sep 30, 2005 9:45 am
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.

Posted: Fri Sep 30, 2005 9:46 am
by pilau
Um, n00b saibot, that's the way print_r() is working, lol.

Posted: Fri Sep 30, 2005 9:55 am
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

Posted: Fri Sep 30, 2005 9:56 am
by feyd
may like to use parse_str() since the cookie has a url encoded string in it..

Posted: Fri Sep 30, 2005 9:59 am
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:

Posted: Fri Sep 30, 2005 10:24 am
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)

Posted: Fri Sep 30, 2005 11:08 am
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.