【问题标题】:Change text upon redirect from certain page...?从特定页面重定向时更改文本...?
【发布时间】:2020-04-07 12:07:04
【问题描述】:

正如标题所示,我正在尝试使用相同的 .php 页面,并让它在从特定位置重定向时显示一些新内容。

在上下文中... 我有一个登录名,成功登录后会重定向到主页,但如果不成功,则会重定向到索引。有没有办法让我的索引页面在从我的登录页面重定向时显示“登录错误”消息?

这是我的登录代码...

<?php
session_start();
include('conn.php');

$query = "SELECT * FROM User";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

if (isset($_POST["submit"])) {

    $logEmail = $conn->real_escape_string($_POST['logEmail']);
    $logPass = $conn->real_escape_string($_POST['logPass']);

    $checkuser = "SELECT * FROM User WHERE Email='$logEmail' AND UserPassword=AES_ENCRYPT('$logPass', 'MyKey')";
    $userresult = mysqli_query($conn, $checkuser) or die(mysqli_error($conn));
    $loginsucc = (mysqli_num_rows($userresult) > 0);
    if (mysqli_num_rows($userresult) > 0) {

        while ($row = mysqli_fetch_assoc($userresult)) {

            $userPriKey = $row['UserID'];
            $userid = $row['Email'];
            $accounttype = $row['IsAdmin'];
            $firstname = $row['FirstName'];
            $surname = $row['LastName'];

            $_SESSION['userPriKey'] = $userPriKey;
            $_SESSION['name'] = $firstname;
            $_SESSION['surname'] = $surname;
            $_SESSION['Email'] = $userid;
            $_SESSION['IsAdmin'] = $accounttype;

            if($accounttype == '1'){
                header("Location: home.php");
            }else if ($accounttype == '0'||$accounttype == NULL ) {
                header("Location: userhome.php");
            }
        }
    } else {
        header("Location: index.php");
    }
}

?>

【问题讨论】:

标签: php html authentication redirect


【解决方案1】:

在调用header() 之前,像这样设置会话变量

$_SESSION['msg'] = 'success you are logged in';
header('Location: page.php');
exit; 

然后在page.php中,

session_start();
if (isset($_SESSION['msg'])) {
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
}

另外仅供参考,您应该使用准备好的语句。您的代码并不完全安全

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 2020-09-03
    • 2013-06-12
    • 1970-01-01
    • 2016-05-03
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多