2017-09-16 39 views
0

我對PHP非常陌生,所以請原諒我缺乏知識。目前我有一個沒有數據庫的PHP登錄腳本。我測試了只有一個用戶名和密碼,它的工作原理非常好。但是,我需要添加另一組用戶名和密碼,並且他們應該重定向到不同的頁面。沒有數據庫的PHP登錄腳本 - 不同的頁面重定向

PHP登錄腳本:

<?php session_start(); /* Starts the session */ 

    /* Check Login form submitted */  
    if(isset($_POST['Submit'])){ 
     /* Define username and associated password array */ 
     $logins = array('Username1' => '123456', 'ReadOnly' => '1234567'); 

     /* Check and assign submitted Username and Password to new variable */ 
     $Username = isset($_POST['Username']) ? $_POST['Username'] : ''; 
     $Password = isset($_POST['Password']) ? $_POST['Password'] : ''; 

     /* Check Username and Password existence in defined array */   
     if (isset($logins[$Username]) && $logins[$Username] == $Password){ 
      /* Success: Set session variables and redirect to Protected page */ 
      $_SESSION['UserData']['Username']=$logins[$Username]; 
      $_SESSION['start'] = time(); // Taking logged in time. 
      $_SESSION['expire'] = $_SESSION['start'] + (5400*60); 
      // Ending a session in an hour from the starting time. 
      header("location:index.php"); 
      exit; 
     } else { 
      /*Unsuccessful attempt: Set error message */ 
      $msg="<span style='color:red'>Invalid Login Details</span>"; 
     } 
    } 
?> 

腳本的頁面被重定向到:

<?php 
session_set_cookie_params(0); 
session_start(); /* Starts the session */ 

if(!isset($_SESSION['UserData']['Username'])){ 
    header("location:login.php"); 
} 
    else { 
     $now = time(); // Checking the time now when home page starts. 

     if ($now > $_SESSION['expire']) { 
      session_destroy(); 
      echo "Your session has expired! <a href='http://localhost/PortalV4/login.php'>Login here</a>"; 
     } 
     else { //Starting this else one [else1] 

?> 

目前,它只是重定向用戶名爲 'USERNAME1' 和密碼「123456到index.php 」。我需要使用用戶名'ReadOnly'和密碼'1234567'來讀取不同的頁面重定向到readOnly.php。我如何比較不同的用戶名和密碼並重定向到不同的頁面?

+0

我有很多問題,所以你需要實現USERNAME1到的index.php和其他所有用戶到其他index2.php的?對?如果要添加更多用戶,強烈建議使用數據庫。 –

+0

@AjmalPraveen是的,這是正確的。我很樂意使用數據庫,但不幸的是,我正在使網頁的公司無法爲我安裝數據庫,由於安全原因 – nurul98

+0

OH壞..但我可以看到有人給你一個解決方案,你可以使用。 –

回答

0

您可以添加目標URL作爲登錄陣列的一部分:

$logins = [ 
    'Username1' => 
     [ 
      'pass'  =>'123456', 
      'destination'=>'index.php', 
     ], 
    'ReadOnly' => 
     [ 
      'pass'  =>'1234567', 
      'destination'=>'read-only.php', 
     ] 
    ]; 

    /* Check Username and Password existence in defined array */   
    if (isset($logins[$Username]) && $logins[$Username]['pass'] == $Password){ 
     //session stuff here then 
     header("location:" . $logins[$Username]['destination']);