Tạo trang đăng nhập với PHP
Tạo CSS.
Style.css lưu trong thu mục Style:
@charset "utf-8";
/* CSS Document */
*{
margin:0px;
padding:0px;
}
body{
margin:auto;
width:800px;
}
h1{
font-size:36px;
}
Tạo cơ sở dữ liệu:
CREATE DATABASE ‘login’
Tạo bảng users:
CREATE TABLE `login`.`users` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`FullName` VARCHAR( 100 ) NOT NULL ,
`UserName` VARCHAR( 35 ) NOT NULL ,
`Password` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci
Tạo trang chủ index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Style/Style.css"/>
<title>cocolee.info</title>
</head>
<body>
<h1>Chào mừng các bạn đến với diễn đàn cocolee.info</h1>
<a href="Register.php">Đăng ký thành viên </a>|<a href="Login.php"> Đăng nhập hệ thống</a>
</body>
</html>
Tạo trang đăng ký: Register.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cocolee.info</title>
<link rel="stylesheet" type="text/css" href="Style/Style.css"/>
</head>
<body>
<h1>Chào mừng các bạn đến với diễn đàn cocolee.info</h1>
<form method="post" action="Register.php">
<label>Họ tên</label><input type="text" name="txtFullName" size="50" /><br />
<label>Tên đăng nhập</label><input type="text" name="txtUserName" /><br />
<label>Mật khẩu</label><input type="password" name="txtPassword" /><br />
<label> </label><input type="submit" name="btnRegister" value="Register" /><br />
</form>
</div>
<?php
//Kiem tra button Register
if(isset($_POST["btnRegister"]))
{
mysql_connect("localhost","root","")or die("Not connect host");
mysql_select_db("login")or die("Not select database");
if(!$_POST["txtFullName"] || !$_POST["txtUserName"] || !$_POST["txtPassword"])
{
echo "<p>Nhập đầy đủ thông tin</p>";
exit();
}
//mã hóa password
$_POST["txtPassword"]=md5($_POST["txtPassword"]);
$check=mysql_query("select UserName from users where UserName = '$_POST[txtUserName]'");
if(mysql_num_rows($check)==0)
{
$result=mysql_query("insert into users (FullName, UserName, Password) value('$_POST[txtFullName]','$_POST[txtUserName]','$_POST[txtPassword]')");
if($result)
{
echo "<p>Đăng ký thành công Quay về <a href=\"index.php\">Trang chủ</a></p>";
}
else
echo "<p>Lỗi đăng ký</p>";
}
else
{
echo "<p>Tên đăng nhập đã có người sử dụng</p>";
}
}
?>
</body>
</html>
Tạo trang đăng nhập: Login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Style/Style.css"/>
<title>cocolee.info</title>
</head>
<body>
<h1>Chào mừng các bạn đến với diễn đàn cocolee.info</h1>
<form method="post" action="Login.php">
<label>Tên đăng nhập</label><input type="text" name="txtUserName" /><br />
<label>Mật khẩu</label><input type="text" name="txtPassword" /><br />
<label> </label><input type="submit" name="btnLogin" value="Login" /><br />
</form>
<?php
if(isset($_POST["btnLogin"]))
{
mysql_connect("localhost","root","")or die("Not connect host");
mysql_select_db("login")or die("Not select database");
if(!$_POST["txtUserName"] || !$_POST["txtPassword"])
{
echo "<p>Nhập đầy đủ thông tin</p>";
exit();
}
$_POST["txtPassword"]=md5($_POST["txtPassword"]);
$result=mysql_query("select UserName from users where UserName = '$_POST[txtUserName]' and Password='$_POST[txtPassword]'");
if(mysql_num_rows($result)!=0)
{
setcookie("CookieUserName",$_POST["txtUserName"],time()+3600);
header("location:Member.php");
}
else
{
echo "<p>đăng nhập không thành công</p>";
}
}
?>
</body>
</html>
Tạo trang xem thành viên: Member.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Style/Style.css"/>
<title>cocolee.info</title>
</head>
<body>
<h1>Chào mừng các bạn đến với diễn đàn cocolee.info</h1>
<?php
echo "<p>Đăng nhập thành xin chào bạn: $_COOKIE[CookieUserName] <a href=\"Logout.php\">Thoát khỏi đăng nhập</a></p>";
mysql_connect("localhost","root","")or die("Not connect host");
mysql_select_db("login")or die("Not select database");
$result=mysql_query("select * from users where UserName='$_COOKIE[CookieUserName]'");
$row=mysql_fetch_assoc($result);
echo "<label>Họ và tên: </label>$row[FullName]<br />";
echo "<label>Tên đăng nhập: <label>$row[UserName]";
?>
</body>
</html>
Tạo trang thoát khỏi đăng nhập: Logout.php
<?php
setcookie ("CookieUserName", "", time() - 3600);
header("location:index.php");
?>