Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Newb question about sessions and security
Old 12-17-2007, 03:12 PM Newb question about sessions and security
Mad182's Avatar
Skilled Talker

Posts: 54
Name: Madars
Location: Latvia
Trades: 0
I've been using cookie based user autentification system for a while, and now decided to switch to sessions.
Actually I don't know anything about what I'm doing

Basically the question is - is this safe:
PHP Code:
<?php
session_start
();
header("Cache-control: private");
if (@
$_SESSION["logged"]) {
    if (isset(
$_GET['logout']) && $_GET['logout'] == "true") {
        
session_destroy();    
        
header('Location: file.php');
    }
?>
<h2>CONTENT HERE</h2>
stuff
stuff
stuff
<a href="?logout=true">logout</a>
<?php
} elseif (isset($_POST['user']) && isset($_POST['pass']) && strtolower($_POST['user']) == "guest" && $_POST['pass'] == "root") {
    
$_SESSION["logged"] = true;
    
header('Location: file.php');
} else {
?>
<h2>LOG IN!</h2>
<form action="file.php" method="post">
    Username:<br />
    <input type="text" name="user" /><br />
    Password:<br />
    <input type="password" name="pass" /><br />
    <input type="submit" value="Log in..." />
</form>
<?php ?>
Can someone "fake" the session?

Maybe there is any good guide about session based login systems?
I tried reading php.net manual, but didn't get everything about session id's and times.
__________________
There's no place like 127.0.0.1

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
 
Register now for full access!
Old 12-17-2007, 04:11 PM Re: Newb question about sessions and security
Experienced Talker

Posts: 30
Trades: 0
I think sessions can't be faked no.

I believe the best way to use a session is to store the userid in it.
So instead of $_SESSION['logged']=true; Put his id or in it. So you can call info of the logged user later from your database. For the rest, session are quite easy I guess, just give them a value, if correct login, destroy them by logging out (or by closing browser or timeout) en use the session as an id value of the logged user.
klaroen is offline
Reply With Quote
View Public Profile
 
Old 12-19-2007, 02:10 AM Re: Newb question about sessions and security
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
How do you think a session differs from a cookie?

If you cannot answer this question, I will do. Cookie contains the information which is sent to your script every time a user requests a page. You can store user login and hash of user password in a cookie and check whether this user is valid every time he requests a page. Session stores this information on your server, but to identify a user the cookie is still used, but in this case it contains only the session id.

So if you receive a request to authorize the user with login and password provided and make $_SESSION['logged'] = true the session can be faked if another user sends you the same session id. Quite the same way as a cookie can be faked if another user sends you a cookie with user login and password hash.

So? Obviously sessions are neither less secure nor more secure than cookies. They only differ in place where the data is stored - either on client or on the server. If you need more security you should bind user's cookie to something else like IP address.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-19-2007, 03:52 AM Re: Newb question about sessions and security
Experienced Talker

Posts: 30
Trades: 0
But sessions are a bit safer because they run out and get destoryed on browser close.
klaroen is offline
Reply With Quote
View Public Profile
 
Old 12-19-2007, 04:12 AM Re: Newb question about sessions and security
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
I insist that you carefully RTFM about sessions until you understand it clearly.
Session IS NOT destroyed on browser close. It is a COOKIE that CAN BE set to be removed on browser close. Session will be removed either according to garbage collection settings or by forcing session_destroy(), in other case it will remain being usable.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-19-2007, 06:27 AM Re: Newb question about sessions and security
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
And I will add, that somebody forging a cookie (or if it's enabled, passing an PHPSESSID= parameter) with a valid id can hijack the session.

I'd say that sessions are a more flexible than cookies, but not that much safer.
You still have to be careful what you save in it, and do more check depending the security level you need.

For instance, you could store the browser ip in a db and the session, and check it on each page load.
If you see that the ip changed between 2 calls, you invalidate the session, and ask the user to log back in.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Newb question about sessions and security
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29630 seconds with 12 queries