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
Session help
Moderator: General Moderators
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
-
Sphen001
- Forum Contributor
- Posts: 107
- Joined: Thu Mar 10, 2005 12:24 pm
- Location: Land of the Beaver
Hi,
Creating a session is simpler than you think
Here's a sample script
Hope this helps 
Sphen001
EDIT: And yes, the session is destroyed once the browser is closed.
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();Sphen001
EDIT: And yes, the session is destroyed once the browser is closed.
Re: Session help
retrive data from db and register sessionsandylyon87 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
$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'];
?>Session is active until users browser is active. If user exit all browser windows that are active with session - session dies.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
Might want to read [url]http:/www.php.net/session[/url]
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
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();
}
}