Robizzle01
Limp Gawd
- Joined
- Sep 30, 2005
- Messages
- 159
I spent nearly all of my weekend toying with this code and I just cant seem to get it to work. Hopefully whatever i'm doing is something simple. I think I documented it fairly well so you should be able to follow but if not, I'll answer any questions.
The idea was to write a script that I can include on all my pages and it will allow me to easily check if a user is logged in or not. It provides me with a function I can call that will display the login form but the nice part is, if the user is already logged in the function wont display the form -- it will display a welcome message instead. My problem is that the session variables aren't getting set. Maybe some kind of a scope issue? I haven't really figured out exactly how includes and functions effect the scope of variables.
I have created a user for you guys to play with, username is HardOPC@msu.edu (my script checks domain name portion of email address hence the @msu.edu.) The password is simply 'password'. I set up a page containing only the login form at http://www.robmeyer.net/store/login_test.php.
Here is the code for all of those pages...
login_test.php
login2.php
logout2.php
The idea was to write a script that I can include on all my pages and it will allow me to easily check if a user is logged in or not. It provides me with a function I can call that will display the login form but the nice part is, if the user is already logged in the function wont display the form -- it will display a welcome message instead. My problem is that the session variables aren't getting set. Maybe some kind of a scope issue? I haven't really figured out exactly how includes and functions effect the scope of variables.
I have created a user for you guys to play with, username is HardOPC@msu.edu (my script checks domain name portion of email address hence the @msu.edu.) The password is simply 'password'. I set up a page containing only the login form at http://www.robmeyer.net/store/login_test.php.
Here is the code for all of those pages...
login_test.php
Code:
<?php
include 'login2.php';
$logged_in = checkLoggedIn();
echo "<p>\n\$_SESSION['Username'] == '".$_SESSION['Username']."'<br />";
echo "\$_SESSION['Password'] == '".$_SESSION['Password']."'<br />";
echo "\$_COOKIE['Username'] == '".$_COOKIE['Username']."'<br />";
echo "\$_COOKIE['Password'] == '".$_COOKIE['Password']."'<br />";
echo "\$logged_in == ".$logged_in."\n</p>";
displayLogin();
?>
login2.php
Code:
<?php
/*
* Details about this specific implementation
* The database structure we are using contain fields with the following names and values:
* Email - varchar(50) - email address of user in the form [email]user@domain.exte[/email]nsion
* PasswordSalt - varchar(32) - random string different for each user
* Password - varchar(40) - The result of sha1(PasswordSalt.Plain Text Password)
* Active - binary - 0 if user has not activated account, 1 if user has
*
* For security, the SESSION['Username' / 'Password'] and COOKIE['Username' / 'Password']
* will be set to the plain text username (email address) and the sha1(PasswordSalt.Password)
*/
// checkUsernamePassword - Given the username in plain text and sha1 and salted password
// this function will check if they are a valid combination. If they are, then
// we will further check to see that the 'Active' bit is set in the database.
// This gets set when the user activates his/her account by following a link provided
// in email.
// Possible return codes:
// 0: Username / Password combination is valid and Active bit is set
// 1: Username is not in database
// 2: Username / Password combination is not valid
// 3: Username / Password combination is valid, however, Active bit is not set
// 4: Unknown error occured
// Note: in this case, the user's login name is the same as his/her email address
function checkUsernamePassword ($username, $password)
{
// connect to mysql
require 'shared_files/mysql_usedtastic.php'; // defines $mysql_username, $mysql_password and $mysql_database
$db = mysql_connect("localhost", $mysql_username, $mysql_password);
mysql_select_db($mysql_database, $db);
// check if $username is in our database
$sql = "SELECT Password, Active FROM Users WHERE Email = '".$username."'";
$result = mysql_query($sql, $db);
mysql_close();
if (!$result)
{
echo "<!-- MySQL Error: ".mysql_error()." -->";
return 4;
}
if ($row = mysql_fetch_array($result))
{
// user exists so we will check if passwords match
if ($password == $row['Password'])
{
if ($row['Active'] == 1)
{
$_SESSION['Username'] = $username;
$_SESSION['Password'] = $password;
return 0; // Username/Password combination is correct and Active bit is set
}
else
return 3; // Username/Password combination is correct, however, Active bit is not set
}
else
return 2; // Username/Password combination is not correct
}
return 1; // No such user listed in database
}
// checkLoggedIn - Checks if we either have a session or a cookie set that says the user
// is already logged in. If we find a cookie, then we set a session as well.
// If we do, then we check if the Username/Password combination they supply is valid
// Possible return codes:
// 0: Either a cookie or session validates this user as logged in
// Note: if it was a cookie, we will set the session to the same value
// 1: User has neither a valid cookie or valid session
function checkLoggedIn()
{
// Check if user has a valid cookie, if so, make it the session as well
if(isset($_COOKIE['Username']) && isset($_COOKIE['Password']))
{
$_SESSION['Username'] = $_COOKIE['Username'];
$_SESSION['Password'] = $_COOKIE['Password'];
}
// Check if session Username and Password are set
if(isset($_SESSION['Username']) && isset($_SESSION['Password']))
{
/* Confirm that username and password are valid */
if(checkUsernamePassword($_SESSION['Username'], $_SESSION['Password']) != 0){
/* Variables are incorrect, user not logged in */
unset($_SESSION['Username']);
unset($_SESSION['Password']);
return 1;
}
return 0; // user is logged in
}
// User not logged in
else
{
return 1;
}
}
/* displayLogIn() - This function will call checkLoggedIn() to see if the user is
* logged in or not. If not, then it will display the log in form. If the user
* is logged in, it will display a welcome message and a link that may be used to
* log out at any time.
*/
function displayLogIn()
{
if (checkLoggedIn() == 0)
{
// user is logged in
?>
<p>You are currently logged in as, <?php echo $_SESSION['Username']; ?></p>
<p>To log out, click <a href="logout2.php">here</a></p>
<?php
}
else
{
// user is not logged in
?>
<form action="" method="post">
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="left">Email:</td>
<td><input type="text" name="Username" /></td>
</tr>
<tr>
<td align="left">Password:</td><td><input type="password" name="Password" /></td>
</tr>
<tr>
<td align="left" colspan="2"><input type="checkbox" name="RememberMe" /> Remember me
<input type="submit" name="SubmitLogin" value="Login" />
<a href="index.php?ShowPage=CreateAccount">Sign Up</a></td>
</tr>
</table>
</form>
<?php
}
}
/* Outside of all functions
* We will check if the user has supplied a username and password
* in the login form. If so, we will check if they are correct and
* set the session variables. If the user checked the "remember me"
* checkbox then we will set cookies as well.
*/
if (isset($_POST['SubmitLogin']))
{
require 'shared_files/mysql_usedtastic.php'; // defines $mysql_username, $mysql_password and $mysql_database
$db = mysql_connect("localhost", $mysql_username, $mysql_password);
mysql_select_db($mysql_database, $db);
$sql = "SELECT PasswordSalt FROM Users WHERE Email = '".$_POST['Username']."'";
$result = mysql_query($sql, $db);
if (!$result)
echo "<!-- MySQL Error: ".mysql_error()." -->";
$row = mysql_fetch_array($result);
mysql_close();
// if form information is not correct
if (checkUsernamePassword($_POST['Username'], sha1($row['PasswordSalt'].$_POST['Password'])) != 0)
{
?>
<p>Invalid username and password combination.</p>
<?php
}
else
{
$_SESSION['Username'] = $_POST['Username'];
$_SESSION['Password'] = sha1($row['PasswordSalt'].$_POST['Password']);
if (isset($_POST['RememberMe']))
{
// if user requested to be remembered, we will set a cookie
// with the current session info (expires in 30 days.)
setcookie("Username", $_SESSION['Username'], time()+60*60*24*30, "/");
setcookie("Password", $_SESSION['Password'], time()+60*60*24*30, "/");
}
}
}
?>
logout2.php
Code:
<?php
session_destroy();
?>
<p>You have been logged out, please click <a href="index.php">here</a> to go back to the main page</p>