2015-04-30 161 views
0

我創建了一個LoginPage.php用下面的代碼頁之間不宣Session變量仍然

<? 
session_start(); 
if (isset($_SESSION['uid'])) 
{ 
    header('Location: index.php');   
} 
?> 
<!DOCTYPE html> 
    <html> 
    <head> 
    <?php 
    include("indexhead.php"); 
    ?> 
    </head> 
<body> 
    <?php 
     include("myCarousel.php"); 
     echo " 
     <div class='container'> 
      <div class='row'> 
       <div class='col-sm-6 col-md-4 col-md-offset-4'> 

       <h1 class='text-center login-title'>Sign in to continue:</h1> 
        <div class='account-wall'> 

         <form class='form-signin' method='POST'> 

          <input name='uid' type='text' class='form-control' 
          placeholder='Username' required autofocus > 
          <input name='pass' type='password' class='form-control' 
          placeholder='Password' required > 

          <button class='btn btn-lg btn-primary btn-block' type='submit'> 
          Sign in</button> 

         </form> 
        </div> 

       </div> 
      </div> 
     </div> 
     "; 

    if (isset($_POST['uid']) and isset($_POST['pass'])) 
    { 
     if (empty($_POST['uid']) or empty($_POST['pass'])) 
     { 
      echo "Please type data into the login";     
     } 
     else 
     { 
       $uname = stripslashes($_POST['uid']); 
       $pass = stripslashes($_POST['pass']); 
       $nick = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname. 
       "' and pass = md5('".$pass."');"); 
       if ($nick != '') 
       { 
        $_SESSION['Nick1'] = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname. 
        "' and pass = md5('".$pass."');"); 

        $_SESSION['FullName'] = selectSpecific("select FullName from hera.LoginTable where uid ='".$uname. 
        "' and pass = md5('".$pass."');"); 

        $_SESSION['dept'] = selectSpecific("select dept from hera.LoginTable where uid ='".$uname. 
        "' and pass = md5('".$pass."');"); 

        $_SESSION['accesslevel'] = selectSpecific("select accesslevel from hera.LoginTable where uid ='".$uname. 
        "' and pass = md5('".$pass."');"); 

        $_SESSION['uid'] = selectSpecific("select uid from hera.LoginTable where uid ='".$uname. 
        "' and pass = md5('".$pass."');"); 


        header("Location: index.php"); 
        //echo $_SESSION['Nick1']; 

       } 
       else 
       { 
        echo "Unknown Username/Password"; 
       } 
     } 
    } 
?> 
</body> 
</html> 

它的作用是檢查登錄,當登錄成功,用戶被引導到在index.php然而,index.php頁面沒有檢測到$ _SESSION ['']變量,即$_SESSION['uid']它說:注意:未定義的索引:uid在C:\ xampp \ htdocs \ Hera \ index.php中在線14

但我可以確認會話變量從函數selectSpecific()接收內容。爲什麼會議沒有舉行?

這裏是在index.php

<?php 
    session_start(); 

    echo $_SESSION['uid']; 

?> 
+0

而不是使用頁面頂部的頁面回顯$ _SESSION ['uid']並查看它獲得的值 – Dalvik

+0

您可以檢查您的查詢是否可以嗎? 'echo「test =」。 selectSpecific(「select uid from hera.LoginTable where uid ='」。$ uname。 「'and pass = md5('」。$ pass。「'');」);出口; '$ _SESSION ['uid'] = ...'行 – Peter

+0

之前,如果你可以從數據庫中獲取整行,比如'$ _SESSION ['user'] = selectRow(「select * from hera .LoginTable其中uid ='「。$ uname。 」'和pass = md5('「。$ pass。」');「);'(假設你有selectRow函數) – Peter

回答

1

我建議打破下來到了最簡單的迭代和代碼只是與會話1頁初始化

<?php 
    session_start(); 

    $_SESSION['uid'] = 12345; 

,另一個與回聲。

<?php 
session_start(); 

var_dump($_SESSION); 

如果出現空白,PHP session handler(這裏有很多很好的信息)正在發生。

還要確保會話已建立。默認情況下,它應該使用cookie(php.ini中的session.use_cookies = 1),所以你應該看到一個「PHPSESSID」cookie集。沒有這個,就沒有辦法真正開展會議。

+0

只要我回到項目上,我會馬上做mattrt –

+0

它輸出array(0){} –

+0

session.use_cookies = 1在php.ini中設置 –

0

先檢查您的會話值比改變你的html像下面你有沒有給定的名稱提交按鈕

<form class='form-signin' method='POST'> 

          <input name='uid' type='text' class='form-control' 
          placeholder='Username' required autofocus > 
          <input name='pass' type='password' class='form-control' 
          placeholder='Password' required > 

          <input class='btn btn-lg btn-primary btn-block' type='submit' name='submit'/> 

不是改變你的PHP的if condition像下面

 if(isset($_POST['submit'])) 
     { 
     if(isset($_POST['uid']) && isset($_POST['pass'])) 
     { 
      if (empty($_POST['uid']) || empty($_POST['pass'])) 
      { 
       echo "Please type data into the login";     
      } else 
       {  
       $uname = stripslashes($_POST['uid']); 
       $pass = stripslashes($_POST['pass']); 

     $nick =mysqli_query("select * from `hera.LoginTable` where `uid` ='".$uname."' and `pass` = '".md5($pass)."'","$dbcon"); 
    $rowcount=mysqli_num_rows($nick); 
       if ($rowcount==1) 
       { 
       $row=mysqli_fetch_array($result,MYSQLI_ASSOC); 

        $_SESSION['Nick1'] = $row['Nick1']; 

        $_SESSION['FullName'] = $row['FullName']; 

        $_SESSION['dept'] = $row['dept']; 

        $_SESSION['accesslevel'] = $row['accesslevel']; 

        $_SESSION['uid'] = $row['uid']; 

        header("Location: index.php"); 

       } 
       else 
       { 
        echo "Unknown Username/Password"; 
       } 
     } 
    } } 
    ?> 

,並使用多個查詢爲什麼不要使用下面的單個查詢

"select * from `hera.LoginTable` where `uid` ='".$uname. 
        "' and `pass` = '".md5($pass)."'"); 
+0

我確定登錄沒有問題。我的問題是與會議不工作在網頁上,我去頭與 –

+0

價值仍然傳遞給$ _SESSION時,我使用我的功能selectSpecific(),但它不會在頁面指向的頁面。 –

0

我只是忘了把<?php放在我的LoginForm.php文件的最上面。