Free web development scripts for the webmasterDevelopment Scripts


 
 Site Navigation
 o Development Scripts
 o Text Only Edition
 o PHP.net News
 o Tutorials
 o Register an account
 o New entries this week
 o Affiliates/Links
 o Contact Us
 o About us
 o Advanced Search

 Browse Path
 o 1 user online
 o Most users online: 67
 o Home
  o PHP
   o Cookies/Sessions
    o Script

 Member Login
User Name
Password

 Standards
Valid XHTML 1.0!
Valid 
CSS!

 Recommended Links
 o PHP Freaks
 o Designer Baby Clothes
 o Advertise Here Make Money
 o Wedding Vendor Directory
 o Free Wedding Websites
 o OxyScripts
 o Open Source Design
 o designplace
 o devnet

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


Rate This Script

User Contributed Comments

On 11-03-2003 18:06 Devscripts said:

I might add cookies to this later on.


On 07-10-2004 18:21 admin01 said:

How do you get his script to work? If you dont mind would i require this script if i dont want people to view my passwords in the source? If not please help me


Registered Members login
© Copyright 2003 - Devscripts.net