2012-12-06 100 views
1

當我勾選複選框(cookie)並按 登錄時,它沒有標題到成員頁面。它的頭部索引頁面(當前頁面)。Cookie複選框故障

,但是當我再次點擊複選框(餅乾),然後按日誌 這頭到會員頁面

有人能幫助我哪裏出了問題?

<?PHP 
session_start(); 

function loggedin(){ 
    if(isset($_SESSION['log_email']) || isset($_COOKIE['log_email'])){ 
    $loggedin=TRUE; 
    return $loggedin; 
    } 
} 

$log_email=""; 

if(loggedin()){ 
header("Location:member.php");} 

if(isset($_POST['login'])){ 

    $log_email=strtolower($_POST['log_email']); 
    $log_password=$_POST['log_password']; 

    if(isset($_POST['cookie'])){ 
    $cookie=$_POST['cookie']; 
    } 


    if($log_email && $log_password){ 
     $connect=mysql_connect("localhost", "root", ""); 
     $database=mysql_select_db("phplogin", $connect); 

     $SQL=mysql_query("SELECT * FROM users WHERE email='$log_email'"); 
     $numrows=mysql_num_rows($SQL); 

     if($numrows!=0){ 
      while($result=mysql_fetch_assoc($SQL)){ 
       $db_email=$result['email']; 
       $db_password=$result['password']; 
       $db_firstname=$result['firstname']; 
       $db_lastname=$result['lastname']; 
      } 

      if($log_email==$db_email && md5($log_password)==$db_password){  
        if($cookie){ 
         setcookie("log_email", $log_email, time()+7200); 
        } 
        else{ 
        $_SESSION['log_email']=$log_email; 
        header("location:member.php"); 
        exit(); 
        } 
      } 
      else{echo"wrong username or password";} 
     } 
     else{echo "Can't find the user";} 
    } 
    else{echo "Please enter email or password";} 
} 


?> 




<html> 
<body> 
<div id="container"> 
<div id="logo"></div> 
    <div id="search"></div> 
    <div id="top_search"></div> 


    <div id="login"> 
     <form action="index.php" method="POST"> 
     <table> 
      <tr> 
       <td><lable id="td_1">Email</lable></td> 
       <td><lable id="td_1">Password</lable></td> 
      </tr> 
      <tr> 
       <td><input type="text" name="log_email" value="<?PHP echo $log_email; ?>" maxlength="50"/></td> 
       <td><input type="password" name="log_password" maxlength="25" /></td> 
       <td><input id="log_btn" type="submit" name="login" value="" /></td> 
      </tr> 
       <tr> 
       <td><div id="td_2"><input type="checkbox" name="cookie"/>Remember me</div></td> 
       <td><div id=td_2><a href="forgot_password.php">Forgot your password?</div></a></td> 
      </tr> 
     </table> 
     </form> 
    </div> 

</div> 
</body> 
</html> 
+0

不要HTML或其他輸出之前打印發送標題。 – MrCode

回答

1

<?php打開之前,你開始<html>此頁面,更多的數據。只有在沒有輸出到瀏覽器的情況下,頁眉才能正常工作,所以這不會起作用(除非在顯示的代碼之前,頁面上有某種輸出緩衝處於活動狀態)。

只需從<?php開始,然後移動<html>和其他標籤(包括換行符)在php代碼之後檢查登錄。

然後,而不是與echo直接outputing的錯誤信息,將它們存儲在像$error = "wrong username or password";一個變種,然後在HTML代碼稍後打印<?php echo $error; ?>

<?PHP 
    session_start(); 

    function loggedin(){ 
     if(isset($_SESSION['log_email']) || isset($_COOKIE['log_email'])){ 
     $loggedin=TRUE; 
     return $loggedin; 
     } 
    } 

    $log_email=""; 
    $error = ""; 

    if(loggedin()){ 
    header("Location:member.php");} 

    if(isset($_POST['login'])){ 

     $log_email=strtolower($_POST['log_email']); 
     $log_password=$_POST['log_password']; 

     if(isset($_POST['cookie'])){ 
     $cookie=$_POST['cookie']; 
     } 


     if($log_email && $log_password){ 
      $connect=mysql_connect("localhost", "root", ""); 
      $database=mysql_select_db("phplogin", $connect); 

      $SQL=mysql_query("SELECT * FROM users WHERE email='$log_email'"); 
      $numrows=mysql_num_rows($SQL); 

      if($numrows!=0){ 
       while($result=mysql_fetch_assoc($SQL)){ 
        $db_email=$result['email']; 
        $db_password=$result['password']; 
        $db_firstname=$result['firstname']; 
        $db_lastname=$result['lastname']; 
       } 

       if($log_email==$db_email && md5($log_password)==$db_password){  
         if($cookie){ 
          setcookie("log_email", $log_email, time()+7200); 
          header("location:member.php"); 
         } 
         else{ 
         $_SESSION['log_email']=$log_email; 
         header("location:member.php"); 
         exit(); 
         } 
       } 
       else{$error = "wrong username or password";} 
      } 
      else{$error = "Can't find the user";} 
     } 
     else{$error = "Please enter email or password";} 
    } 


    ?> 


    <html> 
    <link href="CSS/login.css" rel="stylesheet" type="text/css" /> 

    <body> 
    <div id="container"> 
    <div id="logo"></div> 
     <div id="search"></div> 
     <div id="top_search"></div> 


     <div id="login"> 



      <?php 
      if (strlen($error)>0) 
       echo $error; 
      ?> 


      <form action="index.php" method="POST"> 
      <table> 
       <tr> 
        <td><lable id="td_1">Email</lable></td> 
        <td><lable id="td_1">Password</lable></td> 
       </tr> 
       <tr> 
        <td><input type="text" name="log_email" value="<?PHP echo $log_email; ?>" maxlength="50"/></td> 
        <td><input type="password" name="log_password" maxlength="25" /></td> 
        <td><input id="log_btn" type="submit" name="login" value="" /></td> 
       </tr> 
        <tr> 
        <td><div id="td_2"><input type="checkbox" name="cookie"/>Remember me</div></td> 
        <td><div id=td_2><a href="forgot_password.php">Forgot your password?</div></a></td> 
       </tr> 
      </table> 
      </form> 
     </div> 

    </div> 
    </body> 
    </html> 
+1

我在setcookie下添加頭並解決問題 setcookie(「log_email」,$ log_email,time()+ 7200); header(「location:member.php」); –