2015-08-09 134 views
-2

會話ID未顯示錯誤消息注意:未定義索引:UserID也如果我重定向未登錄的用戶,我將不會登錄到主頁iether ..這裏是我的文件任何人都可以請告訴我我要去哪裏錯了。會話ID未顯示錯誤消息注意:未定義索引:UserID

這裏是主頁:

<?php 
require ("insert.php"); 
session_start(); 
if (isset($_SESSION["UserID"])){ 
} 
else { 
    echo "session not transferring"; 
} 
?> 



<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
    <title>AMSadler</title> 
    <meta charset="utf-8"> 
    <meta name="description" content="description of webpage"> 
    <meta name="keywords" content="keywords go here"> 
    <meta name="author" content="Anthony"> 
    <link rel="stylesheet" href="css/style.css"> 
    <link rel="stylesheet" href="css/home.css"> 
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png"> 
</head> 
<body> 

     <div class="header"> 

      <div id="logo"> 
       <a href="index.html"><img src="img/logo.png" alt="logo" title="AMSadler.com"/></a> 
      </div> 

      <div id="headertop"> 
       <div id="pagetitle"> 
        <a href="home.php">HOME</a> 
       </div> 

       <div id="pagetitle2"> 
        <a href="profile.php">PROFILE</a> 
       </div> 
       <div id="pagetitle3"> 
        <h4>Welcome:<?php echo $_SESSION{"UserID"};?>to your page</h4> 
       </div> 
      </div> 

     </div> 

      <div class="wrapper"> 

       <div class="leftsidebar"> 
        <ul> 
         <li><a href="home.php">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
         <li><a href="#">blah</a></li> 
        </ul> 
       </div> 

       <div class="main"> 
        <div id="maintop"> 
         <div id="textboxwrap"> 
          <textarea name="text" id="styled"rows="5" cols="80" placeholder="Write something here."> 
          </textarea> 
         </div> 

         <div id="postbutton"> 
          <input type="submit" name="post" value="Post" /> 
         </div> 
        </div> 

        <div id="mainbottom1"> 
         <div ="mainbottomwrap"> 
          Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br> 
          Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br> 
          Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br>Your posts will go here.<br> 
          Your posts will go here.<br>Your posts will go here.<br> 

         </div> 
        </div> 
       </div> 

       <div class="rightsidebar"> 
        <div id="profilecontainer"> 
         <img src="img/profile.png" alt="Upload a profile pic" title="profile pic" /> 
        </div> 

        <div id="box"> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a><br> 
         <a href="#">blah</a> 
        </div> 
       </div> 
      </div> 

</body> 
</html> 

================================= ================================= 這裏是登錄頁面到達那裏。 +++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++

<?php require ("insert.php"); ?> 
<?php 
    if(isset($_POST[ 'Login' ])) 
    { 
    $email= mysqli_real_escape_string($con, $_POST ['email']); 
    $pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']); 

    $result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' "); 

    $row = $result->fetch_array(MYSQLI_BOTH); 

    session_start(); 

    $_SESSION["User ID"] = $row['UserID']; 
    header ('Location: home.php'); 
    } 
?> 
<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
    <title>AMSadler login</title> 
    <meta charset="utf-8"> 
    <meta name="description" content="description of webpage"> 
    <meta name="keywords" content="keywords go here"> 
    <meta name="author" content="Anthony"> 
    <link rel="stylesheet" href="css/style.css"> 
    <link rel="stylesheet" href="css/login.css"> 
    <link rel="index" href="index.php"> 
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png"> 
</head> 
<body> 

     <div class="header"> 
      <div id="logo"> 
       <a href="index.html"><img src="img/logo.png" alt="logo" title="AMSadler.com"/></a> 
      </div> 

      <div id="signup"> 
       <button type="button"><a href="signup.php">Sign up</a></button> 
      </div> 
     </div> 

     <div id="login"> 
      <form action="home.php" method="post"> 
       <input type="text" name="email" placeholder="Email address"> 
       <br> 
       <input type="password" name="password" placeholder="Password"> 
       <br> 
       <input id="Login" type="submit" name="Login" value="Login"> 
      </form> 
     </div> 

     <footer> 
      <div id="copyright"> 
       <p>&copy Copyright 2015</p> 
      </div> 
     </footer> 

</body> 
</html> 
+0

他們是一個錯字錯誤。他們在表單頁面中的'User ID'中有空格,但在主頁上的'UserID'中沒有空格。 – Pushkar

回答

1

在登錄頁面你分配變量,因爲這:

$_SESSION["User ID"] = $row['UserID']; 

但在主頁上你」重新尋找UserID

變化:

$_SESSION["User ID"] = $row['UserID']; 

$_SESSION["UserID"] = $row['UserID']; 

點你得到你的錯誤是從從在HTML您的使用情況,你說:

<h4>Welcome:<?php echo $_SESSION{"UserID"};?>to your page</h4> 

在首先您使用的是isset(),但在這裏您突然認爲它已經存在並準備好了使用。無論哪種方式,你應該編輯User ID變量,所以這會停止拋出一個錯誤。您可能還想在此處添加isset()聲明。

P.S.同時請注意維克托佩雷斯的答案,認爲您的密碼。並且不要將它們存儲爲純文本。你實際上只是檢查數據庫中的密碼POST,這是不好的。此外,我可能會建議您不要使用此類查詢,並開始使用prepared statements

2

我當然希望你只是發佈一個例子,你沒有明文存儲密碼值......反正...

我沒有足夠的代表,以添加評論@Bono,但除了他的研究結果,您還使用了下面的帖子:

$_POST ['pswrd'] 

但表單上,您有:

<input type="password" name="password" placeholder="Password"> 

無論是密碼的形式,名稱需要改變或張貼您'重新尋找。