2014-05-25 58 views
-1

請幫助我,如果你可以,身份證欣賞它。使用PHP的簡單重定向 - 不會重定向

我有兩個文件 - index.php和home.php,代碼在當前的作品,如果我改變重定向到類似回聲'登錄成功'它的作品,但當我把重定向代碼回來它纔不是。

的index.php

 <link rel="stylesheet" type="text/css" href="css/index.css"/> 
<html> 
<head> 
    <title>SG - 2014</title> 
</head> 
<body background="images/strainsbackground.jpg"> 
<div id="allcolour"> 
<body> 
<h1></h1> 
<?php 
if (!isset($_POST['submit'])){ 
?> 
<a href="/register.php" style="text-decoration:none"> 
     <button label="Register" name="so_link">Register</button> 
</a> 
<div id ="formcolour"> 
<!-- The HTML login form --> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
     Username: <br><input type="text" name="username" /><br /> 
     Password: <br><input type="password" name="password" /><br /> 
     <input type="submit" name="submit" value="Login" /> 

     <a href="register.php">Register</a> 
    </form> 
    </div> 

<?php 
} else { 
    require_once("db_const.php"); 
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
    # check connection 
    if ($mysqli->connect_errno) { 
     echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
     exit(); 
    } 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1"; 
    $result = $mysqli->query($sql); 
    if (!$result->num_rows == 1) { 
     echo "Invalid username/password combination"; 
    } else { 
    $_SESSION['user'] = $_POST['username']; 
     header('location:home.php'); 
    }} 


?> 

</body> 
</div> 
</div> 
</html> 

感謝

+0

PHP頭部必須高於所有HTML。如果沒有,你會得到'頭已經發送'錯誤。 –

+1

1.你應該做精確的用戶名和密碼匹配,否則有可能會選擇錯誤的用戶。 2.你應該加密你的密碼。 不完全是你問的,但你應該考慮。 –

+0

加密和其他的東西將實施一旦我的登錄實際工作,謝謝你的迴應 – user3674355

回答

0

您正在嘗試設置頭之後,他們已經送出。

您的代碼:

Output some html 
Trying to set header LOCATION 
More HTML 

通過發送HTML您發送標題,你不能Manager標題,他們已經發出後。這就是爲什麼你的代碼不起作用。

這樣做:

Set header LOCATION 
Output some html 

所以,你的代碼將是:

<?php 
if (isset($_POST['submit'])){ 
    require_once("db_const.php"); 
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
    # check connection 
    if ($mysqli->connect_errno) { 
     echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
     exit(); 
    } 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1"; 
    $result = $mysqli->query($sql); 
    if (!$result->num_rows == 1) { 
     echo "Invalid username/password combination"; 
    } else { 
    $_SESSION['user'] = $_POST['username']; 
     header('location:home.php'); 
    }} 


?> 

     <link rel="stylesheet" type="text/css" href="css/index.css"/> 
<html> 
<head> 
    <title>SG - 2014</title> 
</head> 
<body background="images/strainsbackground.jpg"> 
<div id="allcolour"> 
<body> 
<h1></h1> 
<?php 
if (!isset($_POST['submit'])){ 
?> 
<a href="/register.php" style="text-decoration:none"> 
     <button label="Register" name="so_link">Register</button> 
</a> 
<div id ="formcolour"> 
<!-- The HTML login form --> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
     Username: <br><input type="text" name="username" /><br /> 
     Password: <br><input type="password" name="password" /><br /> 
     <input type="submit" name="submit" value="Login" /> 

     <a href="register.php">Register</a> 
    </form> 
    </div> 

<?php 
} 
?> 

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

謝謝你的迴應Sharikov!我已經實現了你的代碼,但不幸的是它仍然不起作用。 這是正確的代碼去home.php文件之上 - user3674355

+0

我不知道什麼意思是「它不工作」。你有什麼輸出?你確定你傳球嗎? –

+0

當我輸入我的憑據時,頁面只是刷新並且只顯示背景圖片而變爲空白。 – user3674355

0

相反的:

​​

用途:

echo <<<EOT 

<script> 
document.location = 'home.php'; 
</script> 

EOT; 

是的,爲時已晚,因此請使用Javascript重定向。

或者,將您的PHP腳本移動到文件頂部。 (更新,包括 「在session_start()」。)

的index.php:

<?php 

session_start(); 

if (isset($_POST['submit'])){ 
    require_once("db_const.php"); 
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
    # check connection 
    if ($mysqli->connect_errno) { 
     echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
     exit(); 
    } 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1"; 
    $result = $mysqli->query($sql); 
    if (!$result->num_rows == 1) { 
     $error_msg = 'Invalid username/password combination'; 
    } else { 
     $_SESSION['user'] = $_POST['username']; 
     header('location:home.php'); 
    } 
} 
?> 
<html> 
<head> 
    <title>SG - 2014</title> 
    <link rel="stylesheet" type="text/css" href="css/index.css"/> 
</head> 
<body background="images/strainsbackground.jpg"> 
<div id="allcolour"> 
<body> 

<?=$error_msg?> 

<h1></h1> 
<a href="/register.php" style="text-decoration:none"> 
     <button label="Register" name="so_link">Register</button> 
</a> 
<div id ="formcolour"> 
<!-- The HTML login form --> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
     Username: <br><input type="text" name="username" /><br /> 
     Password: <br><input type="password" name="password" /><br /> 
     <input type="submit" name="submit" value="Login" /> 

     <a href="register.php">Register</a> 
    </form> 
    </div>  
</body> 
</div> 
</div> 
</html> 

home.php:

<?php 

session_start(); 

if(!isset($_SESSION['user'])) { 
    header('location:index.php'); 
} 

// home 

?> 
+0

仍然有你的代碼,它不工作,你能告訴我什麼代碼應該在home.php頁面的頂部嗎? – user3674355

+0

也許你沒有在你的php文件中包含session_start()。 – bloodyKnuckles

0

我沒有看到所有的代碼,但我認爲問題是這一行:

if (!$result->num_rows == 1) { 

嘗試把它變成這樣:

if ($result->num_rows !== 1) {