简体   繁体   English

登录成功后重定向到PHP

[英]Php redirect after successful login

I'm having an issue with a simple verification file, it doesn't redirect to index page after successful login. 我在使用简单的验证文件时遇到问题,成功登录后不会重定向到索引页。

Basically the login.php file has the html form for login, the form calls auth.php file which already has the login data and decides if your login and password is correct or not. 基本上,login.php文件具有用于登录的html表单,该表单调用已具有登录数据的auth.php文件,并确定您的登录名和密码是否正确。 Now it should redirect to index.php after successful login but it doesn't , instead it just cleans up the form in the login.php file and you keep trying , BUT if you refresh the page ( after successful login ) you get auto redirected to index page. 现在,它应该在成功登录后重定向到index.php,但是不会,相反,它只是清除login.php文件中的表单,您可以继续尝试,但是如果刷新页面(成功登录后),则会自动重定向索引页面。

Fixed! 固定! changed the code to something even simpler than that. 将代码更改为比这更简单的代码。

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

This bit doesn't look correct; 这一点看起来不正确; maybe you were looking for: 也许您正在寻找:

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

Sometimes headers does not work well for some reasons, instead try to use a simple html redirect like this: 有时,由于某些原因,标头不能很好地工作,而是尝试使用简单的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;

?>

I presumed the redirect location is in the same folder of the php file. 我认为重定向位置在php文件的同一文件夹中。 Adjust the var $redirect_url path of they aren't. 调整var $ redirect_url路径不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM