我知道我犯了一些小错误,但我找不到哪里。有人能指出吗?当输入值与mysql表的值匹配时,我希望我的页面重定向到索引页面。但它还没有起作用。你能检查一下并告诉我吗
if(isset($_SESSION["manager"]))
{
header("location:index.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login for Photovale</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/datepicker3.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
error_reporting(0);
ini_set('display_errors', 0);
$connect= mysql_connect("localhost","root","")or die('not connected');
$db= mysql_select_db("photovale")or die('not connected');
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
if(isset($_POST['username']) && isset($_POST['password']) )
{
echo $sql=mysql_query("SELECT id from login WHERE user==$username && pass==$password");
echo $rows=mysql_num_rows($sql);
if($rows>0)
{
echo "rowsgenerated";
while($fetch= mysql_fetch_array($sql))
{
$id=$fetch[id];
}
$_SESSION["id"]=$id;
$_SESSION["manager"]=$username;
$_SESSION["password"]=$password;
header("location:index.php");
}
//print_r($_REQUEST);
}
?>
<div class="row">
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">Log in</div>
<div class="panel-body">
<form method="post" role="form">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="username" name="username" type="text" autofocus="" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="" required>
</div>
<!---div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div--->
<input type="submit" value="submit" name="submit" class="btn btn-primary">
</fieldset>
</form>
</div>
</div>
</div><!-- /.col-->
</div><!-- /.row -->
</body>
</html>
您必须像这样编写登录查询,它将重定向
//login script
if(isset($_POST['login'])){
$username = trim(htmlspecialchars($_POST['username']));
$password = trim(htmlspecialchars($_POST['password']));
//if username or password is empty
if(empty($username) || empty($password)){
echo "<div class='alert alert-danger'>Fill in all the fields</div>";
exit();
}
//check username and password match the db record
$q = mysqli_query($con,"SELECT id FROM 'user' WHERE username='$username' AND password='$password'");
if(mysqli_num_rows($q) != 1){
echo "<div class='alert alert-danger'>Invalid username or password</div>";
exit();
}
//fetch the if of the loggedin user start the session
$row = mysqli_fetch_assoc($q);
//set the session with loggedin user id
$_SESSION['id'] = $row['id'];
$_SESSION['username'] = $username;
header("Location: index.php");
exit();
}
?>
LOGIN p[![enter image description here][1]][1]age
and the page will redirect to index.php
[![enter image description here][1]][1]
login
选择id
WHERE username='$username'AND password='$password'";echo$sql=mysql_query($sql1);