[Resolved] Problems with serialize and unserialize

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Watch

Albino

Munky DesignsEstablished Member
Impact
17
problems with serialize and unserialize

hey,

ok, I am saving 2 values in my cookie, like so:

Code:
$cookie = @serialize(array($u, $c));
setcookie("siteLogin", $cookie, time() + 36000, "/");

$u is username, $c is a cookie value (this is part of a remember script for logins).

now, when I come to extract it, I have this:

Code:
if (isset($_COOKIE['siteLogin'])){

    echo $_COOKIE['siteLogin']."<br />";
    echo unserialize($cookie)."<br />";
    list($u, $c) = @unserialize($cookie);
    echo "username: ".$u."  cookie: ".$c;

}

the echo is just for debugging, ive checked if the cookie contains a value, it does (and looks serialized). I can;t echo it unserialized or listed, am I doing something wrong?

here is a test url:

http://munkydesigns.co.uk/ScriptsNew/login.php

u: username
p: password

make sure you check remember me lol

thanks in advance :)
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
First thing, look at unserialize error messages:

Code:
    list($u, $c) = [b]unserialize[/b]($cookie);
 
0
Close
there are no error messages :(
 
0
Close
Can you paste the serialized cookie here?
 
0
Close
You are referring to a variable $cookie which doesnt seem to be set unserialize($_COOKIE['siteLogin']); should do it.
 
1
Close
Jido, here is the serialized cookie: a:2:{i:0;s:8:\"username\";i:1;s:32:\"29ef5859fbfe34e0918bdec7ec00a988\";}

Neroux, good spot haha. thats because I copied and pasted the snippet from the actual function, with $cookie as a parameter. However, even as :

PHP:
echo $_COOKIE['siteLogin']."<br />";
echo unserialize($_COOKIE['siteLogin'])."<br />";
list($u, $c) = @unserialize($_COOKIE['siteLogin']);
echo "username: ".$u."  cookie: ".$c;

I get no outcome :(
 
0
Close
I know you said the cookie contains the value, but can you check it once again? The reason is, in your example I am not even getting the cookie but only the session cookie.
 
0
Close
Try this, it's ugly and someone will probably yell at me. But it seems to work.

PHP:
$u = "hello";
$c = "world";

$cookie = urlencode(serialize(array($u, $c)));

if(!setcookie("siteLogin", $cookie, time() + 20000)) die("Hmm");

if(isset($_COOKIE['siteLogin'])) {
		
	$line = urldecode($_COOKIE['siteLogin']);
	
	if (get_magic_quotes_gpc()) $line = stripslashes($line);
	
	list($a, $u) = unserialize($line);
	echo "username: " . $a . "<br />pass: " . $u;
}

Remember: Cookie names can not contain any of the following '=,; \t\r\n\013\014
 
Last edited:
0
Close
cheers Danltn, worked a treat :)
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back