Bài viết này phù hợp với bạn mới bắt đầu lập trình PHP.Trong bài viết tôi muốn giải thích việc tạo ra một cơ sở dữ liệu(creating a database), gửi giá trị từ mẫu(posting form values), lưu trữ các giá trị phiên và phá hủy phiên(storing the session value and destroy the session).
Đầu tiên tôi sẽ lên danh sách các công việc cần được thực hiên.
- Created Table Account
- Tạo bảng Account trên phpAdmin. Bảng này được dùng để lưu thông tin tài khoản của người đăng nhập.
- Config.php
- Tạo trang lưu trữ thông tin cấu hình host, user, password của cơ sở dữ liệu chứa bảng Account.
- Login.php
- Tạo trang đang nhập, khi người dùng thực hiện việc đăng nhập sẽ được thực hiện tại trang này.
- Login.css
- Dùng để thiết kế giao diện nhập của trang Login.php.
- Welcome.php
- Tạo trang chào mừng(welcome) khi người dùng đăng nhập thành công.
- Logount.php
- Tạo trang thoát đăng nhập khi người dùng muốn đóng phiên đăng nhập.
Created Table Account:
create table account
(
id int not null auto_increment,
user varchar(100),
pass varchar(100),
status varchar(50),
primary key (id)
)
insert into account (user, pass, status) values ('admin', SHA('gem123#@!'), 'action')
insert into account (user, pass, status) values ('bnson', SHA('gem123#@!'), 'action')
(
id int not null auto_increment,
user varchar(100),
pass varchar(100),
status varchar(50),
primary key (id)
)
insert into account (user, pass, status) values ('admin', SHA('gem123#@!'), 'action')
insert into account (user, pass, status) values ('bnson', SHA('gem123#@!'), 'action')
Config.php
<?php
// Replace the variable values below with your specific database information.
$host = "Your host";
$user = "Username";
$pass = "Password";
$db = "Name Database";
// This part sets up the connection to the database (so you don't need to reopen the connection again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want is selected.
mysql_select_db($db);
?>
// Replace the variable values below with your specific database information.
$host = "Your host";
$user = "Username";
$pass = "Password";
$db = "Name Database";
// This part sets up the connection to the database (so you don't need to reopen the connection again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want is selected.
mysql_select_db($db);
?>
Login.php
<?php
include('Config.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = addslashes($_POST['tb_user']);
$password = addslashes($_POST['tb_pass']);
$sql = "SELECT id FROM account WHERE user='$username' and pass=SHA('$password')";
$rs = mysql_query($sql);
$count = mysql_num_rows($rs);
if($count == 1) {
session_start();
session_register('username');
$_SESSION['user_login'] = $username;
header("location: Welcome.php");
} else {
$error = "<hr /> Error:
- User or Pass is invalid!";
}
}
?>
<!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>Untitled Document</title>
<link href="Login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="div_login">
<form action="Login.php" method="post" enctype="application/x-www-form-urlencoded" name="login_form">
<div id="div_lg_header"><label>LOGIN</label></div>
<div id="div_lg_user"><label>User:</label><input name="tb_user" type="text" value="" maxlength="20" /></div>
<div id="div_lg_pass"><label>Pass:</label><input name="tb_pass" type="password" value="" maxlength="20" /></div>
<div id="div_lg_control"><a href="" target="_blank" style="font-size:14px">Register</a> <input name="bt_login" type="submit" value="Login" /></div>
</form>
<div id="div_msgError"><?php echo $error; ?></div>
</div>
</body>
</html>
include('Config.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = addslashes($_POST['tb_user']);
$password = addslashes($_POST['tb_pass']);
$sql = "SELECT id FROM account WHERE user='$username' and pass=SHA('$password')";
$rs = mysql_query($sql);
$count = mysql_num_rows($rs);
if($count == 1) {
session_start();
session_register('username');
$_SESSION['user_login'] = $username;
header("location: Welcome.php");
} else {
$error = "<hr /> Error:
- User or Pass is invalid!";
}
}
?>
<!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>Untitled Document</title>
<link href="Login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="div_login">
<form action="Login.php" method="post" enctype="application/x-www-form-urlencoded" name="login_form">
<div id="div_lg_header"><label>LOGIN</label></div>
<div id="div_lg_user"><label>User:</label><input name="tb_user" type="text" value="" maxlength="20" /></div>
<div id="div_lg_pass"><label>Pass:</label><input name="tb_pass" type="password" value="" maxlength="20" /></div>
<div id="div_lg_control"><a href="" target="_blank" style="font-size:14px">Register</a> <input name="bt_login" type="submit" value="Login" /></div>
</form>
<div id="div_msgError"><?php echo $error; ?></div>
</div>
</body>
</html>
Login.css
body {
font-family: Arial, Helvetica, sans-serif;
text-align: left;
}
#div_login {
border: 1px solid #333;
float: left;
}
#div_lg_header {
padding: 2px;
text-align: center;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
font-weight: bold;
color: #FFF;
background-color: #666;
}
#div_lg_user {
font-family: "Courier New", Courier, monospace;
margin: 5px;
word-spacing: 10em;
text-align: center;
}
#div_lg_pass {
margin: 5px;
font-family: "Courier New", Courier, monospace;
text-align: center;
}
#div_lg_control {
margin: 5px;
text-align: right;
font-family: Verdana, Geneva, sans-serif;
vertical-align: middle;
}
#div_msgError {
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
color: #F00;
text-align: left;
margin: 5px;
}
font-family: Arial, Helvetica, sans-serif;
text-align: left;
}
#div_login {
border: 1px solid #333;
float: left;
}
#div_lg_header {
padding: 2px;
text-align: center;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
font-weight: bold;
color: #FFF;
background-color: #666;
}
#div_lg_user {
font-family: "Courier New", Courier, monospace;
margin: 5px;
word-spacing: 10em;
text-align: center;
}
#div_lg_pass {
margin: 5px;
font-family: "Courier New", Courier, monospace;
text-align: center;
}
#div_lg_control {
margin: 5px;
text-align: right;
font-family: Verdana, Geneva, sans-serif;
vertical-align: middle;
}
#div_msgError {
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
color: #F00;
text-align: left;
margin: 5px;
}
Welcome.php
<?php
$msg = "";
session_start(); //Start the session
$user = $_SESSION['user_login'];
if($user == "") {
$msg = "You must login!";
} else {
$msg = "^^! Wellcome " . $user . " to my stie.
<a href=\"Logout.php\">Loutout</a>";
}
?>
<!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>Untitled Document</title>
</head>
<body>
<?php echo $msg; ?>
</body>
</html>
$msg = "";
session_start(); //Start the session
$user = $_SESSION['user_login'];
if($user == "") {
$msg = "You must login!";
} else {
$msg = "^^! Wellcome " . $user . " to my stie.
<a href=\"Logout.php\">Loutout</a>";
}
?>
<!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>Untitled Document</title>
</head>
<body>
<?php echo $msg; ?>
</body>
</html>
Logount.php
<?php
session_start(); //Start the current session
session_destroy(); //Destroy it! So we are logged out now
header("location:Login.php"); // Move back to login.php with a logout message
?>
<!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>Untitled Document</title>
</head>
<body>
You has logout site.
</body>
</html>
session_start(); //Start the current session
session_destroy(); //Destroy it! So we are logged out now
header("location:Login.php"); // Move back to login.php with a logout message
?>
<!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>Untitled Document</title>
</head>
<body>
You has logout site.
</body>
</html>
No comments:
Post a Comment