2015-11-29 92 views
0

我正在構建一個有成員和用戶的登錄系統。我有一張有名字,用戶名,密碼,電子郵件和類型列的表格。類型指定用戶的類型。我已經創建了一個工作登錄表單。我的問題是每次我嘗試登錄時都不會重定向到指定的頁面。它保持在同一頁面上,但給我一個空白頁面。我一直在弄清楚代碼有什麼問題。有人能幫我嗎 ? 謝謝。管理員/會員登錄PHP不重定向

PHP登錄代碼:

<?php 
include 'try_connect.php'; 

if (isset($_POST['login'])) { 
$user = $_POST['username']; 
$pass = $_POST['password']; 
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'"); 
$data = mysql_fetch_array($hsl); 
$username = $data['username']; 
$password = $data['password']; 
$type = $data['type']; 
$name = $data['name']; 
if ($user==$username && $pass==$password) { 
    session_start(); 
    $_SESSION['name']=$name; 
    if ($type =='admin') { 
     header('Location: try_admin.php'); 
    } 
    elseif ($type =='user') { 
     header('Location:try_user.php'); 
    } 
    } 
    } 
?> 

登錄FORM

<form action="try_login.php" method="POST"> 
<table style="margin-left: 30%; margin-bottom: 1%;"> 
    <tr> 
     <th style="font-family: Arial; line-height: 5px;">Username:</th> 
     <td><input type="text" name="username" style="margin-left: 10%; width: 120%; margin-bottom: 5%;"></td> 
    </tr> 
    <tr> 
     <th style="font-family: Arial;">Password:</th> 
     <td><input type="password" name="password" style="margin-left: 10%; width: 120%; margin-bottom: 0%; margin-top: 10%;"></td> 
    </tr> 
</table> 
<input type="submit" value="login" name="login" style="background-color: black; color: white; margin-bottom: 5%; margin-top: 0%; margin-left: 59%; border-color: #89cff0; border-style: double solid;"> 

+0

什麼是在控制檯還請確保您的header()功能是任何html輸出之前叫什麼名字? – Afsar

回答

0

試試這個:

<?php 
include 'try_connect.php'; 

if (isset($_POST['login'])) { 
$user = $_POST['username']; 
$pass = $_POST['password']; 
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'"); 
$data = mysql_fetch_array($hsl); 
$username = $data['username']; 
$password = $data['password']; 
$type = $data['type']; 
$name = $data['name']; 
if ($user==$username && $pass==$password) { 
    session_start(); 
    $_SESSION['name']=$name; 
    if ($type =='admin') { 
     header("Location: try_admin.php"); 
    } 
    elseif ($type =='user') { 
     header("Location:try_user.php"); 
    } 
    } 
    } 
?> 
+0

有什麼區別? –

+0

只需將單引號改爲雙引號 –

0

使用exit()header()exit()將立即終止您的腳本。

if ($type =='admin') { 
     header('Location: try_admin.php'); 
     exit(); 
    } 
    elseif ($type =='user') { 
     header('Location:try_user.php'); 
     exit(); 
    } 

甚至在<html>標籤