我在点击登录表单中的提交按钮后尝试使用header("location: profile.php")
重定向页面,但页面不会重定向。用户名和密码只是在某处提交。
这是我的主页:
<?php
include ($_SERVER["DOCUMENT_ROOT"]."/example/login.php");
if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>welcome to noteshare</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap-theme.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/jquery_popup.css" />
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/npm.js"></script>
<script src="js/jquery_popup.js"></script>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/jquery.leanModal.min.js"></script>
<style>
/* Remove the navbar's default margin-bottom and rounded borders */
.navbar {
margin-bottom: 0;
border-radius: 0;
}
/* Set height of the grid so .sidenav can be 100% (adjust as needed) */
.row.content {height: 450px}
/* Set gray background color and 100% height */
.sidenav {
padding-top: 20px;
background-color: #f1f1f1;
height: 100%;
}
/* Set black background color, white text and some padding */
footer {
background-color: #555;
color: white;
padding: 15px;
}
/* On small screens, set height to 'auto' for sidenav and grid */
@media screen and (max-width: 767px) {
.sidenav {
height: auto;
padding: 15px;
}
.row.content {height:auto;}
}</style>
</head>
<!-- Body Starts Here -->
<body id="body">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Logo</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#" id="popup" onclick="div_show()"><span class="glyphicon glyphicon-log-in"></span>Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<p><a href="#">Link</a></p>
<p><a href="#">Link</a></p>
<p><a href="#">Link</a></p>
</div>
<div class="col-sm-8 text-left">
<h1>noteshare</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididuntut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui
officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<hr>
<img src="images/girl_studying.jpg">
</div>
<div class="col-sm-2 sidenav">
<div class="well">
<p>ADS</p>
</div>
<div class="well">
<p>ADS</p>
</div>
</div>
</div>
</div>
<footer class="container-fluid text-center">
<p>Footer Text</p>
</footer>
<div id="abc">
<!-- Popup Div Starts Here -->
<div id="popuplogin">
<!-- Contact Us Form -->
<form action="" id="form" method="post" name="form">
<img id="close" src="images/cross.png" onclick ="div_hide()">
<h2>login form</h2>
<hr id="line">
<input id="username" name="username" placeholder="username" type="text" required>
<input id="password" name="password" placeholder="password" type="password" required>
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
<p> <a href="register.php">click here to register first</a></p>
</form>
</div>
<!-- Popup Div Ends Here -->
</div>
<!-- Display Popup Button -->
</body>
<!-- Body Ends Here -->
</html>
这是包含header()
函数调用的login.php页面:
<?php
session_start(); // Starting Session
$error=""; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$server="localhost";
$username="root";
$password="";
$database="noteshare";
$conn = new mysqli($server, $username, $password, $database);
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
//$username = mysql_real_escape_string($username);
//$password = mysql_real_escape_string($password);
// Selecting Database
//$db = mysqli_select_db("noteshare", $conn);
// SQL query to fetch information of registerd users and finds user match.
$qry="select * from signin where password='$password' AND username='$username'";
$query = mysqli_query($conn,$qry);
$rows = mysqli_fetch_row($query);
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("Location:http://localhost.com/profile.php", true, 301); exit;// Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($conn); // Closing Connection
}
}
?>
答案 0 :(得分:0)
您与mysql的连接是否正常,然后尝试这个,
header('Location:filename');
Eg: header('Location:test1.php');
答案 1 :(得分:0)
function mysqli_fetch_row返回一个数组或NULL。因此
if ($rows == 1)
永远不会是真的,你不会用header()来达到重定向。
您可以使用if (is_array($rows))
或if (isset($rows))
您总是可以插入一个var_dump($ rows)进行调试,以查看变量$ rows的样子。
注意,HTTP代码301是永久重定向。它应该是302.或者只是你可以省略header()函数的第二个和第三个参数,它将是302.
答案 2 :(得分:0)
试试这个
coxDeBoor
或者用JS重定向
header("Location:profile.php"); die();
答案 3 :(得分:0)
您使用的是绝对网址。在本地服务器上工作时不使用TLD .com。
变化:
header("Location:http://localhost.com/profile.php");
要:
header("Location:http://localhost/profile.php");
它应该有用!