Ok, here is the code. Every user can have the same name and pass or seperate ones, its up to you. This does use a username and pass, but its really no extra work to use a username too, and it is more secure.
PHP Code:
<?php
// If form submitted
if ($_POST['login']) {
$user = $_POST['username'];
// Turn password into hash
$pass = md5($_POST['password']);
// Chech for user
$qry = mysql_query("SELECT * FROM `users` WHERE username = '$username'") or die ('Error Getting User! <br />' .mysql_error());
$u = mysql_fetch_array($qry);
$chk = mysql_num_rows($qry);
// See if useraname exists
if ($chk < 1) {
echo '<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'" />
<span style="color: #FF0000"><b>Invalid Username!</b></span>';
}
// Check password
elseif ($pass !== $u['password']) {
echo '<meta http-equiv="refresh" content="2;URL='.$_SERVER['PHP_SELF'].'" />
<span style="color: #FF0000"><b>Invalid Password!</b></span>';
}
// If username and pass match database, set sessions
elseif ($pass == $u['password']) {
// Set username, password, and ip sessions
@session_start();
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
echo '<meta http-equiv="refresh" content="2;URL=YOUR ADMIN HOME.php" />
<b>You are now logged in!</b>';
}
}
// If form not submitted
if (!$_POST['login']) {
?>
<!-- Login Form -->
<form method="post" action="">
<table width="232">
<tr>
<td width="75"><b>Username:</b></td>
<td width="145"><input type="text" name="username" /></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td>
</tr>
</table>
</form>
<!-- /Login Form -->
<?
}
You will have to add your database connection info for it to work. Also note that near the end, replace the "YOUR ADMIN HOME.php" with the filename of you control panel. You will need to add this table to your database:
Code:
CREATE TABLE `users` (
`id` int(5) NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
When you insert your users, be sure that when you set the password, it is using md5($password); because the password in the database must be stored as a hash for the submitted password to match it. Add this code to the top of your protected pages:
PHP Code:
<?php
if(empty($_SESSION['user']) || empty($_SESSION['pass']) || $_SESSION[['ip'] !== $_SERVER['REMOTE_ADDR']) {
header("Location: login.php");
}
?>
I didn't test it because I am on my way out for work, but it should work. If there are any issues, just let me know and I will check it out when I get the change. Hope this helps
Bookmarks