Re: Sessions

From: Date: Sat, 17 May 2003 18:43:42 +0000
Subject: Re: Sessions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148036@lists.php.net to get a copy of this message
At 17:43 17.05.2003, Ryan A said: --------------------[snip]-------------------- >All i want is basically to get the username/pass from a form and then pass >it to my pages plus the page should expire in n minutes (using cookies i >guess??? have not done that yet) > >Another reason I wanted to do this is coz i know sessions are important and >i didnt have a clue about them, never used them before. --------------------[snip]-------------------- Try to see sessions (I mean, the $_SESSION array) as a variable that lives across multiple connects from the same client browser. How it works is completely transparent; don't bother with the intricacies until you get aquainted with using sessions. About your problem, you can very easily go this way (untested but should work as planned): -- login.php -- <?php // HTML to send if the user is not logged in $html_login = <<< EOT <form action="{$_SERVER['PHP_SELF']}" method="POST"> <b>User Name:</b> <input type="text" name="uid"><br /> <b>Password:</b> <input type="password" name="pass"><br /> <input type="submit" name="login" value="Log In"> EOT; session_start(); if (!$_SESSION['loginid']) { // this code gets control if the user is not logged in if (array_key_exists('login', $_POST)) { // login form has been submitted, check results // this function needs to be implemented $loginid = check_login($_POST['uid'], $_POST['pass']); if ($loginid) $_SESSION['loginid'] = $loginid; } } if (!$_SESSION['loginid']) { // user is logged in; redirect to other page header('Location: private_page.php' . (SID ? '?' . SID : null)); exit; } // user is not logged in, present the login page echo $html_login; ?> -- end login.php -- -- any other (PHP) page -- <?php session_start(); if (!$_SESSION['loginid']) { header('Location: login.php' . (SID ? '?' . SID : null)); exit; } ?> -- end any other (PHP) page -- This is a very simple but effective method. The only problem that may occur in passing the session identifier between pages is if the client doesn't accept cookies, or the server is configured to not use session cookies. To handle this, I am using the construct (SID ? '?' . SID : null) to append to any "Location" header. The SID is a constant that is empty if the server can use cookies, or set to "PHPSESSID=xxxx" otherwise. Unfortunately PHP cannot automatically rewrite Location headers (as it does with any other href link, or form), so we need to handle this separately. HTH, -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#148036) next »