Trusten
07-15-2002, 12:32 AM
<?php
// Login Routine and Logout Routine
//
// VARS:
// $user
// $password
// zcommon.php for std set of vars and message text
// Include common variables and message text
// include 'zcommon.php';
// Normally these would come from an include file. Since this is
// an example, we'll just put them here:
// db login parameters
$dbhost = "YOURHOSTHERE";
$dbuser = "YOURUSERHERE";
$dbpassword = "YOURPASSHERE";
$db = "YOURDBHERE";
$sysadminemail = "sjohnson@fuzzygroup.com";
switch ($action) {
case login:
process_login();
die();
case logout:
//null out cookies at start of login routine
// note on using cookies.
// MUST BE SET before ANY http output.
// They TRAVEL in the http HEADER so have to go first.
setcookie ("ck_username", "");
setcookie("ck_password", "");
setcookie("ck_user_id", "");
die();
}
function process_login() {
global $dbhost;
global $dbuser;
global $dbpassword;
global $db;
// define homepage and text variables
global $homepage;
global $homedir;
global $sysadminemail;
global $userstable;
//form vars
global $username;
global $password;
// Connecting, selecting database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpassword")
or die("Could not connect");
mysql_select_db("$db")
or die("Could not select database");
//Check that the user exists in the db and if not, create an
// error page
$query = "SELECT user_id FROM imsaver_users WHERE "
. "username='$username'";
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");
//Logic concept: if the user_id doesn't exist, an empty string
// or "" will be returned with the $user_id call below.
// We can test this to see if the user has entered the username
// correctly
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
$user_id = $row[0];
//very important for user friendliness -- tell them
// what the login error was -- incorrect
// username or incorrect password
// first test -- did the username exist
if ($user_id == "") {
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "Incorrect username";
print "</TITLE>";
print "<BODY>";
print "<CENTER>";
print "<B><CENTER>We're sorry but the username that you";
print "entered doesn't seem to exist in our database.<BR>";
print "Perhaps you entered it in error. Press the back button ";
print "to try again.";
}
else {
//this means that there was 1 result from the query so that
// username exists in the database
//now have to verify password. Basically same code.
$query = "SELECT password "
. " FROM imsaver_users "
. " WHERE username='$username'";
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");
//Encrypt the password the user entered since our
// database stores it in encrypted fashion and we need to
// compare it this way
$encryptedpassword = md5($password);
$row = mysql_fetch_array($result);
//grab the password from the row array, 0th element
// since only 1 column selected
// have to use a variable $passwordfromdb so we don't
// overwrite our $password variable from the form var
$passwordfromdb = $row[0];
if ($encryptedpassword == $passwordfromdb) {
//set our cookies for our future security checks
setcookie ("ck_username", $username);
setcookie("ck_password", $password);
setcookie("ck_user_id", $user_id);
// Create our results page showing them they are logged in
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "You're Logged In!";
print "</TITLE>";
print "<BODY>";
print "You're Logged In";
//This needs to have a link added of course
//If you wanted to automatically take them to the main screen
// then use the header function to redirect them
print "Click Here to Continue";
print "</BODY>";
print "</HTML>";
//close the database
// Closing connection
mysql_close($link);
}
else {
//passwords didn't match so make an error page
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "Incorrect password";
print "</TITLE>";
print "<BODY>";
print "<CENTER>";
print "<B><CENTER>We're sorry but the password that you entered";
print "doesn't match with the one in our database.<BR>";
print "Press the back button to try again.";
print "</CENTER>";
print "</BODY>";
print "</HTML>";
// Closing connection
mysql_close($link);
}
}
}
?>
PLEASE HELP ME WITH THIS CODE. each time i try to use it, firstly, it doesn't mask the user name and id, and secondly, it gives me a blank page. this is the form i'm suppose to use with it.
<form name=registration action="logme.php" method="get">
Username:
<input name="username" type="text" width="10"><BR>
Password:
<input name="password" type="password" width="10"><BR>
<INPUT TYPE=SUBMIT VALUE="Log In">
</form>
// Login Routine and Logout Routine
//
// VARS:
// $user
// $password
// zcommon.php for std set of vars and message text
// Include common variables and message text
// include 'zcommon.php';
// Normally these would come from an include file. Since this is
// an example, we'll just put them here:
// db login parameters
$dbhost = "YOURHOSTHERE";
$dbuser = "YOURUSERHERE";
$dbpassword = "YOURPASSHERE";
$db = "YOURDBHERE";
$sysadminemail = "sjohnson@fuzzygroup.com";
switch ($action) {
case login:
process_login();
die();
case logout:
//null out cookies at start of login routine
// note on using cookies.
// MUST BE SET before ANY http output.
// They TRAVEL in the http HEADER so have to go first.
setcookie ("ck_username", "");
setcookie("ck_password", "");
setcookie("ck_user_id", "");
die();
}
function process_login() {
global $dbhost;
global $dbuser;
global $dbpassword;
global $db;
// define homepage and text variables
global $homepage;
global $homedir;
global $sysadminemail;
global $userstable;
//form vars
global $username;
global $password;
// Connecting, selecting database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpassword")
or die("Could not connect");
mysql_select_db("$db")
or die("Could not select database");
//Check that the user exists in the db and if not, create an
// error page
$query = "SELECT user_id FROM imsaver_users WHERE "
. "username='$username'";
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");
//Logic concept: if the user_id doesn't exist, an empty string
// or "" will be returned with the $user_id call below.
// We can test this to see if the user has entered the username
// correctly
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
$user_id = $row[0];
//very important for user friendliness -- tell them
// what the login error was -- incorrect
// username or incorrect password
// first test -- did the username exist
if ($user_id == "") {
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "Incorrect username";
print "</TITLE>";
print "<BODY>";
print "<CENTER>";
print "<B><CENTER>We're sorry but the username that you";
print "entered doesn't seem to exist in our database.<BR>";
print "Perhaps you entered it in error. Press the back button ";
print "to try again.";
}
else {
//this means that there was 1 result from the query so that
// username exists in the database
//now have to verify password. Basically same code.
$query = "SELECT password "
. " FROM imsaver_users "
. " WHERE username='$username'";
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");
//Encrypt the password the user entered since our
// database stores it in encrypted fashion and we need to
// compare it this way
$encryptedpassword = md5($password);
$row = mysql_fetch_array($result);
//grab the password from the row array, 0th element
// since only 1 column selected
// have to use a variable $passwordfromdb so we don't
// overwrite our $password variable from the form var
$passwordfromdb = $row[0];
if ($encryptedpassword == $passwordfromdb) {
//set our cookies for our future security checks
setcookie ("ck_username", $username);
setcookie("ck_password", $password);
setcookie("ck_user_id", $user_id);
// Create our results page showing them they are logged in
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "You're Logged In!";
print "</TITLE>";
print "<BODY>";
print "You're Logged In";
//This needs to have a link added of course
//If you wanted to automatically take them to the main screen
// then use the header function to redirect them
print "Click Here to Continue";
print "</BODY>";
print "</HTML>";
//close the database
// Closing connection
mysql_close($link);
}
else {
//passwords didn't match so make an error page
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "Incorrect password";
print "</TITLE>";
print "<BODY>";
print "<CENTER>";
print "<B><CENTER>We're sorry but the password that you entered";
print "doesn't match with the one in our database.<BR>";
print "Press the back button to try again.";
print "</CENTER>";
print "</BODY>";
print "</HTML>";
// Closing connection
mysql_close($link);
}
}
}
?>
PLEASE HELP ME WITH THIS CODE. each time i try to use it, firstly, it doesn't mask the user name and id, and secondly, it gives me a blank page. this is the form i'm suppose to use with it.
<form name=registration action="logme.php" method="get">
Username:
<input name="username" type="text" width="10"><BR>
Password:
<input name="password" type="password" width="10"><BR>
<INPUT TYPE=SUBMIT VALUE="Log In">
</form>