ผมเขียนโปรแกรมให้มันล็อกอิน แล้วถ้าเราใส่ id ถูกมันจะขึ้นโชวรูปถูก ถ้าผิดมันจะกากบาทครับ โดยดึงข้อมูลมาจากฐานข้อมูลตานี้พอใส่ข้อมูลถูกแล้วมันไม่ login เข้าหน้าใหม่ให้ครับ
ผมเขียนโปรแกรมให้มันล็อกอิน แล้วถ้าเราใส่ id ถูกมันจะขึ้นโชวรูปถูก ถ้าผิดมันจะกากบาทครับ โดยดึงข้อมูลมาจากฐานข้อมูล
ตานี้พอใส่ข้อมูลถูกแล้วมันไม่ login เข้าหน้าใหม่ให้ครับ
ช่วยดูโค๊ดให้หน่อยครับ
index.php(PHP)
<form action="index_code.php" name="frmMain" method="post" target="iframe_target">
<iframe id="iframe_target" name="iframe_target" style="width:0;height:0;border:0px solid #fff;" src="#"></iframe>
<script language="JavaScript">/*ulog*/
var HttPRequest = false;
function doCallAjax() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'index_code.php';
var pmeters = "ulog=" + encodeURI( document.getElementById("ulog").value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "..";
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'ch.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
}
</script>
<div id="div_ulog">
<div id="div_u"><input name="ulog" type="text" id="ulog" placeholder="ชื่อผู้ใช้งาน" maxlength="20" OnChange="JavaScript:doCallAjax();"></div>
<div id="mySpan"></div>
<div id="div_p">
<input name="plog" type="password" id="plog" placeholder="รหัสผ่าน" maxlength="14">
</div>
<div id="div_s"><input id="slog" name="slog" type="submit" value="เข้าสู่ระบบ" ></div>
</div>
</form>
index_code.php (PHP)
<?php
session_start();
@ini_set('display_errors', '0');
include('connect.php');
$strulog = trim($_POST["ulog"]);
$strplog = trim($_POST["plog"]);
if(trim($strulog) == "")
{
echo "<img src='img/x.gif'>";
exit();
}
if(isset($_POST['ulog'])){
$sql1 = mysql_query('SELECT * FROM member WHERE u_ser = "'.$strulog .'"');
$result1 = mysql_fetch_array($sql1);
$num1 = mysql_num_rows($sql1);
if($num1<=0){
echo "<img src='img/x.gif'>";
}else{
echo "<img src='img/y.gif'>";
$sql2 = mysql_query('SELECT * FROM member WHERE u_ser = "'.$strulog .'" and p_pass = "'.$strplog .'"');
$result2 = mysql_fetch_array($sql2);
$num2 = mysql_num_rows($sql2);
if($num2<=0){
}else{
if($result1['u_type']== 1){
$_SESSION['user'] = $result1['u_ser'];
$_SESSION['type_id'] = '1';
$_SESSION['mem_id'] = $result1['i_user'];
header("location:ch.php");
}
else{
$_SESSION['user'] = $result1['u_ser'];
$_SESSION['type_id'] = '0';
$_SESSION['mem_id'] = $result1['i_user'];
header("location:ch.php");
}
}
}
}
mysql_close($conn);
?>
Tag : PHP, MySQL, HTML/CSS, Ajax
Date :
2016-05-23 14:59:37
By :
slaparum
View :
698
Reply :
4
ไม่เห็นต้องใช้ ajax เลย ปกติเขาใช้แค่ bootstrap + flash session แค่นั้นเอง
เช่น เวลา validate ไม่เข้าก็ส่ง flash session มาเปลี่ยน css ของ input ให้เป็นสีแดง + error message
Code (PHP)
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-sign-in"></i>Login
</button>
<a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
</div>
</div>
</form>
ปล. comment ล้มกระดานอีกแระ
Date :
2016-05-23 15:20:09
By :
ห้ามตอบเกินวันละ 2 กระทู้
ผมอยากให้มันเป็นแบบนี้อะครับ เลยทำแบบนั้นออกมา
อ้างอิงจาก ครับ
https://www.thaicreate.com/tutorial/ajax-check-username.html
ประวัติการแก้ไข 2016-05-23 15:38:53
Date :
2016-05-23 15:32:52
By :
slaparum
ขอแนะนำว่าการตรวจสอบ username และ password ไม่ควรแม้แต่จะแจ้งว่า username ผิด หรือแค่ password ผิด ไม่ควรบอกอะไรเลย ควรบอกแค่ว่า username หรือ password ไม่ถูกต้อง
ให้คนกรอกเดาเอาเองว่าอะไรกันแน่ที่ผิด เพราะเพื่อปกป้อง account ของเขาเอง
ถ้าให้ใครก็ได้รู้ว่า username คืออะไร ที่เหลือก็แค่เดา password แล้วถ้าผู้ใช้นั้นตั้งรหัสผ่านโง่ก็จบง่ายเลย
หรือถ้าเจอ brute-force attack เข้าไปก็จบเห่อีกเหมือนกัน
แล้วนี่อะไร? มาโชว์ว่ากรอก username ถูกหรือผิด
ลองหาข้อมูลความปลอดภัยเกี่ยวกับเรื่องนี้เพิ่มเติมดูนะ
http://stackoverflow.com/questions/14922130/which-error-message-is-better-when-users-entered-a-wrong-password
Date :
2016-05-23 20:11:11
By :
mr.v
ขอบคุณครับ ผมอยากให้มาติแบบนี้ละครับ จะได้แก้ไปทีละอย่าง ขอบคุณครับ mr.v
Date :
2016-05-23 20:33:35
By :
slaparum
Load balance : Server 04