==========================
Due to a lot of problem reported by friends about these script writing, so here I put the complete scripts. You can grab it here:
http://rapidshare.com/files/195043222/login-frozenade.co.nr.zipThank you for concern.
==========================
The production of login page using PHP and MySQL is actually very simple.
I assume that you use local PHP connection and your MySQL database configuration use ‘localhost’ as hostname and ‘root’ as user with blank password.
First, let’s create a database:
create database dblogin;
Then use our database we’ve just created with command:
use dblogin;
Now, let’s create a table:
create table tbuser
(username varchar(50),
password varchar(255))
After that, fill the table with username ‘admin’ and password ‘adminpass’.
insert into tbuser values ('admin',md5('adminpass'));
Now, let’s prepare the login page.
<html>
<head>
<title>login page</title>
</head>
<body>
<form method="POST" action="loginproc.php">
<p>username: <input type="text" name="username" size="20"></p>
<p>password: <input type="text" name="password" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
Save above document with name ‘loginpage.php’.
Now, prepare a file and give it name ‘loginproc.php’ to check the validity of username and password.
<?
session_start();
$server = "localhost";
$username = "root";
$password = "";
$db_name = "dblogin";
$db = mysql_connect($server,$username,$password) or DIE("Connection to database failed, perhaps the service is down !!");
mysql_select_db($db_name) or DIE("Database name not available !!");
$login = mysql_query("select * from tbuser where (username = '" . $_POST['username'] . "') and (password = '" . md5($_POST['password']) . "')",$db);
$rowcount = mysql_num_rows($login);
if ($rowcount == 1) {
$_SESSION['username'] = $_POST['username'];
header("Location: securedpage.php");
}
else
{
header("Location: loginpage.php");
}
?>
As you see above, there is ’session_start();’.
This function is used to initializes a data session. It will creates a new session or continues previous session from data session change by GET, POST or cookie.
See the detail information about session here:
http://id.php.net/function.session-startIf username and password are correct, then we’ll be directed to ’securedpage.php’.
This is a page that we want to show if login is successful. This page cannot be accessed if there is not found the correct data session when login is successfully passed. This page also contains ‘logout’ menu, so we can destroy our login data session then return to login page.
This is content of ’securedpage.php’:
<?
session_start();
if (!isset($_SESSION['username'])) {
header("Location: loginpage.php");
}
?>
<html>
<head>
<title>secured page</title>
</head>
<body>
<p>secured page with session <? echo $_SESSION['username']; ?></p>
<p><a href="logoutpage.php">logout</a></p>
</body>
</html>
Script for ‘logout.php’:
<? session_start();
unset($_SESSION['username']);
session_destroy();
header("Location: loginpage.php");
?>
Store your files you have created in one location. e.g: ‘logintest’.
So, the login page could be
http://localhost/logintest/loginpage.phpSo easy, isn’t it?
==========================
Due to a lot of problem reported by friends about these script writing, so here I put the complete scripts. You can grab it here:
http://rapidshare.com/files/195043222/login-frozenade.co.nr.zipThank you for concern.
==========================