的login.php
<form class="form-signin" action="#" method="POST">
<span id="reauth-email" class="reauth-email"></span>
<input type="text" name="usern" class="form-control" placeholder="Username">
<input type="password" name="passw" class="form-control" placeholder="Password">
<div id="remember" class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<input onClick="myFunction()" class="btn btn-lg btn-primary btn-block btn-signin" type="submit" name="sub" value="Login">
</form>
PHP脚本
<?php
if(isset($_POST['sub']))
{
include('user.php');
$us = mysqli_real_escape_string($conn,$_POST['usern']);
$pw = mysqli_real_escape_string($conn,$_POST['passw']);
if(!empty($_POST['usern']) || !empty($_POST['passw']))
{
$query = "SELECT ID FROM myuser WHERE username = '$us' AND
password = SHA('$pw')";
$result = @mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
if($row)
{
header('Location: homepage.php');
exit;
}
else
{
echo '<script language="javascript">';
echo 'alert("Invalid Username/Password")';
echo '</script>';
}
}
else
{
echo '<script language="javascript">';
echo 'alert("Please enter Username/Password")';
echo '</script>';
mysqli_close($conn);
}
}
?>
我无法使标题位置有效!我不知道为什么,我已经搜索了一些修复和线程,但没有任何结果。如何修改我的代码,有人能发现错误吗?
答案 0 :(得分:0)
尝试添加“http('s',如果需要):// your_domain/homepage.php
答案 1 :(得分:0)
尝试改变:
if($row) {
为:
if(!empty($row)) {
希望这有效。
如果解决方案#1不起作用,请尝试更改:
$row = mysqli_fetch_array($result);
为:
$row = mysqli_num_rows($result);
和
if($row) {
为:
if($row > 0) {
)
答案 2 :(得分:0)
我终于解决了它们! 我做的是放弃mysqli并使用PDO。 这是我的新代码:
<?php
session_start();
$h = "localhost";
$u = "root";
$p = "";
$db = "user";
$msg = "";
try
{
$con = new PDO("mysql:host=$h; dbname=$db", $u, $p);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["sub"]))
{
if(empty($_POST["usern"]) || empty($_POST["passw"]))
{
$msg = "<label>Please enter username/password!</label>";
}
else
{
$sqlquery = "SELECT * FROM myuser WHERE username = :username AND password = :password";
$stat = $con->prepare($sqlquery);
$stat->execute(
array(
':username' => $_POST["usern"],
':password' => $_POST["passw"]
)
);
$countrow = $stat->rowCount();
if($countrow > 0)
{
$_SESSION["username"] = $_POST["usern"];
header("Location: homepage.php");
}
else
{
$msg = "<label>Invalid username/password</label>";
}
}
}
}
catch(PDOException $er)
{
$msg = $er->getMessage();
}
?>
答案 3 :(得分:-1)
PHP 5.6及更高版本不支持带标题的重定向 要解决此问题,请使用JavaScript重定向,如下面的代码
if($row){
echo '<script>window.location = "YOUR_URL";</script>';
}
答案 4 :(得分:-2)
mysql_fetch_array不会返回 true
。
if($row !== false){
header("Location: homepage.php");
}