2015-02-24 137 views
0

這是我的每個.php頁面。我可以讓它註冊並進入我的本地服務器。但是,當我進入登錄頁面並實際登錄時,它不會重定向到成員頁面。我不確定它是什麼問題。我對PHP很新,代碼看起來不錯。很簡單,但我試圖讓這個工作。任何幫助表示讚賞。謝謝。PHP標頭不能在本地服務器上工作

的config.php

<?php 
$host  = "localhost"; 
$username = "root"; 
$password = "root"; 
$db  = "motofoto"; 

//Connect to MySQL Server 
$con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server."); 

?> 

的login.php

<?php 
    session_start(); 
require "config.php"; //Connection Script, include in every file! 

//Check to see if the user is logged in. 
if(isset($_SESSION['username'])){ 
    header("Location: members.php"); //isset check to see if a variables has been 'set' 
} 



if(isset($_POST['submit'])) 
{ 
    //Variables from the table 
    $user = $_POST['user']; 
    $pass = $_POST['pass']; 

    //Prevent MySQL Injections 
    $user = stripslashes($user); 
    $pass = stripslashes($pass); 

    $user = mysqli_real_escape_string($con, $user); 
    $pass = mysqli_real_escape_string($con, $pass); 

    //Check to see if the user left any space empty! 
    if($user == "" || $pass == "") 
    { 
     echo "Please fill in all the information!"; 
    } 

    //Check to see if the username AND password MATCHES the username AND password in the DB 
    else 
    { 
     $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB."); 
     $count = mysqli_num_rows($query); 

     if($count == 1){ 
     //YES WE FOUND A MATCH! 
     @$_SESSION['username'] = $user; //Create a session for the user! 
     header ("Location: members.php"); 
     } 

     else{ 
     echo "Username and Password DO NOT MATCH! TRY AGAIN!"; 
     } 
    } 

} 

?> 

<html> 
<table> 

<tr> 
<form name="register" method="post" action="login.php"> 
<td> 

<table> 

<tr> 
<td colspan="3"><strong><center>Login </center></strong></td> 
</tr> 

<tr> 
<td>Username</td> 
<td>:</td> 
<td><input autofocus name="user" type="text" id="user"></td> 
</tr> 

<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="pass" type="password" id="pass"></td> 
</tr> 

<tr> 
<td></td> 
<td></td> 
<td><input type="submit" name="submit" value="Login"></td> 
</tr> 

</table> 
</td> 
</form> 
</tr> 
</table> 

<table> 
    <tr> 
     <td>Not a Member? <a href="register.php">Register</a></td> 
    </tr> 
</table> 


</html> 

register.php

<?php 
session_start(); //Must Start a session. 

require "config.php"; //Connection Script, include in every file! 

//Check to see if the user is logged in. 
//'isset' check to see if a variables has been 'set' 
if(isset($_SESSION['username'])){ 
    header("location: members.php"); 
} 

//Check to see if the user click the button 
if(isset($_POST['submit'])) 
{ 
    //Variables from the table 
    $user = $_POST['user']; 
    $pass = $_POST['pass']; 
    $rpass = $_POST['rpass']; 

    //Prevent MySQL Injections 
    $user = stripslashes($user); 
    $pass = stripslashes($pass); 
    $rpass = stripslashes($rpass); 

    $user = mysqli_real_escape_string($con, $user); 
    $pass = mysqli_real_escape_string($con, $pass); 
    $rpass = mysqli_real_escape_string($con, $rpass); 

    //Check to see if the user left any space empty! 
    if($user == "" || $pass == "" || $rpass == "") 
    { 
     echo "Please fill in all the information!"; 
    } 

    else 
    { 
     //Check too see if the user's Passwords Matches! 
     if($pass != $rpass) 
     { 
     echo "Passwords do not match! Try Again"; 
     } 

     //CHECK TO SEE IF THE USERNAME IS TAKEN, IF NOT THEN ADD USERNAME AND PASSWORD INTO THE DB 
     else 
     { 
     //Query the DB 
     $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user'") or die("Can not query the TABLE!"); 

     //Count the number of rows. If a row exist, then the username exist! 
     $row = mysqli_num_rows($query); 
     if($row == 1) 
     { 
      echo "Sorry, but the username is already taken! Try again."; 
     } 

     //ADD THE USERNAME TO THE DB 
     else 
     { 
      $add = mysqli_query($con,"INSERT INTO members (id, username, password) VALUES (null, '$user' , '$pass') ") or die("Can't    Insert! "); 
      echo "Successful! <a href='members.php'> Click Here </a> to log In."; 
     } 


     }  

    } 

} 
?> 

<html> 
<table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> 

<tr> 
<form name="register" method="post" action="register.php"> 
<td> 

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 

<tr> 
<td colspan="3"><strong><center>Registration</center></strong></t 
d> 
</tr> 

<tr> 
<td width="78">Username</td> 
<td width="6">:</td> 
<td width="294"><input name="user" type="text" id="user"></td> 
</tr> 

<tr> 
<td>Password</td> 
<td>:</td> 
<td><input name="pass" type="password" id="pass"></td> 
</tr> 

<tr> 
<td>Repeat Password</td> 
<td>:</td> 
<td><input name="rpass" type="password" id="rpass"></td> 
</tr> 

<tr> 
<td></td> 
<td></td> 
<td><input type="submit" name="submit" value="Register"></td> 
</tr> 

</table> 
</td> 
</form> 
</tr> 
</table> 

</html> 

members.php

<?php 
session_start(); 
require "config.php"; 

//Check to see if the user is logged in. 
if(isset($_SESSION['username'])){ 
    echo "Hello ".$_SESSION['username'].", you are logged in. <br /> This the member's page! Nothing here :(. <a href='logout.php'>Click Here </a>to log out."; 
} 

else{ 
    echo "Please <a href='login.php'>Log In </a> to view the content on this page!"; 
} 

?> 

logout.php

<?php 
session_start(); 
require "config.php"; 
session_destroy(); 

echo "You have successfully logged out. <a href='login.php'> Click here </a> to login!"; 

?> 
+0

你得到的錯誤是什麼? – 2015-02-24 03:50:30

+3

在每個標題後添加'exit;'。另外,在打開PHP標記 (例如'<?php error_reporting(E_ALL);)後立即將錯誤報告添加到文件頂部。 ini_set('display_errors',1);'然後你的代碼的其餘部分,看看它是否產生任何東西。 還要把'或者死(mysqli_error($ con))'添加到'mysqli_query()'中。也刪除'@'符號;這是一個錯誤抑制器。 – 2015-02-24 03:52:07

+2

注意:您應該在標題中使用位置,而不是位置。 – 2015-02-24 03:59:12

回答

0

或者您可以使用下面的函數,通過Java腳本來重定向。這不是解決方案,但可以用作替代方案。

function redirect($url) 
{ 
    echo $data= "<script type='text/javascript'> window.location.href = '".$url."'; </script>"; 
    break; 
} 
1

1)嘗試添加會話關閉功能,這可能有助於爲會話可能尚未保存。

@$_SESSION['username'] = $user; //Create a session for the user! 
session_write_close(); 
header ("Location: members.php"); 

2)和弗雷德提到的嘗試調試與PHP錯誤報告。

3)小記:register.php =>更改鏈接的login.php不members.php

echo "Successful! <a href='Login.php'> Click Here </a> to log In."; 

PS:我測試你的腳本,它工作得很好,即使沒有session_write_close();

相關問題