請幫助我,如果你可以,身份證欣賞它。使用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>
感謝
PHP頭部必須高於所有HTML。如果沒有,你會得到'頭已經發送'錯誤。 –
1.你應該做精確的用戶名和密碼匹配,否則有可能會選擇錯誤的用戶。 2.你應該加密你的密碼。 不完全是你問的,但你應該考慮。 –
加密和其他的東西將實施一旦我的登錄實際工作,謝謝你的迴應 – user3674355