我有一个登录页面,每当我按下登录按钮时,我想引导我进入主页面。如您所见,我在导航栏中有登录页面。我想从那里删除登录页面。如何从登录重定向到主页?在此先感谢!!!
的login.php
<html>
<head><h1>Login Page</h1>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<center>
<form action = "login.php" method = "POST">
Username: <input type="text" name="username"> <br /><br/>
Password: <input type="password" name="password"> <br /><br/>
<input type="submit" value="Login" name="submit">
</form>
</center>
</body>
</html>
<?php
require('connect.php');
$username = @$_POST['username'];
$password = @$_POST['password'];
if(isset($_POST['submit']))
{
if($username && $password)
{
$check = mysql_query("SELECT * FROM users WHERE username='".$username."' AND password= '".$password."'");
$rows = mysql_num_rows($check);
if(mysql_num_rows($check)!=0)
{
echo "Successful login.";
}
else
{
echo "Couldn't find username.";
}
}
else
{
echo "Please fill all the fields.";
}
}
?>
e-Statistics.htm
<html>
<head>
<title>e-Statistics</title>
<style>
.body{
margin: 0;
}
.nav{
width: 115%;
height:40px;
background:black;
text-align:center;
}
.nav ul{
width: 800px;
margin:0 auto;
padding:0;
}
.nav ul li{
list-style: none;
}
.nav ul li a{
float: left;
text-decoration:none;
display:block;
padding:10px 40px;
color:#ff9933 ;
border-right:1px solid #ccc;
}
.nav ul li a:hover{
color:white;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li><a href="login.php">Login</a></li>
<li><a href="">Principal</a></li>
<li><a href="">Academic Director</a></li>
<li><a href="">Lecturer</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
首先将PHP代码移动到文件的开头,然后使用header(“location:/main.php”);出口();成功登录后重定向
<?php
$error = '';
require('connect.php');
$username = @$_POST['username'];
$password = @$_POST['password'];
if(isset($_POST['submit']))
{
if($username && $password)
{
$check = mysql_query("SELECT * FROM users WHERE username='".$username."' AND password= '".$password."'");
$rows = mysql_num_rows($check);
if(mysql_num_rows($check)!=0)
{
header("location: /main.php");
exit();
}
else
{
$error = "Couldn't find username.";
}
}
else
{
echo "Please fill all the fields.";
}
}
?>
<html>
<head><h1>Login Page</h1>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<center>
<?php echo $error; ?>
<form action = "login.php" method = "POST">
Username: <input type="text" name="username"> <br /><br/>
Password: <input type="password" name="password"> <br /><br/>
<input type="submit" value="Login" name="submit">
</form>
</center>
</body>
</html>
首先需要进行PHP处理,因为标题(“location:...如果内容已经发送,将无效。
答案 1 :(得分:0)
在登录页面中,在您完成登录用户的操作后,您可以使用header()
功能:
header("Location: index.php")
答案 2 :(得分:0)
尝试使用此...
if(mysql_num_rows($check) > 0)
{
header("Location: index.php");
}
并注意在创建登录页面时始终使用mysql_real_escape_string
..它可以避免SQL注入。