There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 boot bsod computer cpu crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware help please hijackthis hjt hjt log install internet internet explorer itunes javascript keyboard lan laptop log malware monitor network networking outlook outlook 2003 outlook express password php popups problem router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless youtube
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: Secure sessions in PHP


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
05-May-2008, 06:18 AM #1
Solved: Secure sessions in PHP
I'm trying to incorperate sessions into an admin section of my site but I'm a bit confused about how to actually authenticate a user as they go to each page. I have looked at quite a few tutorials but they don't seem to explain it. I've heard that you shouldn't store the username and password in session variables and you should store a session ID instead. Where is the session ID stored server-side, how is it created, and how is it cleared when the session expires?

I'd be grateful for any help.
__________________
There's no place like 127.0.0.1

TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
10-May-2008, 12:36 PM #2
Bump. Has nobody here worked with secure sessions?
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
10-May-2008, 03:46 PM #3
You can store the username and password in session variables, just not in a cookie. The session ID is stored in a cookie (usually.) This is secure, assuming your server or webhost is setup properly.

Typically, you would authenticate the user, then just store a session variable like $logged_in = true; or $user_id = 5;.
__________________
-Brendan
Mudley's Avatar
Computer Specs
Member with 99 posts.
 
Join Date: Apr 2008
Experience: Advanced
10-May-2008, 10:38 PM #4
i've never worked with sessions at all.
my site is too big for sessions
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
11-May-2008, 03:56 AM #5
Thanks for your replies. Would I store the session ID in the MySQL database with the login details, or somewhere else so it knows who is logged in?
Big-K's Avatar
Distinguished Member with 6,878 posts.
 
Join Date: Nov 2003
Location: Kansas
Experience: Advanced
11-May-2008, 05:01 AM #6
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
11-May-2008, 07:42 PM #7
Session IDs are typically saved in a temp file (session.save_path in php.ini.) There are also 3rd-party session management implementations that use MySQL if you prefer.
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
12-May-2008, 01:28 PM #8
So can I just store the session ID in the members database, and use session_id() to find the ID and logon the user? Would this be insecure because of the stored session ID, or would that method work?
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
12-May-2008, 09:25 PM #9
The session ID is stored by PHP. You can store the user's ID in a session variable, like $_SESSION['user_id'].
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
13-May-2008, 01:04 PM #10
So to authenticate someone would I store the username and password in session variables? I'm reading a book on website security and it says not to do that...
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
14-May-2008, 12:47 PM #11
You don't need store both, just authenticate the user and then store the username in a session variable.
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
14-May-2008, 05:18 PM #12
Sorry if I sound picky, but doesn't that make session hijacking really easy? The username is a known variable, so all someone would need to do is create a fake session with the username session variable.
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
14-May-2008, 10:33 PM #13
Session variables are stored on the server. The user can't set/change them.
TheRobatron's Avatar
Computer Specs
Senior Member with 391 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
15-May-2008, 12:06 PM #14
I get it now Thanks for all your help!
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
15-May-2008, 06:24 PM #15
yourewelcome
Reply

Tags
php, sessions


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

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

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

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:07 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.