2016-07-28 52 views
-1

我通過登錄表單使用會話,但是在嘗試登錄時沒有重定向到index.php。請讓我知道我錯在哪裏。下面 是下面的login.php代碼HTMLPHP PDO SESSION無法重定向

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>Admin Panel</title> 
 
\t <link rel="stylesheet" type="text/css" href="admin_style.css"> 
 
</head> 
 
<body> 
 

 
<h1>Welcome To Home Page</h1> 
 

 
</body> 
 
</html>

  <?php } ?> 

在與形式我對指數和登錄

  <?php 
     session_start(); 

      if (!isset($_SESSION['user_name'])) { 
      header("location: login.php"); 
      } 
      else{ 
       echo "welcome $user_name"; 
      ?> 

代碼

  <?php 
      session_start(); 
      ?> 

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>Admin Login</title> 
 
</head> 
 
<body> 
 
<form action="login.php" action="post"> 
 
\t <table width="400" align="center" border="20"> 
 

 
\t <tr> 
 
\t \t <td colspan="5" align="center" bgcolor="gray"><h1>Admin Login</h1></td> 
 
\t </tr> 
 

 
\t <tr> 
 
\t \t <td align="right">User Name:</td> 
 
\t \t <td><input type="text" name="user_name"></td> 
 
\t </tr> 
 

 
\t <tr> 
 
\t \t <td align="right">User Password</td> 
 
\t \t <td><input type="password" name="user_pass"></td> 
 
\t </tr> 
 

 
\t <tr> 
 
\t \t <td align="center" colspan="5"><input type="submit" name="login" value="Login"></td> 
 
\t \t 
 
\t </tr> 
 
\t \t 
 
\t </table> 
 
</form> 
 

 
</body> 
 
</html>

<?php 
include("connect.php"); 

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

    $user_name = $_POST['user_name']; 
    $user_pass = $_POST['user_pass']; 

    $sth = $con->prepare("SELECT * FROM admin_login WHERE user_name='$user_name' AND user_password='$user_pass'"); 


    $sth->execute(); 


    if($sth->rowCount() > 0) { 

     $_SESSION['user_name'] = $user_name; 
     header("Location: index.php"); // Redirecting To index Page 
     } else { 
     echo "<script>alert('Username or Password is invalid')</script>"; 
     } 

} 


?> 

通過這個代碼,我只是能夠看到地址欄線和沒有JavaScript警告: http://localhost/malala/admin/login.php?user_name=kitchen&user_pass=kitchen&login=Login

請幫助....

+0

永遠不要發送或存儲清除密碼!更多的'GET'方法,這真的很危險 – Vuldo

回答

0

http://localhost/malala/admin/login.php?user_name=kitchen&user_pass=kitchen&login=Login發送變量爲GET,你正在尋找它們作爲POST

+0

謝謝Alok Patel現在工作,但JavaScript錯誤它不會彈出 – David

+0

以及它的工作很好謝謝 – David