2017-09-16 100 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']); 
相关问题