2016-08-26 79 views
0

我有一個用戶登錄的登錄網頁。然後頁面作爲loginvalidte.php重定向到臨時頁面。此頁面保存在會話的用戶數據,並轉發該請求到index.php頁面其中一些一些用戶數據,並且還具有被重定向到的login.php我的會話重新出現在PHP甚至在銷燬後

總之退出按鈕,

login.php - For user to enter username and password 
loginvalidate.php - Session values are initialized 
index.php - Dashboard page with logout button 

這裏是我的網頁:

的login.php

<!DOCTYPE html> 
<?php 

//session_unset(); 
session_destroy(); 
$_SESSION = array(); 

$authError='false'; 
if($_GET['AuthCheck']=='failed'){ 
     $authError='true'; 
} 
if($_GET['Expired']=='true'){ 
     $sessionexpire='true'; 
} 

//print_r ($_SESSION); 
foreach($_SESSION as $key => $val) 
{ 
     unset($_SESSION[$key]); 
} 

//unset($_SESSION["InfraUser"]); 
//unset($_SESSION["InfraPassword"]); 
$_SESSION["InfraUser"]=''; 
$_SESSION["InfraPassword"]=''; 

$_SESSION = NULL; 
print_r($_SESSION); 

?> 

<html > 
    <head> 
    <meta charset="UTF-8"> 
    <title>One click Infra</title> 
     <link rel="stylesheet" href="loginstyle/css/style.css"> 
    </head> 
    <body> 
    <html> 
<html> 

<head> 

    <meta charset="UTF-8"> 

    <title>Login Form</title> 
<script src="loginstyle/js/prefixfree.min.js"></script> 

</head> 

<body> 

    <div id="logo"> 
    <h1><i> One Click Infra</i></h1> 
</div> 
<section class="stark-login"> 

    <form action="loginvalidate.php" method="post"> 
     <?php if($authError=='true'){ ?> 
       <div id="fade-box"> 
         <p>Authentication Failed. Please Login Again</p> 
       </div> 
     <?php } 
       else if ($sessionexpire=='true'){ ?> 
       <div id="fade-box"> 
         <p>Session Expired. Please Login Again</p> 
       </div> 
     <?php }?> 


    <div id="fade-box"> 
       <input type="text" name="username" class="form-control" placeholder="Username" required="" /> 
       <input type="password" name="userpassword" class="form-control" placeholder="Password" required="" /> 
       <div hidden> 
         <input type="text" name="authorize" class="form-control" placeholder="Authorize" value="on"/> 
       </div> 
      <button>Log In</button> 
     </div> 
     </form> 
     <div class="hexagons"> 
       <img src="http://i34.photobucket.com/albums/d133/RavenLionheart/NX-Desktop-BG.png" height="768px" width="1366px"/> 
       </div> 
      </section> 
      <div id="circle1"> 
       <div id="inner-cirlce1"> 
       <h2> </h2> 
       </div> 
      </div> 
      <ul> 
       <li></li> 
       <li></li> 
       <li></li> 
       <li></li> 
       <li></li> 
      </ul> 
    <script src='http://codepen.io/assets/libs/fullpage/jquery.js'></script> 
    <script src="loginstyle/js/index.js"></script> 
</body> 
</html> 
     <script src="loginstyle/js/index.js"></script> 
    </body> 
</html> 

loginvalidate.php

<?php 

session_start(); 
$User = $_POST["username"]; 
$Password = $_POST["userpassword"]; 

include('/opt/lampp/htdocs/oneclickinfra/Net/SSH2.php'); 
$ssh = new Net_SSH2('10.41.66.73'); 
if (!$ssh->login('centos', 'centos')) { 
     exit('OCI Server Is Down. Please send mail to [email protected]'); 
} 


///////////////////////////////////////////////////////////////////////////////////////////// 
if ($_POST['authorize']){ 
     $command0 = 'curl --request POST "http://gitlab.snapdeal.com/api/v3/session?login='.$User.'&password='.$Password.'"'; 
     $req_data0 = $ssh->exec($command0); 
     if (strpos($req_data0,'Unauthorized')!==false){ 
       header("Location: login.php?AuthCheck=failed"); 
     } 
     else{ 
       $_SESSION["InfraUser"] = $User; 
       $_SESSION["InfraPassword"] = $Password; 
       print 'Data here is: '.$_SESSION["InfraUser"].' and '.$_SESSION["InfraPassword"]; 
       //sleep(10); 
       header("Location: index.php"); 
     } 
} 
//////////////////////////////////////////////////////////////////////////////////////////// 
?> 

的index.php的某些部分:

<?php 
    session_start(); 

    $User = ''; 
    $Password = ''; 

    print_r($_SESSION); 

    if(!isset($_SESSION['InfraUser'])){ 
    //if($_SESSION['InfraUser']===''){ 
      header("Location: login.php?AuthCheck=failed"); 
    } 
    else{ 
      $User = $_SESSION["InfraUser"]; 
      $Password = $_SESSION["InfraPassword"]; 
    } 

    //////////////////////////////////// Maintains Session Only for 30 Minutes /////////////////////// 
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 3600)) { 
      // last request was more than 30 minutes ago 
      //session_unset();  // unset $_SESSION variable for the run-time 
      //session_destroy(); // destroy session data in storage 
      header("Location: login.php?Expired=true"); 
    } 
    $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp 
    ////////////////////////////////////////////////////////////////////////////////////////////////// 

    $chefApiFetchAuthCheck = $_GET["chefApiFlavorFetchAuthenticationError"]; 

的問題是,當我按註銷,它被重定向到被清除所有的會話變量,因爲我沒有得到任何數據頁的login.php通過在login.php頁面打印會話數組。但是當我直接在index.php上輸入網站時,我仍然得到我的用戶會話值。

如果用戶在登出後直接輸入index.php,請幫助我將用戶重定向到loginPage。

+0

如果使用$ this-> session-> sess_destroy(); –

回答

2

您需要在login.php開始時致電session_start()。這就是爲什麼你沒有看到$_SESSION變量以及爲什麼他們沒有被重置。

+0

感謝它的工作 –

+0

@AmandeepSingh很高興幫助! –

2

因爲$_SESSION['LAST_ACTIVITY']仍然在被設置,所以即使在重定向時,您應該在header("Location: login.php?Expired=true");之後die()

對於您遇到的錯誤,您只能銷燬現有的正在運行的會話。但似乎@avenged_badger擊敗了我的那句話。

+0

感謝您的評論。它確實幫了我。 –