Home > Archive > PHP Programming > December 2006 > serialize() and unserialize()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
serialize() and unserialize()
|
|
| powellgg@masters.edu 2006-12-20, 3:59 am |
| I've just taken over a PHP website and am converting it to ASP.NET
(don't shoot!). I'm not a PHP guy so I'm doing a lot of searching for
things that I aren't obvious, and I'm hoping I'll be able to get help
from here for things I can't find on the Internet. Here's the first...
Here's the PHP code:
setcookie("site_search_keyword","", time() - 3600);
$site_search_keyword =
unserialize(base64_decode($site_search_k
eyword));
Microsoft's PHP to ASP.NET convert translated it to this:
PHP.HttpSupport.SetCookie("site_search_keyword", "",
PHP.DateTimeSupport.Time() - 3600);
site_search_keyword =
PHP.TypeSupport. ToString(unserialize(base64_decode(site_
search_keyword)));
..Net has no corresponding method for unserialize() and I can't find
where the page/site does the initial serialization. Is there an
underlying serialization when it is creating the cookie? If the first
line is setting a null value to the cookie, why would the second line
try to read it? It's going to be the same every time... right?
| |
| Kimmo Laine 2006-12-20, 3:59 am |
| <powellgg@masters.edu> wrote in message
news:1166598953.760601.100690@73g2000cwn.googlegroups.com...
> I've just taken over a PHP website and am converting it to ASP.NET
See, there's your problem, right there. Just don't do that and you're fine.
| |
| pangea33 2006-12-20, 3:59 am |
| powellgg@masters.edu wrote:
> I've just taken over a PHP website and am converting it to ASP.NET
> (don't shoot!). I'm not a PHP guy so I'm doing a lot of searching for
> things that I aren't obvious, and I'm hoping I'll be able to get help
> from here for things I can't find on the Internet. Here's the first...
>
> Here's the PHP code:
>
> setcookie("site_search_keyword","", time() - 3600);
> $site_search_keyword =
> unserialize(base64_decode($site_search_k
eyword));
>
> Microsoft's PHP to ASP.NET convert translated it to this:
>
> PHP.HttpSupport.SetCookie("site_search_keyword", "",
> PHP.DateTimeSupport.Time() - 3600);
> site_search_keyword =
> PHP.TypeSupport. ToString(unserialize(base64_decode(site_
search_keyword)));
>
>
> .Net has no corresponding method for unserialize() and I can't find
> where the page/site does the initial serialization. Is there an
> underlying serialization when it is creating the cookie? If the first
> line is setting a null value to the cookie, why would the second line
> try to read it? It's going to be the same every time... right?
There is equivalent functionality available in .NET. Googled it...
http://www.411asp.net/func/search? ...cat=all&x=0&y=0
http://www.google.com/search?q=base...=&start=10&sa=N
| |
| Toby Inkster 2006-12-20, 3:59 am |
| powellgg wrote:
> .Net has no corresponding method for unserialize() and I can't find
> where the page/site does the initial serialization. Is there an
> underlying serialization when it is creating the cookie? If the first
> line is setting a null value to the cookie, why would the second line
> try to read it? It's going to be the same every time... right?
serialize() converts from a nested array structure to a string.
unserialize() converts back the other way. This provides a convenient
method of storing a nested array structure into a file, database or
cookie.
These functions are very PHP-specific, though some people have written
functions to deal with PHP-style serialization in other languages. Of
interest to you might be:
http://csphpserial.sf.net/
In general though, I tend to recommend *against* rewriting code just
because it's in a language you don't know. You may have a very good
reason to be rewriting this website, but do consider whether a full
rewrite is the best solution, or whether it might be better to learn
some PHP and maintain the website that way. It's always a good idea
to expand ones repetoire of skills.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
| |
| seaside 2006-12-20, 6:59 pm |
|
powellgg@masters.edu schrieb:
> .Net has no corresponding method for unserialize() and I can't find
> where the page/site does the initial serialization. Is there an
> underlying serialization when it is creating the cookie? If the first
> line is setting a null value to the cookie, why would the second line
> try to read it? It's going to be the same every time... right?
..Net has serialization support, as many other languages too:
http://www.codeguru.com/columns/Dot...icle.php/c6595/
In fact, each language/architeture has its own, often proprietary
format.
In case you'd like to go xplat, you might wish to serialize into an XML
string.
| |
| Willem Bogaerts 2006-12-20, 6:59 pm |
| > ... and I can't find
> where the page/site does the initial serialization.
In earlier versions of PHP, it was normal that page parameters were
visible in scripts as variables. So if you requested
http://www.example.com/index.php?user=john , you could use the $user
variable in your script, which would contain 'john' if you did not
overwrite that value.
So it might just be that the variable is set by another page or a form.
In this case, you'll encounter the serialization as well, so you can do
it "the .NET way".
Best regards
--
Willem Bogaerts
Application smith
Kratz B.V.
http://www.kratz.nl/
| |
| powellgg@masters.edu 2006-12-21, 3:59 am |
| True... but this is what they hired me for :).
Kimmo Laine wrote:
> <powellgg@masters.edu> wrote in message
> news:1166598953.760601.100690@73g2000cwn.googlegroups.com...
>
>
> See, there's your problem, right there. Just don't do that and you're fine.
| |
| powellgg@masters.edu 2006-12-21, 3:59 am |
| Thanks for the info! Fortunately or unfortunately I was hired for the
purpose of doing this. The organization went through a process of
determining whether to stay with PHP or move to .Net. I wasn't privy
to those meetings so I have no idea what the discussions where like...
but the end result is they hired me to do this. But I am sure I will
definitely learn some PHP along the way :)
Toby Inkster wrote:
> powellgg wrote:
>
>
> serialize() converts from a nested array structure to a string.
> unserialize() converts back the other way. This provides a convenient
> method of storing a nested array structure into a file, database or
> cookie.
>
> These functions are very PHP-specific, though some people have written
> functions to deal with PHP-style serialization in other languages. Of
> interest to you might be:
>
> http://csphpserial.sf.net/
>
> In general though, I tend to recommend *against* rewriting code just
> because it's in a language you don't know. You may have a very good
> reason to be rewriting this website, but do consider whether a full
> rewrite is the best solution, or whether it might be better to learn
> some PHP and maintain the website that way. It's always a good idea
> to expand ones repetoire of skills.
>
> --
> Toby A Inkster BSc (Hons) ARCS
> Contact Me ~ http://tobyinkster.co.uk/contact
|
|
|
|
|