Session help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Session help

Post 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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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 :D

Sphen001

EDIT: And yes, the session is destroyed once the browser is closed.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Re: Session help

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 &quote;until the browser is closed.&quote; 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();
  }
}
Post Reply