 |
จะพัฒนาระบบ Login & Register โดยแยก User กับ Admin จะต้องทำยังไงค่ะ |
|
 |
|
|
 |
 |
|
อันนนี้ไฟล์ javascript
Code (PHP)
/********************************************************************************************************************
* This script is brought to you by Vasplus Programming Blog by whom all copyrights are reserved.
* Website: www.vasplus.info
* Email: [email protected]
* Please, do not remove this information from the top of this page.
*********************************************************************************************************************/
function vpb_users_registration()
{
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var vpb_firstname = $("#firstname").val();
var vpb_lastname = $("#lastname").val();
var vpb_email = $("#email").val();
var vpb_passwd = $("#passwd").val();
if(vpb_firstname == "")
{
$("#signup_status").html('<div class="info">Please enter your firstname in the required field to proceed.</div>');
$("#firstname").focus();
}
else if(vpb_lastname == "")
{
$("#signup_status").html('<div class="info">Please enter your lastname to proceed.</div>');
$("#lastname").focus();
}
else if(vpb_email == "")
{
$("#signup_status").html('<div class="info">Please enter your email address to proceed.</div>');
$("#email").focus();
}
else if(reg.test(vpb_email) == false)
{
$("#signup_status").html('<div class="info">Please enter a valid email address to proceed.</div>');
$("#email").focus();
}
else if(vpb_passwd == "")
{
$("#signup_status").html('<div class="info">Please enter your desired password to go.</div>');
$("#passwd").focus();
}
else
{
var dataString = 'firstname='+ vpb_firstname + '&lastname=' + vpb_lastname + '&email=' + vpb_email + '&passwd=' + vpb_passwd + '&page=users_registration';
$.ajax({
type: "POST",
url: "vpb_save_details.php",
data: dataString,
cache: false,
beforeSend: function()
{
$("#signup_status").html('<br clear="all"><br clear="all"><div align="left"><font style="font-family:Verdana, Geneva, sans-serif; font-size:15px; color:black;">Please wait</font> <img src="images/loadings.gif" alt="Loading...." align="absmiddle" title="Loading...."/></div><br clear="all">');
},
success: function(response)
{
var response_brought = response.indexOf('registered_successfully=yes');
if (response_brought != -1)
{
$("#signup_status").html('');
window.location.replace(response);
}
else
{
$("#signup_status").fadeIn(1000).html(response);
}
}
});
}
}
function vpb_users_login()
{
var vpb_email = $("#email").val();
var vpb_passwd = $("#passwd").val();
if(vpb_email == "")
{
$("#login_status").html('<div class="info">Please enter your account email address to proceed.</div>');
$("#email").focus();
}
else if(vpb_passwd == "")
{
$("#login_status").html('<div class="info">Please enter your account password to go.</div>');
$("#passwd").focus();
}
else
{
var dataString = 'email=' + vpb_email + '&passwd=' + vpb_passwd + '&page=users_login';
$.ajax({
type: "POST",
url: "vpb_save_details.php",
data: dataString,
cache: false,
beforeSend: function()
{
$("#login_status").html('<br clear="all"><br clear="all"><div align="left"><font style="font-family:Verdana, Geneva, sans-serif; font-size:15px; color:black;">Please wait</font> <img src="images/loadings.gif" alt="Loading...." align="absmiddle" title="Loading...."/></div><br clear="all">');
},
success: function(response)
{
var response_brought = response.indexOf('login_process_completed_successfully=yes');
if (response_brought != -1)
{
$("#login_status").html('');
window.location.replace(response);
}
else
{
$("#login_status").fadeIn(1000).html(response);
}
}
});
}
}
อันนี้คือไฟล์ที่ใช้ประมวลผล
Code (PHP)
<?php
/********************************************************************************************************************
* This script is brought to you by Vasplus Programming Blog by whom all copyrights are reserved.
* Website: www.vasplus.info
* Email: [email protected]
* Please, do not remove this information from the top of this page.
*********************************************************************************************************************/
session_start();
include "database_connection.php";
if(isset($_POST["page"]) && !empty($_POST["page"]))
{
//Sign-up Page Starts here
if($_POST["page"] == "users_registration")
{
$firstname = trim(strip_tags($_POST['firstname']));
$lastname = trim(strip_tags($_POST['lastname']));
$user_email = trim(strip_tags($_POST['email']));
$user_password = trim(strip_tags($_POST['passwd']));
$encrypted_md5_password = md5($user_password);
$check_for_duplicates = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."'");
if($firstname == "" || $lastname == "" || $user_email == "" || $user_password == "")
{
echo '<br><div class="info">กรุณากรอกข้อมูลให้ครบถ้วน</div><br>';
}
elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $user_email))
{
echo '<br><div class="info">กรุณาใส่ E-mail ให้ถูกต้อง</div><br>';
}
else if(mysql_num_rows($check_for_duplicates) > 0)
{
echo '<br><div class="info">e-mailนี้มันคนใช้แล้ว<br>กรุณาเปลี่ยน e-mailใหม่</div><br>';
}
else
{
if(mysql_query("insert into `signup_and_login_table` values('', '".mysql_real_escape_string($firstname)."', '".mysql_real_escape_string($lastname)."', '".mysql_real_escape_string($user_email)."', '".mysql_real_escape_string($encrypted_md5_password)."', '".mysql_real_escape_string(date('d-m-Y'))."')"))
{
$_SESSION["VALID_USER_ID"] = $user_email;
$_SESSION["USER_FULLNAME"] = strip_tags($firstname.' '.$lastname);
echo 'index.php?uid='.$_SESSION["USER_FULLNAME"].'&';
echo 'registered_successfully=yes';
}
else
{
echo '<br><div class="info">เกิดข้อผิดพลาดไม่สามารถสร้างบัญชีสมาชิกได้ กรุณาลองใหม่อีกครั้ง<br>หรือติดต่อ Admin:[email protected]</br></div><br>';
}
}
}
//Sign-up Page Ends here
//Login Page Starts here
elseif($_POST["page"] == "users_login")
{
$user_email = trim(strip_tags($_POST['email']));
$user_password = trim(strip_tags($_POST['passwd']));
$encrypted_md5_password = md5($user_password);
$validate_user_information = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."'");
if(mysql_num_rows($validate_user_information) == 1)
{
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $user_email;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["firstname"].' '.$get_user_information["lastname"]);
echo 'index.php?uid='.$_SESSION["USER_FULLNAME"].'&';
echo 'login_process_completed_successfully=yes';
}
else
{
echo '<br><div class="info">คุณใส่ รหัสผ่าน หรือ emailผิด กรุณาใส่ให้ถูกต้องด้วยค่ะ</div><br>';
}
}
//Login Page Ends here
}
?>
คำถามคือจะสร้างไฟล์เพื่อเช็คว่าเป็น Admin อีกไฟล์ หรือ ใส่ else if ไปได้เลยตรงล่าง
Code (PHP)
if(mysql_num_rows($validate_user_information) == 1)
{
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $user_email;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["firstname"].' '.$get_user_information["lastname"]);
echo 'index.php?uid='.$_SESSION["USER_FULLNAME"].'&';
echo 'login_process_completed_successfully=yes';
}
แต่ลองใส่ else if ไปไม่ทราบว่าเงื่อนไขผิดหรืออะไร คือเวลาล็อกอินเป็น Admin ก็ยังเหมือนเดิม คือไปหน้าเดียวกับผู้ใช้ คืองงค่ะ ช่วยไกด์ให้นิดนึงก็ได้ค่ะ มือใหม่ค่ะ ไปไม่ถูกเลยเอาของเค้ามาพัมฒาต่อ ขอบคุณมากค่ะ
Tag : PHP, JavaScript, Ajax, jQuery
|
|
 |
 |
 |
 |
Date :
2014-12-28 09:16:56 |
By :
WarantornP |
View :
1089 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เห็นมี ตัวแปรสำหรับ ตรวจสอบว่าเป็น admin หรือ user เลยครับ
เมื่อตรวจสอบ user password แล้ว ก็ควร ส่งตัวแปรที่กำหนดว่าเป็น user หรือ admin กลุับมาด้วยครับ
สำหรับ javascript ตัวรับก็เช็คค่า ที่ส่งมา แล้วค่อย redirect ไปยัง program ที่ต้องการต่อไป
|
 |
 |
 |
 |
Date :
2014-12-28 14:21:10 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
/********************************************************************************************************************
* This script is brought to you by Vasplus Programming Blog by whom all copyrights are reserved.
* Website: www.vasplus.info
* Email: [email protected]
* Please, do not remove this information from the top of this page.
*********************************************************************************************************************/
session_start();
ob_start();
include "database_connection.php";
if(isset($_POST["page"]) && !empty($_POST["page"]))
{
//Sign-up Page Starts here
if($_POST["page"] == "users_registration")
{
$firstname = trim(strip_tags($_POST['firstname']));
$lastname = trim(strip_tags($_POST['lastname']));
$user_email = trim(strip_tags($_POST['email']));
$user_password = trim(strip_tags($_POST['passwd']));
$encrypted_md5_password = md5($user_password);
$check_for_duplicates = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."'");
if($firstname == "" || $lastname == "" || $user_email == "" || $user_password == "")
{
echo '<br><div class="info">กรุณากรอกข้อมูลให้ครบถ้วน</div><br>';
}
elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $user_email))
{
echo '<br><div class="info">กรุณาใส่ E-mail ให้ถูกต้อง</div><br>';
}
else if(mysql_num_rows($check_for_duplicates) > 0)
{
echo '<br><div class="info">e-mailนี้มันคนใช้แล้ว<br>กรุณาเปลี่ยน e-mailใหม่</div><br>';
}
else
{
if(mysql_query("insert into `signup_and_login_table` values('', '".mysql_real_escape_string($firstname)."', '".mysql_real_escape_string($lastname)."', '".mysql_real_escape_string($user_email)."', '".mysql_real_escape_string($encrypted_md5_password)."', '".mysql_real_escape_string('normal')."', '".mysql_real_escape_string(date('d-m-Y'))."')"))
{
$_SESSION["VALID_USER_ID"] = $user_email;
$_SESSION["USER_FULLNAME"] = strip_tags($firstname.' '.$lastname);
echo 'index.php?uid='.$_SESSION["USER_FULLNAME"].'&';
echo 'registered_successfully=yes';
}
else
{
echo '<br><div class="info">เกิดข้อผิดพลาดไม่สามารถสร้างบัญชีสมาชิกได้ กรุณาลองใหม่อีกครั้ง<br>หรือติดต่อ Admin:[email protected]</br></div><br>';
}
}
}
//Sign-up Page Ends here
//Login Page Starts here
elseif($_POST["page"] == "users_login")
{
$user_email = trim(strip_tags($_POST['email']));
$user_password = trim(strip_tags($_POST['passwd']));
$encrypted_md5_password = md5($user_password);
// Check for normal user login during the login process
$check_normal_user = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."' and `role` = '".mysql_real_escape_string('normal')."'");$result = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."' and `role` = '".mysql_real_escape_string('normal')."'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Check for admin user login during the login process
$check_admin_user = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."' and `role` = '".mysql_real_escape_string('admin')."'");$result = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($user_email)."' and `password` = '".mysql_real_escape_string($encrypted_md5_password)."' and `role` = '".mysql_real_escape_string('normal')."'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($check_normal_user) > 0) // If this user is a normal user
{
header("location: 'index.php'");
}
elseif(mysql_num_rows($check_admin_user) > 0) // If this user is an admin user
{
// Redirect to admins page
header("location: 'home.php'");
}
else
{
echo '<br><div class="info">คุณใส่ รหัสผ่าน หรือ emailผิด กรุณาใส่ให้ถูกต้องด้วยค่ะ</div><br>';
}
}
//Login Page Ends here
}
?>
ตอนนี้ งงว่าจะแยก สถานะยังไง จะ insert ว่าเปง user หรือ admin ตรงไหน
|
 |
 |
 |
 |
Date :
2014-12-29 22:57:53 |
By :
WarantornP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|