2015-11-14 116 views
0

我正在創建登錄頁面。當我第一次登錄時,它將我重定向到main.php。當它從var_dump得到這個:string(7)「Francis」這很好。但是,如果我刷新main.php,我得到這個通知:未定義的索引:在第20行path_to_file \ main.php中的名字 NULL(var_dump($ _ SESSION ['firstname']);)。爲什麼會話首次運行,然後在刷新時失敗/消失?未定義的會話索引PHP

下面是代碼:

頁:finishline_module.php

<?php 
    /*****************************************************/ 
    /* This module was created on 10/20/2015 at 1:40AM. */ 
    /*****************************************************/ 

    // Start the session 
    session_start(); 

    /**************************/ 
    /* Create System Settings */ 
    /**************************/ 

    /* Database Connection Settings */ 
    $_SESSION['servername']  = "localhost"; 
    $_SESSION['mysql_username'] = "xxxxxxxxxxxxx"; 
    $_SESSION['mysql_password'] = "xxxxxxxxxxxxx"; 
    $_SESSION['dbname']   = "xxxxxxxxxxxxx"; 

    //Turn on Error Report. True = On/False = Off 
    ErrorReporting(true); 

    //Display PHP Errors. 
    function ErrorReporting($ErrOn){ 
     if ($ErrOn == true) { 
      //Show Error 
      ini_set('display_errors',1); 
      ini_set('display_startup_errors',1); 
      error_reporting(-1); 
     } 
    } 

    /************************************** 
    Open Database Connection Function. 
    ***************************************/ 
    function db_conn($servername, $mysql_username, $mysql_password, $dbname) { 
     $conn = mysqli_connect($servername, $username, $password, $dbname); 

     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 

     // Test if connection succeeded 
     if(mysqli_connect_errno()) { 
      die("Database connection failed: " . 
       mysqli_connect_error() . 
       " (" . mysqli_connect_errno() . ")" 
     ); 
     } 
    } 

    /************************************** 
    Close Database Connection Function. 
    ***************************************/ 
    function db_disconn() { 
    $conn = null; 
    } 

    /*************************************** 
    Employee Login Check: 
    ****************************************/ 
    function CheckLogin($strUserName, $strPassword) { 

    if (isset($strUserName) && !empty($strUserName) && isset($strPassword) && !empty($strPassword)) { 

     /*db_conn("localhost", "FinishLine2015!$", "Knuckle$20025272",  "nj_finishline2015"); Open db*/ 
      $conn = new mysqli($_SESSION['servername'],  $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']); 
     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

       $sql = "SELECT id, firstname, lastname, user_name, password FROM tbl_employees WHERE user_name='$strUserName' AND password='$strPassword' AND account_disabled='';"; 
       $result = $conn->query($sql); 

      //Check and see if there are records avaiable. 
      if ($result->num_rows > 0) { 
       // output data of each row with a loop. 
       while($row = $result->fetch_assoc()) { 

        //Store the info into a session variable. 
        $_SESSION['eid']  = $row["id"]; 
        $_SESSION['firstname'] = $row["firstname"]; 
        $_SESSION['lastname'] = $row["lastname"]; 

        return $_SESSION["eid"]; 
        //break; //Stop the loop process. 
       } 
      } else { 
       //No records found prompt the user. 
        return "User name or Password was Incorrect! Please try again!"; 
      } 
      db_disconn(); /*Close db*/ 
     } 
    } 
    ?> 

頁:的index.php

<?php 
//Included file. 
include 'modules/finishline_module.php'; 
include 'modules/validate_login.php'; 

//Turn on Error Report. True = On/False = Off 
ErrorReporting(true); 

//Grab Login Info and store into a variable. 
if (isset($_POST["username"]) && !empty($_POST["username"]) &&  isset($_POST["password"]) && !empty($_POST["password"])) { 
    $username = trim($_POST["username"]); 
    $password = trim($_POST["password"]); 
} 

//Use to test username and password output. 
    //if (isset($_POST["username"]) && !empty($_POST["username"]) &&  isset($_POST["password"]) && !empty($_POST["password"])) { 
    // echo $username." ".$password; 
    // exit(); 
    //} 

//Check if a value was entered for username and password. 
if (isset($username) && !empty($username) && isset($password) &&   !empty($password)) { 
    //Call function to log user in. 
    $strLogin = CheckLogin($username, $password); 

    if ($strLogin != ""){ 
     header('Location: main.php'); 
    } 
} 
?> 

<html> 
<head> 
<title>XXXXX Online</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<script Language="JavaScript"> 

var popup="This Site Copyright © protected 2015 XXXXXX all rights reserved."; function noway(go) 
{ if 
(document.all) { if (event.button == 2) { alert(popup); return false; } } if   (document.layers) 
{ if (go.which == 3) { alert(popup); return false; } } } if  (document.layers) 
{ document.captureEvents(Event.MOUSEDOWN); } document.onmousedown=noway; 

function validRequired(formField,fieldLabel) 
{ 
    var result = true; 

    if (formField.value == "") 
    { 
    alert('Please enter a value for the "' + fieldLabel +'" field.'); 

    formField.focus(); 

    result = false; 
    } 
    return result; 
} 


function validateForm(theForm) 
{ 

    if (!validRequired(theForm.username,"User name")) 
    return false; 

    if (!validRequired(theForm.password,"Password")) 
    return false; 

    return true; 
} 
</script> 
     <link href="css/xxxxx.css" rel='stylesheet' type='text/css' /> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <script type="application/x-javascript"> addEventListener("load",  function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){  window.scrollTo(0,1); } </script> 
     <!--webfonts--> 
     <link href='http://fonts.googleapis.com/css? family=Open+Sans:600italic,400,300,600,700' rel='stylesheet' type='text/css'> 
     <!--//webfonts--> 
</head> 
<body> 

<!-----start-main----> 
    <div class="main"> 
     <div class="login-form"> 
      <h1>Finish Line Member Login</h1> 
        <div class="head"> 
         <img src="menubar/finish_line_logo_trans.gif" alt=""/> 
        </div> 
       <form name="formlgin" method="post" action="index.php" onSubmit="return validateForm(this)" id="formlgin"> 

         <input type="text" value="Knuckles02" name="username" class="text" placeholder="Username" onFocus="this.value = '';"> 
         <input type="password" value="12345" name="password" placeholder="Password" onFocus="this.value = '';"> 
         <div class="submit"> 
          <input type="submit" onClick="myFunction()" value="LOGIN" > 
        </div> 
       </form> 
      </div> 
      <!--//End-login-form--> 

     </div> 
      <!-----//end-main----> 
</body> 
</html> 

頁:main.php

<?php 
//Included file. 
include 'modules/finishline_module.php'; 
include 'modules/validate_login.php'; 

//Turn on Error Report. True = On/False = Off 
ErrorReporting(true); 
?> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Finish Line CMS</title> 
</head> 

<body> 

<?php 
var_dump($_SESSION['firstname']); 

if (isset($_SESSION['firstname']) && !empty($_SESSION['firstname']) &&  isset($_SESSION['lastname']) && !empty($_SESSION['lastname'])) { 
    echo "Welcome Back: ".$_SESSION['firstname']." ".$_SESSION['lastname']; 
} 
?> 

</body> 
</html> 

頁:validate_login.php

<?php 
if (isset($_SESSION['eid']) && !empty($_SESSION['eid'])) { 
    // Finally, destroy the session. 
    session_destroy(); 
} 
?> 

問題:刷新main.php時

會議消失。

回答

1

好吧我想清楚發生了什麼,以及爲什麼我在第一次工作後失去了會話,然後在刷新頁面時會發生折騰和錯誤。它與包含的「validate_login.php」頁面有關。當我刷新導致錯誤未定義索引時,它正在銷燬會話。這裏是導致問題的代碼:

<?php 
if (isset($_SESSION['eid']) && !empty($_SESSION['eid'])) { 
     // Finally, destroy the session. 
     session_destroy(); 
} 
?>