Let's do this but using HTML, PHP and MySQL. The improvements that you could make is that you create pages to help you manage your database which means you would have a registration page, a forgotten password page and a login page and whatever else you think you would require, concept behind it is a full feature membership login procedure. But for this I will only show you the basics and may provide the complete package in the HOWTO and TUTORIAL section.
So lets begin with setting up our database, hopefully you have a database already created, our one will be named
MyDB for this example.
Next we will pass some MySQL query inside phpmyadmin to create the table and entries we require. Since it's only for a simple login, what would we require? We need the username and password. We should have some security features behind this, but for this simple login, we won't be needing that.
CREATE TABLE users (
userid int(25) NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
passwd varchar(255) NOT NULL default '',
PRIMARY KEY (userid),
UNIQUE KEY (username)
) TYPE = MyISAM COMMENT = 'MyDB Users';
That create our table we will use to store our username/password,
We then create our first user, using
phpmyadmin once again to execute this query.
INSERT INTO `users` (`userid`, `username`, `passwd`)
VALUES (
'', 'myUserName', PASSWORD('myPassWord')
);What this does is add a user named
myUserName and a password that is
myPassWord as well as doing the auto_increment used for userid, which should get set to 1.
Next we write our connection to database file, this is the user that has permissions on the database to be able to access the required information we need, this file we will include in our login script to connect to our database and perform the required tasks. We will call this
dbcon.inc.php<?php
$dbhost = 'localhost';
$dbusername = 'MyDB_username';
$dbpassword = 'MyDB_password';
$database = 'MyDB';
$connection = mysql_connect("$dbhost", "$dbusername", "$dbpassword") or die('Error: Connection to Server failed');
$db = mysql_select_db("$database", $connection) or die('Error: Database selection failed');
?>We then create our simple
login.html form, not set up nicely, but I'll leave that up to you.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-NZ" lang="en-NZ">
<head>
<title>
Login Page
</title>
</head>
<body>
<form action="/php-bin/login.php" method="post" id="loginform">
<table summary="login information">
<tr>
<td>Username</td>
<td><input id="username" type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input id="password" type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id="submit" value="Submit" name="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Now all we need to do is create our
login.php script
<?php
include '../includes/db.inc.php';
$username = $_POST['username'];
$password = $_POST['password'];
if((!$username) || (!$password))
exit();
$sql = mysql_query("SELECT * FROM users WHERE userid = '1' AND username = '$username' AND passwd = PASSWORD('$password')");
$login_check = mysql_num_rows($sql);
if($login_check > 0)
{
echo '<' . '?xml version=1.0" encoding="iso-8859-1"?' . '>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-NZ" lang="en-NZ">
<head>
<title>
Secret Page
</title>
</head>
<body>
<p>blah blah blah blah blah SECRET PAGE blah blah blah</p>
</body>
</html>
<?php
}
else
echo "Your login was invalid, Either username or password were incorrect<br />";
?>And that's it, You just have to make sure that you have a folder called includes (new folder created) for all your included files to go and that the path is correct, that the login.php exists in your php-bin (new folder created). There have been issues with using the word password in MySQL as I believe it's a reserved word, so you might want to change from using password to passwd as I have done in MySQL.
To login you would use myUserName and myPassWord.
This has been tested and verified that it works.Cheers, MC