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 08-19-2008, 08:34 PM   #1 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
shownomercy is on a distinguished road
Default Storing Session data in $_COOKIE as well

I have been struggling for a while on how to share information stored in $_SESSION in order to avoid pop up issues with IE and etc. I would really like some insight into how to deal more gracefully with cookies, from someone who uses them more often than i.

Basically all I want to do is populate the session data into the cookie. Here is an example of code that would be nice to work:


PHP Code:
$userid $_SESSION['userid'];
$username $_SESSION['username'];
$password $_SESSION['password'];
$userlevel $_SESSION['userlevel'];

setcookie("uservars['userid']"$userid);    
setcookie("uservars['username']"$username);    
setcookie("uservars['password']"$password);    
setcookie("uservars['userlevel']"$userlevel); 
For whatever reason, setting the cookies on the same script the session is being set from POST is not working properly either ... but I would be happy to employ another similar method.

The only cookie that is working is the one to identify the session ... erg
shownomercy is offline  
Reply With Quote
Old 08-19-2008, 09:46 PM   #2 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

You shouldn't do this, it's totally unsafe to store data from sessions in cookies. That's why there are sessions so you can store data the client shouldn't see.
sjaq is offline  
Reply With Quote
Old 08-19-2008, 10:01 PM   #3 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
shownomercy is on a distinguished road
Default

Fair enough, although for this particular application, security is not a real issue.

The problem is that sessions aren't persistent through page loads due to having a load balanced server. There is also the issue of pop ups killing the session in IE. I am trying to work out a two birds, one stone approach ;(

Do you know of any workarounds for this that don't involve using cookies? I certainly prefer sessions all things equal. But I certainly can't be having a user authenticate on each new page ...
shownomercy is offline  
Reply With Quote
Old 08-20-2008, 12:05 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Never ever store their password in a public location. It's perfectly safe to store other session data in a cookie, so long as the authentication doesn't take place directly from the cookie to the application.

Just out of curiousity, what is this about popups in IE killing sessions?
-m
delayedinsanity is offline  
Reply With Quote
Old 08-20-2008, 01:19 PM   #5 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
shownomercy is on a distinguished road
Default

Thanks for all your input so far ... again security for this particular bit of code is not an issue, I just want to figure out why setting cookies isn't working ...

The IE issue is pretty well known, I run into it mostly when spawning pop ups (to input data etc.). There is a lot of information about it here.
shownomercy is offline  
Reply With Quote
Old 08-20-2008, 02:19 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Never heard of it till now, but as for your cookie problem, have you tried setting your own expiration on them? According to the manual;

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

So perhaps you're running into a situation where the cookie is expiring before the browser is closing, for whatever reason. Try the following perhaps;

PHP Code:
setcookie("uservars['userid']"$useridtime()+60*60*24*14);     
setcookie("uservars['username']"$usernametime()+60*60*24*14);     
setcookie("uservars['password']"$passwordtime()+60*60*24*14);     
setcookie("uservars['userlevel']"$userleveltime()+60*60*24*14); 
Which will set them to expire in two weeks time (you can change this to anything you wish).
-m
delayedinsanity is offline  
Reply With Quote
Old 08-21-2008, 10:26 AM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Setting session variables then setting cookies with the data from the sessions breaks the whole concept of sessions and as people have said, potentially causes a security issue.

For a load balanced server setup you may be interested in using the DB to store session information with:
session_set_save_handler


That way the sessions are no longer stored on the server but in the DB (which should be central and available to all boxes) therefore they exist to all servers on the load balancer (in theory)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 08-21-2008 at 12:16 PM.
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
buggabill (08-21-2008)
Old 08-21-2008, 01:01 PM   #8 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

@sketchMedia
I have been working on a login system here, and this will suit my needs well. I had never thought to look for something like it. I was coding my own solution, but this will make things a lot easier. Thanks!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 08-21-2008, 02:00 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

no probs m8, glad to help.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia 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 01:58 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