我正在尝试重定向我的页面,如果登录是真的,也是新的会话,所以它应该工作,如果没有输入详细信息它应该显示错误消息。请帮我这个代码。
<?php
if(isset($_SESSION['validate'])){
if(isset($_POST['Username'])){
$username=$_POST['Username'];
$password=$_POST['Passwd'];
if($username=='admin' && $password=='admin'){
$_SESSION['validate']=true;
echo "logged in..";
header('location:127.0.0.1/php/admin.php');
exit(0);
}
else{
echo "Wrong Password";
}
}
else{
echo "Set fields please..";
}
}
?>
<html>
<head><title>Login</title></head>
<body>
<br><br><br><br><br><br><br><br><br>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<?php session_start(); ?>
<table border="0" align="center">
<tr>
<td>Username:</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="Password" name="passwd"></td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" name="submit"></center></td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:1)
<?php
if(isset($_SESSION['validate'])){
echo "you are already logged in!";
}
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$username=$_POST['Username'];
$password=$_POST['Password'];
if(!empty($username) && !empty($password)){
if($username=='admin' && $password=='admin'){
$_SESSION['validate']=true;
//echo "logged in.."; //not required because after redirecting it will stateless.
header('location:127.0.0.1/php/admin.php');
exit(0);
}
else{
echo "Wrong Credential";
}
}
else{
echo "Set fields please..";//it will appear if credentials are empty..
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<br><br><br><br><br><br><br><br><br>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<?php session_start(); ?>
<table border="0" align="center">
<tr>
<td>Username:</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="Password" name="password"></td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" name="submit"></center></td>
</tr>
</table>
</form>
</body>
</html>
答案 1 :(得分:0)
<?php
session_start();
if(isset($_POST['Username'])){
$username=$_POST['Username'];
$password=$_POST['passwd'];
if($username == "admin" && $password=="admin"){
$_SESSION['validate'] = true;
echo "logged in..";
header('location: http://127.0.0.1/php/admin.php');
exit(0);
}
else{
echo "Wrong Password";
}
}
else{
echo "Set fields please..";
}
?>
<html>
<head><title>Login</title></head>
<body>
<br><br><br><br><br><br><br><br><br>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<table border="0" align="center">
<tr>
<td>Username:</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="Password" name="passwd"></td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" name="submit"></center></td>
</tr>
</table>
</form>
</body>
</html>
删除if(isset($ _ SESSION ['validate']))
导致变量未在您的代码中定义
这对我有用