TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-19-2008, 10:04 AM   #1 (permalink)
The Contributor
 
Devels's Avatar
 
Join Date: Nov 2007
Posts: 27
Thanks: 2
Devels is on a distinguished road
Default encoding cookie content

I want to accomplish a shopping cart that is storing its content in the database and setting a cookie.
This cookie needs to be refered to the shopping cart, even if the visitor comes back the orginal shoppingcart still excist just like Amazon, so a php session_id is not the best solution?
The question is, what values needs the cookie to be? I can set the shooping cart ID, only that is not really safe. It needs to be encoded somehow. Someone good advise about the smartest/safest solution?
Devels is offline  
Reply With Quote
Old 02-19-2008, 11:13 AM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Well, the only good ways to encode a string, is first to put it in an array. Then you can serialise() it and then write it into the cookie, which can be decoded with unserialise(). If you prefer NOT to use an array but a string, you should first urlencode() it and then base64_encode(). Both can decoded again with their opposite functions called urldecode() and base64_decode().

I hope this helps you out.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-19-2008, 12:00 PM   #3 (permalink)
The Contributor
 
Devels's Avatar
 
Join Date: Nov 2007
Posts: 27
Thanks: 2
Devels is on a distinguished road
Default

Everything will be stored on the server, I only need to identify the visitor each time. So I only need to have a ID in the cookie, but that is not safe because, of the cookie manipulation. So I can encode it, but base64 is also not a good idea because still everyone can encode/decode it and manipulate the cookie.

I also can generate a unique string in the database and use that as an cookievalue.
But why not generating a unique string with the ID or something else, so I can decode it back to the ID. But what is a good method? Maybe blowfish?
Devels is offline  
Reply With Quote
Old 02-19-2008, 05:52 PM   #4 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

You can create your own "encryption algorithm", for example:
new_cartID = cartID * 13 + 7

Then to reverse back the number:
old_cartID = (new_cartID - 7) / 13

After creating the new cartID you could base64 encode it to store it in the cookie.
This example is too simple, but if your cartID was 10, the new_cartID would be 137. If you base64 encode 137 you get MTM3. A smart guy trying to manipulate the cookie could base64 decode it back to 137.. but he doesn't know that 137 is not the real cartID.

This method won't eliminate the possibility of a person seeing the cart of another. If the smart guy changes his cookie to MTI0, your PHP script will decode it to 124, then convert 124 to 9 [(124-7)/13].. which could be a valid cartID of another customer.

If you really want security then you should look for encryption algorithms like blowfish, aes, des, RC4.

RC4 is very simple, yet powerfull, and should do the job for you.
You provide a password/key and the data and it returns the encrypted data, the only way to decrypt it is to RC4 it again using the same password/key. There are a lot of RC4 classes available for download, like this one.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-19-2008, 09:50 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Session IDs are secure. You just have to look at possible session ID downfalls in terms of security. Obviously for the session ID that determines whether or not they have access to their account, that has to be a long and complex session ID that is unlikely to be guessed. Whereas for a shopping cart, I wouldn't even say that has to be that long and complex -- after all, if I somehow guess your session ID, what will I get? Your shopping list, and nothing more. I won't even know who you are and so I can't be your wife checking up on how many other women you bought presents for!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-20-2008, 09:18 AM   #6 (permalink)
The Contributor
 
Devels's Avatar
 
Join Date: Nov 2007
Posts: 27
Thanks: 2
Devels is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Session IDs are secure. You just have to look at possible session ID downfalls in terms of security. Obviously for the session ID that determines whether or not they have access to their account, that has to be a long and complex session ID that is unlikely to be guessed. Whereas for a shopping cart, I wouldn't even say that has to be that long and complex -- after all, if I somehow guess your session ID, what will I get? Your shopping list, and nothing more. I won't even know who you are and so I can't be your wife checking up on how many other women you bought presents for!
My concern about session ID is, that I need to store it in a database for checking if the returning visitor has created a shopping cart before.

How can I link an session_id that is created before and excists in a custom cookie to a new session?
Devels is offline  
Reply With Quote
Old 02-20-2008, 07:17 PM   #7 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

You can use the session_id() function to change the id of the current session.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-21-2008, 01:56 PM   #8 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
_HItman47 is on a distinguished road
Default

Hey man,
You could generate some long random string or number, which you use as session ID. So, you store this string, as it is, in your db, and put that string, encrypted with xor algorithm, in cookie. You can use user ip address, as encryption key, so, when you see that user has that cookie set to some value, you decrypt it using that user ip, and if everything is ok, you'll get real session ID.

the only advantage of this method, compared to standard php session, is that that session id is attached to ip, so, if "hacker" will steal that session ID to do some filthy things, he wount be able to do this from another computer(if, ofcourse, he does not know about your implementation of sessionID encryption)

and sorry for my ugly English :)
_HItman47 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design