Welcome to WebmasterWorld Guest from 54.224.202.224

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php session instead of cookie

     
11:22 am on Aug 31, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


I am currently setting a cookie and reading it like this:

setcookie("user", "Alex", $expire);

if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "<br>";
else
echo "Welcome guest";

How can I do the exact same thing but with PHP sessions so that as long as the current browser window is opened the session info will always remain?

thanks

11:56 am on Aug 31, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12547
votes: 2


You can use the session [php.net] cookie handlers.
6:01 pm on Aug 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Almost the same syntax. You just have to set your session variable where you set that cookie for username $_SESSION['user'] = $username;

if (isset($_SESSION["user"]))
echo "Welcome " . $_SESSION["user"] . "<br>";
else
echo "Welcome guest";

10:27 am on Sept 1, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


thanks for your replies. will it be safe to store a users password in a session variable? i need to store the password so that i can log the user into certain applications automatically. or should i store it encrypted? and then decrypt it when needed?
1:07 pm on Sept 1, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2004
posts:941
votes: 0


its relative safe but take note that the sessions have a limited life span (around 20-25 mins by default if page is idle all that time if i remember correctly or until the user closes the browser)

you can change that of course
but you cannot relog the user on next visit. You will force him to re-login manually

1:42 pm on Sept 1, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


If you want to save their log in after they close the browser for an extended period of time then you need to use a COOKIE. Also, for the security issue, you can store the passwords encrypted, but anything that can be decrypted can be hacked and someone could catch it. What I would recommend is having an encrypted session variable name with an encrypted value. Look at this php

//if the user is logged in set the variable to this
$_SESSION['userLogged'] = md5("loggedIn");

var_dump($_SESSION['userLogged']);

Just use their username to get their information, but check if that session variable equals that to see if they are logged in.

10:53 pm on Sept 2, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


...sessions have a limited life span (around 20-25 mins by default if page is idle...

Yes, default value of session.gc_maxlifetime is 1440 secs (24mins).

12:44 am on Sept 3, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Also, you can change your session time (if you run the server that hosts your website that is). If you have facebook open it sometime and just leave it open and see how their sessions are. It's a really long time.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members