2015-11-21 42 views
0

這是我的登錄頁面,登錄後,普通玩家去home.php。但我需要的,如果用戶名和密碼是與例如該用戶一個特殊的球員等於:如何登錄到PHP的管理員頁面

admin

然後去adminpage.php

和代碼:

<?PHP 
require_once("./include/membersite_config.php"); 

if(isset($_POST['submitted'])) 
{ 
    if($fgmembersite->Login()) 
    { 
     $fgmembersite->RedirectToURL("home.php"); 
    } 
} 
     <!-- Form Code Start --> 
<div id='fg_membersite'> 
<form id='login' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'> 
<fieldset > 
<legend>ورود</legend> 

<input type='hidden' name='submitted' id='submitted' value='1'/> 

<div class='short_explanation' dir="rtl">فیلدهای الزامی *</div> 

<div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div> 
<div class='container' dir="rtl"> 
    <label for='username' >:نام کاربری *</label><br/> 
    <input type='text' name='username' id='username' value='<?php echo $fgmembersite->SafeDisplay('username') ?>' maxlength="50" /><br/> 
    <span id='login_username_errorloc' class='error'></span> 
</div> 
<div class='container' dir="rtl"> 
    <label for='password' >:کلمه عبور *</label><br/> 
    <input type='password' name='password' id='password' maxlength="50" /><br/> 
    <span id='login_password_errorloc' class='error'></span> 
</div> 

<div class='container'> 
    <input type='submit' name='Submit' value='ورود' /> 

</div> 
<!-- 
<div class='short_explanation'><a href='reset-pwd-req.php'>Forgot Password?</a></div> 
</fieldset> --> 
</form> 
<!-- client-side Form Validations: 
Uses the excellent form validation script from JavaScript-coder.com--> 

<script type='text/javascript'> 
// <![CDATA[ 

    var frmvalidator = new Validator("login"); 
    frmvalidator.EnableOnPageErrorDisplay(); 
    frmvalidator.EnableMsgsTogether(); 

    frmvalidator.addValidation("username","req","لطفا نام کاربریتان را وارد نمایید"); 

    frmvalidator.addValidation("password","req","لطفا رمز عبورتان را وارد نمایید"); 

// ]]> 
</script> 
<p dir="rtl"><a href="confirmreg.php">تایید ثبت نام</a></p> 
</div> 
<!-- 
Form Code End (see html-form-guide.com for more info.) 
--> 

這裏是我的fg_membersite.php中的函數

function Login() 
{ 

    if(empty($_POST['username'])) 
    { 
     $this->HandleError("UserName is empty!"); 
     return false; 
    } 

    if(empty($_POST['password'])) 
    { 
     $this->HandleError("Password is empty!"); 
     return false; 
    } 

    $username = trim($_POST['username']); 
    $password = trim($_POST['password']); 

    if(!isset($_SESSION)){ session_start(); } 
    if(!$this->CheckLoginInDB($username,$password)) 
    { 
     return false; 
    } 

    $_SESSION[$this->GetLoginSessionVar()] = $username; 

    return true; 
} 

這是用戶表(tablesite)

enter image description here

+0

您的問題是什麼?你嘗試過什麼嗎? –

+0

不知道我是否正確理解問題,但所有這些答案都可以工作。你嘗試過嗎? – sinaza

+0

@sinaza:你可以回答這個問題:http://stackoverflow.com/questions/33860829/choose-a-name-from-drop-down-list-or-if-not-exit-can-write-in-text -box/33861097#33861097 – sammy

回答

0
function Login() 
{ 

    if(empty($_POST['username'])) 
    { 
     $this->HandleError("UserName is empty!"); 
     return false; 
    } 

    if(empty($_POST['password'])) 
    { 
     $this->HandleError("Password is empty!"); 
     return false; 
    } 

    $username = trim($_POST['username']); 
    $password = trim($_POST['password']); 

    if(!isset($_SESSION)){ session_start(); } 
    if(!$this->CheckLoginInDB($username,$password)) 
    { 
     return false; 
    } 

    if($username == "special_user_name") 
    // redirect to adminlogin.php 

    $_SESSION[$this->GetLoginSessionVar()] = $username; 

    return true; 
} 
+0

你能否提供一些解釋 –

1

嘗試:

if(isset($_POST['submitted'])) 
{ 
    if($fgmembersite->Login()){ 
     if($fgmembersite->UserFullName() == "sajad"){ 
       $fgmembersite->RedirectToURL("adminpage.php"); 
     }else{ 
       $fgmembersite->RedirectToURL("home.php"); 
     } 
    } 
} 
0

如果你能和不反對這樣做,我建議將表添加到數據庫是包含各種用戶角色和上表中顯示爲新表的關鍵字的新字段。通過分配的角色,更容易改變哪些用戶可以看到什麼內容 - 即:管理員可以看到編輯鏈接,註冊用戶可以看到新頁面等。

create table `roles` (
    `id` tinyint(3) unsigned not null auto_increment, 
    `role` varchar(16) not null, 
    primary key (`id`) 
) 
collate='utf8mb4_persian_ci' engine=innodb; 

insert into `roles` (`id`, `role`) values 
    (1, 'administrator'), 
    (2, 'special guest'), 
    (3, 'guest'), 
    (4, 'public'); 

alter table `tablesite` 
    add column `role` tinyint unsigned not null default '3' after `id_user`, 
    add index `role` (`role`); 

alter table `tablesite` 
    add constraint `fk_usr_role` foreign key (`role`) references `roles` (`id`) on update cascade on delete cascade;