User Login System with Sessions (Rated 5)Description:
You can use this script to see how to log someone onto your site.
It uses sessions, but could easily be adapted for using cookies as well if required.
This is particularly useful for allowing users to add news etc to your site, or just to allow them to vote on news etc etc Code starts here
<?PHP session_start(); ?>
<form action="index.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td><div class="small" align="right">User Name</div></td>
<td><input name='username' type='TEXT' id="username" value="" maxlength='20'></td>
</tr>
<tr>
<td><div class="small" align="right">Password</div></td>
<td><input name="password" type="password" id="password" value=""></td>
</tr>
<tr>
<td><div align="center"></div></td>
<td><div align="left"><input type="submit" name="userlogin" value="Login"></div></td>
</tr>
</table>
</form>
<?PHP
// check login and password
// connect and execute query
// db_connect is a script which provides a bd connection
include_once('db_connect.php');
if(isset($_POST['userlogin']))
{
$username = strip_tags($_POST['username']) ;
$password = md5($_POST['password']) ;
$query = "SELECT UserID, UserName, Password from UserTable WHERE username ='$username' AND password = '$password'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// if row exists - login/pass is correct
if (mysql_num_rows($result) == 1)
{
list($id, $username, $password) = mysql_fetch_row($result);
// initiate a session
// as we gonna log the user in too
// register the user's ID
session_register('UserID');
session_register('Username');
$_SESSION['UserID'] = $id;
$_SESSION['Username'] = $username;
// Redirect them as logon sucessful
}
else
// login/pass check failed
{
// Handle the bad logon
}
}
?>
Submitted by Devscripts on 07-03-2003 14:22 |