Page 1 of 1
Session help
Posted: Fri Jul 01, 2005 6:55 am
by andylyon87
Hey guys
I have had a look at the php freaks tutorial on php sessions an still dont understand. How would I go about saving someones username and password to a session on the server and then call it in a form later.
eg. have a master login in the first page and then once logged in allow the users to do what ever they like in the site.
Thanks
Andy
Posted: Fri Jul 01, 2005 6:58 am
by andylyon87
does the session automatically recognise the browser as I have jus read that the session dies after the user exits their browser
Andy
Posted: Fri Jul 01, 2005 1:43 pm
by Sphen001
Hi,
Creating a session is simpler than you think
Here's a sample script
Code: Select all
//user logged in, this is the processing page
//check their creds and such
//start a session
session_start();
//now add their username to the session
$_SESSION['username'] = $_POST['username'] //Assuming you got your user info from a POST form
//You can add as many different session values as you like, and call them on other pages. Just be sure to call session_start() at the beginning of every page.
//To logout a user
unset $_SESSION['username'];
session_destroy();
Hope this helps
Sphen001
EDIT: And yes, the session is destroyed once the browser is closed.
Re: Session help
Posted: Fri Jul 01, 2005 1:48 pm
by ddragas
andylyon87 wrote:Hey guys
I have had a look at the php freaks tutorial on php sessions an still dont understand. How would I go about saving someones username and password to a session on the server and then call it in a form later.
eg. have a master login in the first page and then once logged in allow the users to do what ever they like in the site.
Thanks
Andy
retrive data from db and register sessions
$username = $_SESSION['username'] = row['username'];
$password = $_SESSION['password'] = row['password'];
on all pages put text on top of page - BEFORE EVERYTHING
Code: Select all
<?
session_start();
$username = $_SESSIONї'username'];
$password = $_SESSIONї'password'];
?>
andylyon87 wrote:does the session automatically recognise the browser as I have jus read that the session dies after the user exits their browser
Andy
Session is active until users browser is active. If user exit all browser windows that are active with session - session dies.
Posted: Fri Jul 01, 2005 6:48 pm
by timvw
Might want to read [url]http:/www.php.net/session[/url]
Code: Select all
session.cookie_lifetime integer
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "e;until the browser is closed."e; Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params().
I wouldn't store the password. You probably only need it to make sure there is such a user + password combination. Once that is verified you only need to remember the user and the result of the verification.
Code: Select all
if (!isset($_SESSION))
{
session_start();
}
function login($username, $password)
{
// see they are in the database
if (in_database($username, md5($password)))
{
$_SESSION['user'] = $username;
$_SESSION['valid'] = true;
}
}
function logout()
{
unset($_SESSION['user']);
$_SESSION['valid'] = false;
unset($_SESSION['valid']);
}
function requirevalid()
{
if (!$_SESSION['valid'])
{
header('Location: http://example/login');
die();
}
}