成功登录后 php 重定向


Php redirect after successful login

我在使用简单的验证文件时遇到问题,成功登录后它不会重定向到索引页面。

基本上,登录.php文件具有用于登录的html表单,表单调用auth.php文件,该文件已经具有登录数据并决定您的登录名和密码是否正确。现在它应该重定向到 index.php成功登录后,但它没有,相反,它只是清理登录.php文件中的表单,然后您继续尝试,但是如果您刷新页面(成功登录后),您将自动重定向到索引页面。

修复了!将代码更改为比这更简单的代码。

if($logindata[$_POST["username"]]==$_POST["password"])

这个位看起来不正确;也许你正在寻找:

if($logindata[$_POST["password"]]==$_POST["password"])

有时由于某些原因,标头不能很好地工作,而是尝试使用简单的 html 重定向,如下所示:

<?php
$usernames = array("user1", "user2");
$passwords = array("pass1", "pass2");
$page = "index.php";
for($i=0;$i<count($usernames);$i++){  
    $logindata[$usernames[$i]]=$passwords[$i]; 
}
    $found = 0; 
    for($i=0;$i<count($usernames);$i++) {    
        if ($usernames[$i] == $_POST["username"])    {    
        $found = 1;    
        } 
        } 
        if ($found == 0) {   
            $redirect_url = "./login.php?login_error=1"
        }
if($logindata[$_POST["username"]]==$_POST["password"]) {        
    session_start();    
    $_SESSION["username"]=$_POST["username"]; 
$redirect_url = "./index.php"
    } 
    else {    
        $redirect_url = "./login.php?login_error=2"
    }
    echo "<center><br><br><br><p>You will be redirected in about 2 seconds. If not, click this link: <a href='$redirect_url'>Back</a></p></center>";
    ?>
     <html>
            <head>
            <meta http-equiv="refresh" content="2;url='<?php echo "$redirect_url"; ?>'/>
        <title>Redirecting...</title>
            </head>
            </html> 
<?php
exit;
?>

我假设重定向位置位于 php 文件的同一文件夹中。调整它们不是的变量 $redirect_url 路径。