繁体   English   中英

成功登录/或登录失败后重定向

[英]Redirect after successfull login /or failed login

为我的页面我创建了一个登录。 它按计划完成所有工作,但是如何在登录成功时自动将其重定向到另一个页面,如果不是,则如何将其重定向到另一个页面?

不同的主题,我是这个页面的新手,如何自动为引用创建四个空格? 在每条线前面手动放置四个空格似乎有点奇怪。 谢谢你的帮助:)克里斯

    <?php session_start()?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>checklogin</title>
</head>

<body>

<?php
$con = mysqli_connect("mysql.freehostingnoads.net","u291_admin","...","u291_login");
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }



$sql= "SELECT * FROM login_data";
$result= mysqli_query ($con, $sql);
$row= mysqli_fetch_array($result);
if($row["username"] == $_POST["user"] && $row["password"] == $_POST["pwd"]){

    $_SESSION['username'] =$_POST["user"];


    echo "You succesfully logged in";
   echo '<br /> If you are not redirected automatically please click <a href="showdata.php">here</a>';
    //header("location:hidden.php");


}
else
{   echo "Wrong username or password";
    // header("location: www./index.html");
}



?>  

</body>
</html>

你已经找到了

header("location:index.html");

但问题是,您的标头已经发送。 在此之前你不应该回应任何事情。

如果您已经输出标题,那么javascript重定向可能是您的解决方案

<script type="text/javascript">
window.location = "http://www.google.com/"
</script>
if($row["username"] == $_POST["user"] && $row["password"] == $_POST["pwd"]){
    $_SESSION['username'] =$_POST["user"];
    header("location:showdata.php");   
}
else
{    
    header("location: index.html");
}

您可以使用刷新的客户端元标记:

<?php
     echo "You succesfully logged in";
     echo '<br /> If you are not redirected automatically please click <a href="showdata.php">here</a>';
?>
    <meta http-equiv="refresh" content="5;URL=http://someSite.com/hidden.php" />

<?php   
     }
     else{
          echo "Wrong username or password";
?>
     <meta http-equiv="refresh" content="5;URL=http://someSite.com/" />
<?php
     }        
 ?>

一个解决方案是重写你的代码w / MVC像laravel,然后你可以做类似的事情

... UsersController ...

public function getLogin(){
    if(isset(_SESSION['username']){
        Redirect::back()->with('message', 'Login Successful');
    } else {
        Redirect::to('/')->with('message', 'Login failed');
    }
}

一个好处是所有数据库连接逻辑都在Laravel雄辩的ORM中得到解决,它还有很多安全措施,并且内置了用户身份验证系统,因此您实际上不会使用会话数据。 还有其他MVC,以及像Sentry一样可以使用的完整用户登录系统。 如果你想快速建立一个应用程序,你可能需要选择一个mvc。

暂无
暂无

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

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