#
# โครงสร้างตาราง `tb_member`
#
CREATE TABLE `tb_member` (
`id_member` int(11) NOT NULL auto_increment,
`user_log` varchar(15) NOT NULL default '',
`pass_log` varchar(15) NOT NULL default '',
PRIMARY KEY (`id_member`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
#
# dump ตาราง `tb_member`
#
INSERT INTO `tb_member` VALUES (1, 'admin', '12345');
2.หลังจากนั้นให้เราสร้างฟอร์ม Login ในที่นี้ผมใช้ชื่อไฟล์ว่า form_login.php

โค๊ตของไฟล์ form_login.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>webthaidd.com</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="check.php">
<table width="300" border="0" cellspacing="5" cellpadding="0">
<tr>
<td>Username : </td>
<td><input name="username_log" type="text" id="username_log" /></td> //ชื่อตัวแปลที่ใช้ในการส่งค่า
</tr>
<tr>
<td>Password: </td>
<td><input name="password_log" type="password" id="password_log" /></td> //ชื่อตัวแปลที่ใช้ในการส่งค่า
</tr>
<tr>
<td>Login ตลอดไป : </td>
<td><input name="chk" type="checkbox" id="chk" value="on" /></td> //ชื่อตัวแปลที่ใช้ในการส่งค่า
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
<?
ob_start();
$host = "localhost"; //ชื่อโฮส
$db_username = ""; //ชื่อผู้ใช้
$db_password = ""; //รหัสผ่าน
$dbname = "test"; //ชื่อฐานข้อมูล
//เริ่มติดต่อฐานข้อมูล
mysql_connect($host, $db_username, $db_password) or die("ติดต่อฐานข้อมูลไม่ได้");
// เลือกฐานข้อมูล
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้");
// คำสั่ง SQL และสั่งให้ทำงาน
$sql = "select * from tb_member where user_log='$username_log' and pass_log='$password_log'"; //เช็คค่าข้อมูลที่ส่งมาจากฟอร์ม
$dbquery = mysql_db_query($dbname, $sql);
// หาจำนวนเรกคอร์ดข้อมูล
$num_rows = mysql_num_rows($dbquery);
if($num_rows == 1) {
if($chk == "on") { // ถ้าติ๊กถูก Login ตลอดไป ให้ทำการสร้าง cookie
setcookie("username_log",$username_log,time()+3600*24*356);
setcookie("password_log",$password_log,time()+3600*24*356);
header("location:http://www.webthaidd.com"); //ไปไปตามหน้าที่คุณต้องการ
} else {
header("location:http://www.webthaidd.com"); //ไปไปตามหน้าที่คุณต้องการ
}
} else {
header("location: form_login.php"); //ไม่ถูกต้องให้กับไปหน้าเดิม
}
?>
| ผู้จัดทำ : Zerohate ที่อยู่อีเมลล์ : zerohate@hotmail.com website : http://www.zerohatemusic.com |