More Related Content More from Bounsong Byv (6) ສ້າງລະບົບ Loin ດ້ວຍ php1. ສ້າງລະບົບ PHP
ເລີ່ ມຕົ້ນດ້ວຍການສ້າງຕາຕະລາງຊີ່ member
CREATE TABLE `member` (
`UserID` int(3) unsigned zerofill NOT NULL auto_increment,
`Username` varchar(20) NOT NULL,
`Password` varchar(20) NOT NULL,
`Name` varchar(100) NOT NULL,
`Status` enum('ADMIN','USER') NOT NULL default 'USER',
PRIMARY KEY (`UserID`),
UNIQUE KEY `Username` (`Username`)
) ENGINE=MyISAM AUTO_INCREMENT=3 ;
--
-- Dumping data for table `member`
--
INSERT INTO `member` VALUES (001, 'win', 'win123', 'Weerachai Nukitram',
'USER');
INSERT INTO `member` VALUES (002, 'chai', 'chai123', 'Surachai Sirisart',
'ADMIN');
ກີ່ ຽວກັບຕາຕະລາງຈະປະກອບດ້ວຍ UserID , Username , Password , Name , Status ໂດຍຊີ່ ອງ
Status ເປັນໂຕບົງບອກວີ່ າເປັນ ADMIN ຫວີ່ າ User
ຕົວຢີ່ າງ Code ທັງໝົດ
login.php ເປັນ Form Login
view source
2. print?
01.<html>
02.<head>
03.<title>ThaiCreate.Com Tutorials</title>
04.</head>
05.<body>
06.<form name="form1" method="post" action="check_login.php">
07. Login<br>
08. <table border="1" style="width: 300px">
09. <tbody>
10. <tr>
11. <td> Username</td>
12. <td>
13. <input name="txtUsername" type="text" id="txtUsername">
14. </td>
15. </tr>
16. <tr>
17. <td> Password</td>
18. <td><input name="txtPassword" type="password" id="txtPassword">
19. </td>
20. </tr>
21. </tbody>
22. </table>
23. <br>
24. <input type="submit" name="Submit" value="Login">
25.</form>
26.</body>
27.</html>
check_login.php ເປັນ Check Login
view source
print?
01.<?php
3. 02. session_start();
03. mysql_connect("localhost","root","root");
04. mysql_select_db("mydatabase");
05. $strSQL = "SELECT * FROM member WHERE Username =
'".mysql_real_escape_string($_POST['txtUsername'])."'
06. and Password = '".mysql_real_escape_string($_POST['txtPassword'])."'";
07. $objQuery = mysql_query($strSQL);
08. $objResult = mysql_fetch_array($objQuery);
09. if(!$objResult)
10. {
11. echo "Username and Password Incorrect!";
12. }
13. else
14. {
15. $_SESSION["UserID"] = $objResult["UserID"];
16. $_SESSION["Status"] = $objResult["Status"];
17.
18. session_write_close();
19.
20. if($objResult["Status"] == "ADMIN")
21. {
22. header("location:admin_page.php");
23. }
24. else
25. {
26. header("location:user_page.php");
27. }
28. }
29. mysql_close();
30.?>
4. ໄຟລ໌ນ້ຈະເຮັດການກວດສອບ Username ແລະ Password ແລະສະຖານະຂອງສະມາຊິກວີ່ າເປັນ Admin
User Admin ະ admin_page.php ແລະ User ະ
user_page.php
admin_page.php Admin
view source
print?
01.<?php
02. session_start();
03. if($_SESSION['UserID'] == "")
04. {
05. echo "Please Login!";
06. exit();
07. }
08.
09. if($_SESSION['Status'] != "ADMIN")
10. {
11. echo "This page for Admin only!";
12. exit();
13. }
14.
15. mysql_connect("localhost","root","root");
16. mysql_select_db("mydatabase");
5. 17. $strSQL = "SELECT * FROM member WHERE UserID =
'".$_SESSION['UserID']."' ";
18. $objQuery = mysql_query($strSQL);
19. $objResult = mysql_fetch_array($objQuery);
20.?>
21.<html>
22.<head>
23.<title>ThaiCreate.Com Tutorials</title>
24.</head>
25.<body>
26. Welcome to Admin Page! <br>
27. <table border="1" style="width: 300px">
28. <tbody>
29. <tr>
30. <td width="87"> Username</td>
31. <td width="197"><?php echo $objResult["Username"];?>
32. </td>
33. </tr>
34. <tr>
35. <td> Name</td>
36. <td><?php echo $objResult["Name"];?></td>
37. </tr>
38. </tbody>
39. </table>
40. <br>
41. <a href="edit_profile.php">Edit</a><br>
42. <br>
43. <a href="logout.php">Logout</a>
44.</body>
45.</html>
6. ໜ້າຫັກຂອງ Admin ຈະມການກວດສອບ Session ວີ່ າເປັນການ Login ມາຫບໍ ແລະ ກວດສອບສະຖານະ
ນໍາວີ່ າເປັນ Admin
user_page.php User
view source
print?
01.<?php
02. session_start();
03. if($_SESSION['UserID'] == "")
04. {
05. echo "Please Login!";
06. exit();
07. }
08.
09. if($_SESSION['Status'] != "USER")
10. {
11. echo "This page for User only!";
12. exit();
13. }
14.
15. mysql_connect("localhost","root","root");
7. 16. mysql_select_db("mydatabase");
17. $strSQL = "SELECT * FROM member WHERE UserID =
'".$_SESSION['UserID']."' ";
18. $objQuery = mysql_query($strSQL);
19. $objResult = mysql_fetch_array($objQuery);
20.?>
21.<html>
22.<head>
23.<title>ThaiCreate.Com Tutorials</title>
24.</head>
25.<body>
26. Welcome to User Page! <br>
27. <table border="1" style="width: 300px">
28. <tbody>
29. <tr>
30. <td width="87"> Username</td>
31. <td width="197"><?php echo $objResult["Username"];?>
32. </td>
33. </tr>
34. <tr>
35. <td> Name</td>
36. <td><?php echo $objResult["Name"];?></td>
37. </tr>
38. </tbody>
39. </table>
40. <br>
41. <a href="edit_profile.php">Edit</a><br>
42. <br>
43. <a href="logout.php">Logout</a>
44.</body>
45.</html>
8. User ະ Session Login ແລະ
ະ ະ User
edit_profile.php ເປັນໜ້າສໍາຫັບແກ້ໄຂຂໍ້ມູນສີ່ ວນຕົວຂອງ User ແລະ Admin
view source
print?
01.<?php
02. session_start();
03. if($_SESSION['UserID'] == "")
04. {
05. echo "Please Login!";
06. exit();
07. }
08.
09. mysql_connect("localhost","root","root");
10. mysql_select_db("mydatabase");
11. $strSQL = "SELECT * FROM member WHERE UserID =
'".$_SESSION['UserID']."' ";
12. $objQuery = mysql_query($strSQL);
13. $objResult = mysql_fetch_array($objQuery);
14.?>
15.<html>
9. 16.<head>
17.<title>ThaiCreate.Com Tutorials</title>
18.<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
19.<body>
20.<form name="form1" method="post" action="save_profile.php">
21. Edit Profile! <br>
22. <table width="400" border="1" style="width: 400px">
23. <tbody>
24. <tr>
25. <td width="125"> UserID</td>
26. <td width="180">
27. <?php echo $objResult["UserID"];?>
28. </td>
29. </tr>
30. <tr>
31. <td> Username</td>
32. <td>
33. <?php echo $objResult["Username"];?>
34. </td>
35. </tr>
36. <tr>
37. <td> Password</td>
38. <td><input name="txtPassword" type="password" id="txtPassword"
value="<?php echo $objResult["Password"];?>">
39. </td>
40. </tr>
41. <tr>
42. <td> Confirm Password</td>
43. <td><input name="txtConPassword" type="password" id="txtConPassword"
value="<?php echo $objResult["Password"];?>">
44. </td>
45. </tr>
46. <tr>
47. <td> Name</td>
48. <td><input name="txtName" type="text" id="txtName" value="<?php echo
$objResult["Name"];?>"></td>
10. 49. </tr>
50. <tr>
51. <td> Status</td>
52. <td>
53. <?php echo $objResult["Status"];?>
54. </td>
55. </tr>
56. </tbody>
57. </table>
58. <br>
59. <input type="submit" name="Submit" value="Save">
60.</form>
61.</body>
62.</html>
11. save_profile.php ເປັນໜ້າສໍາຫັບບັນທຶກແກ້ໄຂຂໍ້ມູນສີ່ ວນຕົວຂອງ User ແລະ Admin
view source
print?
01.<?php
02. session_start();
03. if($_SESSION['UserID'] == "")
04. {
05. echo "Please Login!";
06. exit();
07. }
08. mysql_connect("localhost","root","root");
09. mysql_select_db("mydatabase");
10.
11. if($_POST["txtPassword"] != $_POST["txtConPassword"])
12. {
13. echo "Password not Match!";
14. exit();
15. }
16. $strSQL = "UPDATE member SET Password = '".trim($_POST['txtPassword'])."'
17. ,Name = '".trim($_POST['txtName'])."' WHERE UserID =
'".$_SESSION["UserID"]."' ";
18. $objQuery = mysql_query($strSQL);
19.
20. echo "Save Completed!<br>";
21.
22. if($_SESSION["Status"] == "ADMIN")
23. {
24. echo "<br> Go to <a href='admin_page.php'>Admin page</a>";
25. }
26. else
27. {
28. echo "<br> Go to <a href='user_page.php'>User page</a>";
29. }
30.
31. mysql_close();