 |
ช่วยดูโค้ด redirect ไปยังเพจต่างๆ หลังจากการ login หน่อยครับ ใช้ jquery (ต้องการแบ่งสิทธิการเข้าถึง) |
|
 |
|
|
 |
 |
|
ผมไปหาตัว jQuery มาใช้นะครับ
ตอนนี้ปรับใช้กับฐานข้อมูลได้แล้ว user ในฐานข้อมูลสามารถ login เข้าสู่ระบบได้
แต่ทีนี้ผมต้องการให้ user แต่ละระดับเข้าสู่ระบบไม่เหมือนกัน
user ธรรมดาก็เข้าไป page แรก
admin ก็เข้าไปจัดการ backend
จะต้องปรับโค้ดยังไงหรอครับ หรือมีแหล่งให้ผมศึกษาเพิ่มรึเปล่าครับ
แบบว่ามือใหม่ พึ่งเริ่ม Ajax jQuery ความรู้ยังไม่พอครับ อาศัยแก้ไปเรื่อยๆ 
init.js
// Preload Images
img1 = new Image(16, 16);
img1.src="images/spinner.gif";
img2 = new Image(220, 19);
img2.src="images/ajax-loader.gif";
// When DOM is ready
$(document).ready(function(){
// Launch MODAL BOX if the Login Link is clicked
$("#login_link").click(function(){
$('#login_form').modal();
});
// When the form is submitted
$("#status > form").submit(function(){
// Hide 'Submit' Button
$('#submit').hide();
// Show Gif Spinning Rotator
$('#ajax_loading').show();
// 'this' refers to the current submitted form
var str = $(this).serialize();
// -- Start AJAX Call --
$.ajax({
type: "POST",
url: "do-login.php", // Send the login info to this page
data: str,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
// Show 'Submit' Button
$('#submit').show();
// Hide Gif Spinning Rotator
$('#ajax_loading').hide();
if(msg == 'OK') // LOGIN OK?
{
var login_response = '<div id="logged_in">' +
'<div style="width: 350px; float: left; margin-left: 70px;">' +
'<div style="width: 40px; float: left;">' +
'<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' +
'</div>' +
'<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">'+
"You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";
$('a.modalCloseImg').hide();
$('#simplemodal-container').css("width","500px");
$('#simplemodal-container').css("height","120px");
$(this).html(login_response); // Refers to 'status'
// After 3 seconds redirect the
setTimeout('go_to_private_page()', 3000);
}
else // ERROR?
{
var login_response = msg;
$('#login_response').html(login_response);
}
});
}
});
// -- End AJAX Call --
return false;
}); // end submit event
});
function go_to_private_page()
{
window.location = 'admin/index.php'; // ผมต้องการแก้ตรงนี้ครับ ถ้าเป็น user ธรรมดาให้ไป user/index.php
}
do-login.php
<?php
session_start();
if($_POST['action'] == 'user_login')
{
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "select * from member as m, classmember as c where m.username = '$username' and m.password = '$password' and m.classid = c.classid";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
$user = $rows["username"];
$pass = $rows["password"];
$class = $rows["classname"];
$firstname = $rows["firstname"];
$lastname = $rows["lastname"];
if(isset($user))
{
$_SESSION["user"] = $user;
$_SESSION["password"] = $pass;
$_SESSION["firstname"] = $firstname;
$_SESSION["lastname"] = $lastname;
$_SESSION["class"] = $class;
if($_POST['remember_me'])
{
// set the cookies for 1 month
setcookie ("remember_me", true, (time() + TIME_DIFF) + (3600 * 24 * 30));
setcookie ("info", $user_id.','.md5($password), (time() + TIME_DIFF) + (3600 * 24 * 30));
}
echo 'OK'; // this response is checked in 'process-login.js'
}
else
{
$auth_error = '<div id="notification_error">The login info is not correct.</div>';
echo $auth_error;
}
}
?>
ส่วนของ form login
<!-- jqyrey login -->
<div id="login_form" style='display:none'>
<div id="status" align="left">
<center>
<h1><img src="images/key.png" align="absmiddle"> LOGIN</h1>
<div id="login_response"><!-- spanner -->
</div> <!--end div login_respone-->
</center>
<form id="login" action="javascript:alert('success!');">
<input type="hidden" name="action" value="user_login">
<input type="hidden" name="module" value="login">
<label>username</label><input type="text" name="username"><br />
<label>Password</label><input type="password" name="password"><br />
<label> </label><input value="Login" name="Login" id="submit" class="big" type="submit" />
<div id="ajax_loading">
<img align="absmiddle" src="images/spinner.gif"> Processing...
</div> <!--end div ajax_loading -->
</form>
</div> <!--end div status -->
</div>
<!--end jquery login -->
Tag : PHP, MySQL, Ajax, jQuery
|
ประวัติการแก้ไข 2010-10-18 23:09:37 2010-10-18 23:10:17 2010-10-18 23:11:50 2010-10-18 23:12:38
|
 |
 |
 |
 |
Date :
2010-10-18 23:06:01 |
By :
taleblue |
View :
3588 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ do-login.php ที่แสดงคำว่า OK ก็เปลี่ยนเป็นแสกงประเภท user อย่างเช่น Admin, User, Guest
แล้วก็เช็คค่าแบบเดียวกับเช็กคำว่า OK
โดยใช้คำสั่ง javascript
window.location = 'backend.php';
เพื่อย้ายหน้า
|
 |
 |
 |
 |
Date :
2010-10-18 23:11:52 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือยังไม่ค่อยเข้าใจน่ะครับ
ทำไงหว่า - -"
แล้วมันไม่ต้องไปยุ่งกับตัว init.js หรอครับ
เพราะผมเห็นมันมีอยู่แค่ในไฟล์นั้นเองที่เรียกไปหน้าอื่น
ยังไงรบกวนขอละเอียดอีกนิดนะครับ
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2010-10-18 23:45:58 |
By :
taleblue |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
//หน้า do-login.php
ไฟล์ do-login.php ที่แสดงคำว่า OK ก็เปลี่ยนเป็นแสกงประเภท user อย่างเช่น Admin, User, Guest
วิธีแก้ก็คือแสดงค่าที่อยู่ในตัวแปรแทนแสดงคำว่า OK ครับ
//หน้า init.js
แล้วก็เช็คค่าแบบเดียวกับเช็กคำว่า OK
โดยใช้คำสั่ง javascript
window.location = 'backend.php';
เพื่อย้ายหน้า
วิธีแก้ก็คือเพิ่มคำสั่ง if ไปครับ
เช่น
if (msg == 'admin'){
window.location = 'backend.php';
}
|
 |
 |
 |
 |
Date :
2010-10-18 23:51:02 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ผมลองแก้ตามที่บอกไว้ดูแล้วนะครับ
มันบอกว่า
You are successfully logged in!
Please wait while you're redirected...
ตามปกติเลย
แต่มันมันไม่ redirect ให้น่ะครับ มันหมุนๆๆๆๆๆๆๆ แต่ไม่เปลี่ยนหน้าให้ซักที
do-login.php
if($class=='admin')
echo 'admin'; // this response is checked in 'process-login.js
else echo 'user';
init.js
function go_to_private_page()
{
if(msg == 'user')
window.location = 'package.php'; // Members Area
else window.location = 'admin/index.php';
}
ผมแก้ไปตามโค้ดนี้ ถูกรึเปล่าครับ
|
 |
 |
 |
 |
Date :
2010-10-19 00:27:10 |
By :
taleblue |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
do-login ผมไม่เห็นโค้ดเต็มแต่เข้าใจว่าโค้ดถูกแล้ว
ดูตรง init.js ครับ
// Preload Images
img1 = new Image(16, 16);
img1.src="images/spinner.gif";
img2 = new Image(220, 19);
img2.src="images/ajax-loader.gif";
// When DOM is ready
$(document).ready(function(){
// Launch MODAL BOX if the Login Link is clicked
$("#login_link").click(function(){
$('#login_form').modal();
});
// When the form is submitted
$("#status > form").submit(function(){
// Hide 'Submit' Button
$('#submit').hide();
// Show Gif Spinning Rotator
$('#ajax_loading').show();
// 'this' refers to the current submitted form
var str = $(this).serialize();
// -- Start AJAX Call --
$.ajax({
type: "POST",
url: "do-login.php", // Send the login info to this page
data: str,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
// Show 'Submit' Button
$('#submit').show();
// Hide Gif Spinning Rotator
$('#ajax_loading').hide();
if(msg == 'user' || msg == 'admin') // LOGIN OK?
{
var login_response = '<div id="logged_in">' +
'<div style="width: 350px; float: left; margin-left: 70px;">' +
'<div style="width: 40px; float: left;">' +
'<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' +
'</div>' +
'<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">'+
"You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";
$('a.modalCloseImg').hide();
$('#simplemodal-container').css("width","500px");
$('#simplemodal-container').css("height","120px");
$(this).html(login_response); // Refers to 'status'
// After 3 seconds redirect the
setTimeout(function(){
if(msg == 'user')
window.location = 'package.php'; // Members Area
else window.location = 'admin/index.php';
}, 3000);
}
else // ERROR?
{
var login_response = msg;
$('#login_response').html(login_response);
}
});
}
});
// -- End AJAX Call --
return false;
}); // end submit event
});
|
 |
 |
 |
 |
Date :
2010-10-19 10:32:21 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ
ทำแบบที่พี่บอกเลย ตัดฟังก์ชั่น go_to_private_page ทิ้ง
แล้วจับการดักจับไปยัดไว้ใน setTimeout
ตอนนี้สำเร็จแล้วครับ
ขอขอบคุณมากๆเลยนะครับ 
ปล. เป็นเพราะอะไรทำไมตอนแรกมันถึงไม่ redirect ให้หรอครับ อยากรู้ไว้หน่อย เผื่อคราวหน้าจะได้ลองแก้ดูเองได้
|
ประวัติการแก้ไข 2010-10-19 11:31:45
 |
 |
 |
 |
Date :
2010-10-19 11:10:38 |
By :
taleblue |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ไม่แน่ใจ แต่อาจจะเกิดจาก ใน function go_to_private_page ไม่รู้จักตัวแปรที่ชื่อ msg ครับ
เลยเกิด error และไม่ทำงานต่อครับ
ส่วน function(){} แบบข้างล่างเรียกว่า anonymous function จะรู้จักตัวแปร msg ครับ
setTimeout(function(){
if(msg == 'user')
window.location = 'package.php'; // Members Area
else window.location = 'admin/index.php';
}, 3000);
|
 |
 |
 |
 |
Date :
2010-10-19 13:47:16 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|