|
|
|
อยากจะสอบถามเรื่อง MySQL prepared statement [Login] |
|
|
|
|
|
|
|
Code (PHP)
<?php session_start();
if(!isset($_SESSION['userid'])){
header('location: /Login.php');
}
|
|
|
|
|
Date :
2018-10-22 20:14:26 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แล้วในส่วนของ $_SESSION['UserID'] ในไฟล์ข้างบนดังกล่าว ต้องใส่ = อะไรอะครับ
|
|
|
|
|
Date :
2018-10-22 21:06:30 |
By :
AloneSpace |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากจะใส่อะไรเข้าไปก็ได้ ก็อยู่ที่ว่าคุณจะเอาไปทำประโยชน์อะไรต่อหรือเปล่า
แต่ตอนนี้เป็น string ความยาว 0 ก็ถือว่ามันเป็นตัวแปรที่มีการกำหนดค่า
ใช้ คำสั่ง isset ก็สามารถตรวจสอบได้ว่า ได้ login หรือยัง
ส่วนการ logout ก็ให้ทำการ unset ตัวแปรนี้ออก ก็จะถือว่า ได้ออกจากโปรแกรมแล้ว
|
|
|
|
|
Date :
2018-10-23 13:42:16 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?
session_start();
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$username = strtolower($_POST['username']);
$password = $_POST['password'];
$sep = '$';
$sha = 'SHA';
$salt = substr(str_shuffle($chars),0,16);
$hash = hash('sha256', hash('sha256', $password) . $salt);
$outpass = $sep.$sha.$sep.$salt.$sep.$hash;
$con = new mysqli("localhost","root","XXX","authme");
$check_login = "SELECT username, password FROM authme WHERE username=? and password=? LIMIT 1";
$stmt_check_login = $con->prepare($check_login);
$stmt_check_login->bind_param('ss',$username,$outpass);
$stmt_check_login->execute();
$stmt_check_login->bind_result($username,$outpass);
$stmt_check_login->store_result();
if($stmt_check_login->num_rows == 1)
{
$_SESSION['Username'] = $username;
header("Location: home.php");
exit();
} else {
header("Location: index.html");
exit();
}
$stmt_check_login->close();
$con->close();
?>
พอหน้า login ไม่ว่าจะใส่รหัสถูกหรือผิด จะเด้งเข้าหน้า index.html ตลอดเลยครับ
|
|
|
|
|
Date :
2018-10-23 19:12:21 |
By :
AloneSpace |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่ยนคำสั่งดูหน่อย
Code (PHP)
$stmt_check_login->execute();
$rs = $stmt_check_login->get_result();
if($rs->num_rows==1){
$ro=$rs->fetch_object();
$_SESSION['Username'] = $ro->username;
header("Location: home.php");
exit();
} else {
header("Location: index.html");
exit();
}
|
|
|
|
|
Date :
2018-10-23 19:54:02 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|