I put this together a while ago maybe it will be of use:
Login.php
PHP Code:
<?php
// Initialize Session
session_start();
header ("Cache-control: private") ; //IE 6 Fix
// Check for previous authentication
if ( isset($_SESSION['auth']) )
{
$loc = 'index.php' ;
header("location:$loc");
exit;
}
// Error Messages
$msg = array();
$msg['login'] = 'Login to access client section';
$msg['invalid'] = 'Username/password incorrect';
$msg['logout'] = 'Log out successful';
// Check for error
if ( isset($_GET['reason']) && !empty($_GET['reason']) )
{
$error = $_GET['reason'];
echo '<font color="#FF0000"><b>' . $msg[$error] . '</b></font><br />';
}
?>
<FORM method="post" action="process_login.php" onSubmit="return validateForm(this)">
<p align="center">
<p>Please enter your Username and Password below and Login.</p>
<p>Username:
<input type="text" name="username" value="" class="inputbox">
<br>
<br>
Password:
<input type="password" name="password" value="" class="inputbox">
<p class="copy">
<input type="submit" name="do_login" value="Login" class= "button">
</p>
</form>
Process_login.php:
PHP Code:
<?php
// Initialize Session
session_start();
header ("Cache-control: private") ; //IE 6 Fix
// Check for previous authentication
if ( isset($_SESSION['auth']) )
{
$loc = 'index.php' ;
header("location:$loc");
exit;
}
// Database Connection
$dbh = @mysql_connect('localhost','','');
if ( !$dbh )
{
die ('Database Error - Connect');
}
@mysql_select_db('your_database',$dbh);
// Get Form Contents
if ( isset($_POST['do_login']) )
{
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password' LIMIT 0,1";
$result = @mysql_query($sql);
if ( !$result )
{
die('Database Error - Query');
}
if ( mysql_num_rows($result) == 1 )
{
$_SESSION['auth'] = 1;
$_SESSION['username'] = $username;
$loc = 'index.php';
header("location:$loc");
exit;
}
else
{
$loc = 'login.php?reason=invalid';
header("location:$loc");
exit;
}
}
else
{
$error_loc = 'login.php?reason=login';
header("location:$error_loc");
exit;
}
?>
Index.php:
PHP Code:
<?php //<-- Place this at line 1 of page you want to protect
// Initialize Session
session_start();
header ("Cache-control: private") ; //IE 6 Fix
// Check for previous authentication
if ( !isset($_SESSION['auth']) )
{
$loc = 'login.php?reason=login';
header("location:$loc");
exit;
}
?>
Logout.php:
PHP Code:
<?php
// Initialize Session
session_start();
// Destroy Session
$_SESSION = array();
session_destroy();
// Redirect to login.php
$loc = 'login.php?reason=logout';
header("location:$loc");
exit;
?>
Datapage.php
PHP Code:
<?php //<-- Place this at line 1 of page you want to protect
// Initialize Session
session_start();
header ("Cache-control: private") ; //IE 6 Fix
// Check for previous authentication
if ( !isset($_SESSION['auth']) )
{
$loc = 'login.php?reason=login';
header("location:$loc");
exit;
}
?>
<?php
// connect to database
$dbcnx = @mysql_connect('localhost', '', '');
if (!$dbcnx) {
echo( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
exit();
}
if (! @mysql_select_db('your_database') ) {
die( '<p>Unable to locate the ' .
'database at this time.</p>' );
}
?>
<?php
$result = mysql_query("SELECT * FROM tablename
WHERE username= '". $_SESSION['username']."'") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
print $row['fieldname'];
}
?>
Colin
Bookmarks