无法在PHP_SELF正在运行的情况下重定向它


Unable to redirect it with PHP_SELF in action

如果登录是真的,并且也是新的会话登录,我会尝试重定向我的页面,所以它应该像没有输入详细信息一样工作,它应该显示一条错误消息。请帮我处理这个代码。

<?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>
    <?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>
<?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'])
因为您的代码中没有定义变量
这适用于我