Here you go. (you just need to make the register.php file)
[File: users.sql]
Code:
CREATE TABLE users (
uid INTEGER AUTO_INCREMENT NOT NULL,
username VARCHAR(15) NOT NULL,
password VARCHAR(15) NOT NULL,
KEY users_key (uid)
);
[File: login.php]
PHP Code:
<?php
// Initialize Session
session_start();
// Check for previous authentication
if ( isset($_SESSION['auth']) )
{
$loc = 'members.php?PHPSESSID' . session_id();
header("location:$loc");
exit;
}
// Error Messages
$msg = array();
$msg['login'] = 'You have to login before you can access member content';
$msg['invalid'] = 'Username/password does not match';
$msg['logout'] = 'You have been successfully logged out';
// Check for error
if ( isset($_GET['reason']) && in_array($_GET['reason'],$msg) )
{
$el = $_GET['reason'];
echo '<font color="#FF0000"><b>' . $msg[$el] . '</b></font><br />';
}
?>
<form action="process_login.php" method="post">
Username <input type="text" name="username" value="" /><br />
Password <input type="password" name="password" value="" /><br />
<input type="submit" name="do_login" value="Login" />
</form>
[File: process_login.php]
PHP Code:
<?php
// Initialize Session
session_start();
// Check for previous authentication
if ( isset($_SESSION['auth']) )
{
$loc = 'members.php?PHPSESSID=' . session_id();
header("location:$loc");
exit;
}
// Database Connection
$dbh = @mysql_connect('your_server','your_username','your_password');
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 = 'members.php?PHPSESSID=' . session_id();
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;
}
[File: members.php]
PHP Code:
<?php
// Initialize Session
session_start();
// Check for previous authentication
if ( !isset($_SESSION['auth']) )
{
$loc = 'login.php?reason=login';
header("location:$loc");
exit;
}
echo 'Welcome ' . $_SESSION['username'] . '<br />';
echo '<a href="logout.php">Logout</a>';
?>
[File: 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;
?>
that has some added stuff, but i was bored. it should work good, if it doesnt, sorry, i'm tired.
Bookmarks