简体   繁体   English

登录后如何将用户重定向到正确的页面?

[英]How to redirect the user to their proper pages after login?

I want to redirect the user to their proper main page. 我想将用户重定向到他们正确的主页。 I also want to keep the sessions that I have know in my login.php . 我也想将我所知道的会话保留在我的login.php In my users table I store the username, password and type. 在我的users表中,我存储用户名,密码和类型。

login.php login.php

<?php

require ('connect.php');
if (isset($_POST['submit'])) {

$username = $_POST['username'];
$password = $_POST['password'];


if ($username && $password) {
    $check = mysql_query("SELECT * FROM users WHERE username='".$username."' AND password= '".$password."'");
    $rows = mysql_num_rows($check);


    if(mysql_num_rows($check) != 0){

      session_start();
      $run_login =mysql_fetch_array($check);
      $uid = $run_login['id'];
      $_SESSION['uid'] = ['uid'];
      $_SESSION['username']=$_POST['username'];
      header("location:../../statistics/home.php");

    }
    else{
      die("Could not find the Username or password.");
   }
}
 else {
     echo "Please fill all the fields.";
 }
}
?>

A. Method 1 A.方法1

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the declaration, for example). 您可以使用header()函数发送新的HTTP标头,但是必须在任何HTML或文本之前(例如,在声明之前)将其发送到浏览器。

header('Location: '.$newURL); header('Location:'。$ newURL);

B. Method 2 B.方法2

die() 死()

header("Location: http://example.com/myOtherPage.php "); header(“ Location: http : //example.com/myOtherPage.php ”);

die(); 死();

C. Method 3 C.方法3

This function doesn't incorporate the 303 status code: 此功能未包含303状态代码:

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);

    exit();
}

Redirect('http://example.com/', false);

This is more flexible: 这更加灵活:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}

For More Information please refer : How to make a redirect in PHP? 有关更多信息,请参见: 如何在PHP中进行重定向?

put die(); 把die(); after header("location:../../statistics/home.php"); 标头之后(“ location:../../ statistics / home.php”); and check session in home.php 并检查home.php中的会话

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

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