简体   繁体   English

登录后重定向到用户页面

[英]Redirect to user page after login

Changed my code to the following thanks to the tips. 由于提示,我的代码更改为以下代码。 But I'm still not redirected to the user.php. 但是我仍然没有重定向到user.php。 Added the variable $rowcount and give it a value. 添加了变量$rowcount并为其赋予一个值。 If the query has a value of a user it have to be redirected to the user.php page. 如果查询具有用户值,则必须将其重定向到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);
}

Change $logged_in with your php stracture 用您的php结构更改$ logged_in

Add session_start() at the top of your page. 在页面顶部添加session_start() Also change the following code. 同时更改以下代码。

session_start();
..
..

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

In user.php, first check whether user is logged in or not. 在user.php中,首先检查用户是否已登录。 For that write a simple function - 为此,编写一个简单的函数-

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

If return FALSE, redirect back to Login page. 如果返回FALSE,请重定向回到“登录”页面。

Your $rowcount must to be declared at outside of the "if": 您的$ 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