|
 |
|
อยากทราบ โค้ด ถ้าทำการล็อกอินแล้ว ปุ่มเข้าสู่ระบบหาย ขึ้นยินดีต้อนรับ และแสดงชื่อ แทน |
|
 |
|
|
 |
 |
|
ตอนทำการ Login ก็ให้สร้าง SESSION["user"] เก็บไว้น่ะครับ
แล้วส่วนหน้าจอแสดงที่คุณต้องการก็เขียน code ว่า
if(isset($SESSION["user"])){
==> แสดงส่วน ยินดีต้อนรับ และแสดงชื่อ แทน
}else{
==> เข้าสู่ระบบ
}
Code (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>Login Form</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="login.php">
<table width="510" border="0" align="center">
<tr>
<td colspan="2">Login Form</td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Code (PHP)
<?php
ob_start();
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('login', $conn);
$username = mysql_real_escape_string($username);
$query = "SELECT id, username, password, salt
FROM member
WHERE username = '$username';";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) // User not found. So, redirect to login_form again.
{
header('Location: login.html');
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again.
{
header('Location: login.html');
}else{ // Redirect to home page after successful login.
session_regenerate_id();
$_SESSION['sess_user_id'] = $userData['id'];
$_SESSION['sess_username'] = $userData['username'];
session_write_close();
header('Location: home.php');
}
?>
|
 |
 |
 |
 |
Date :
2014-10-07 10:23:50 |
By :
nut_ch31 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|