簡體   English   中英

登錄后重定向到用戶頁面

[英]Redirect to user page after login

由於提示,我的代碼更改為以下代碼。 但是我仍然沒有重定向到user.php。 添加了變量$rowcount並為其賦予一個值。 如果查詢具有用戶值,則必須將其重定向到user.php頁面。

<?php
    include("inc/header.php");
?>

<?php
    if(isset($_POST["submit"])) {

    $username = trim($_POST["username"]);
    $password = trim($_POST["password"]);

        if($username == "" && $password == "") {
            echo "Please fill in all the details";
            exit;
        }

        if($username == "admin" &$password == "test") {
            $_SESSION["admin"] = true;
            header("location: admin-panel.php");
        } 

        $rowcount = 0;
        $password_secure = md5($password);
        if($username != "" && $password != "") {
            $sql = "SELECT * FROM user WHERE username = '".mysqli_escape_string($connection, $username)."' 
            AND password = '".mysqli_escape_string($connection, $password_secure)."'";
            $query = mysqli_query($connection, $sql);
            $rowcount = mysqli_num_rows($query);
        } else {
            echo "Username of password was not right, please try again.";
        }

        if($rowcount != 0) {
            $row = mysql_fetch_array($connection, $query);
            $_SESSION["username"] = $row["username"];
            $_SESSION["login"] = true;
            header("location: user.php");
            exit;
        }
    }
?>

<?php
    include("inc/footer.php");
?>
// if logged in, redirect towards user account
if($logged_in) {
    header("Location: useraccount.php");
    exit(0);
}

用您的php結構更改$ logged_in

在頁面頂部添加session_start() 同時更改以下代碼。

session_start();
..
..

if($rowcount == 1) 
{
   while($row = mysqli_fetch_array($query))
   {
        $_SESSION["username"] = $row["username"];
        $_SESSION["login"] = true;
   }
   header("location: user.php");
}

在user.php中,首先檢查用戶是否已登錄。 為此,編寫一個簡單的函數-

function is_loggedin()
{
    if(isset($_SESSION['username']) && isset($_SESSION['login']))
        return TRUE;
    else
        return FALSE;
}

如果返回FALSE,請重定向回到“登錄”頁面。

您的$ rowcount必須在“ if”之外聲明:

$rowcount=0;
if($username != "" && $password != "") {
    $sql = "SELECT * FROM user WHERE username = '".$username."' 
            AND password = '".$password_secure."'";
    $query = mysqli_query($connection, $sql);
    $rowcount = mysqli_num_rows($query);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM