Php=$\u SESSION=>;无法显示该消息

Php=$\u SESSION=>;无法显示该消息,php,session,display,msg,Php,Session,Display,Msg,我无法在index.php文件中显示带有$\u会话的自定义消息 不过,它与简单的警报配合使用效果很好 这是我的行动。php: <?php require '../includes/config.inc.php'; require '../lib/DB.php'; $dbh = DB::getInstance(); // Getting the requested variables for the activation $id = $_GET['log']; $key = $_GET[

我无法在
index.php
文件中显示带有
$\u会话的自定义消息

不过,它与简单的警报配合使用效果很好

这是我的行动。php

<?php
require '../includes/config.inc.php';
require '../lib/DB.php';
$dbh = DB::getInstance();


// Getting the requested variables for the activation
$id = $_GET['log'];
$key = $_GET['key'];

// Getting the key in the table user => called : activationKey

$sql = "SELECT activationKey,activated FROM users WHERE id = :id";
$result = $dbh->prepare($sql);
$result->execute(['id' => $id]);
$user = $result->fetchObject();
$count = $result->rowCount();

if($count > 0) {
    $keyDB = $user->activationKey;  // Getting the key
    $activatedUser = $user->activated; // Activated statut (0 or 1)

    // We check if the user is activated
    if($activatedUser == '1') // If user is already activated
    {
        header('location: ../?page=home');
        $_SESSION['msg'] = 'Your account is already activated !';
    }
    else // Else
    {
        if($key == $keyDB) 
        // The two keys from database and the link ($_GET['key'];)
        // are being compared
        {
            // If they match => account is being activated
            // We switch "activated" value to 1 inside the database.
            $sql="UPDATE users SET activated = 1 WHERE id = :id";
            $result=$dbh->prepare($sql);
            $result->execute(['id' => $id]);
            $_SESSION['msg'] = 'Your  account has been activated!';
            header('location: ../?page=home');
        }
        else // Else : if the two keys don't match
        {
            $_SESSION['msg'] = 'Issue : your  account cannot be activated!';
            header('location: ../?page=home');
        }
    }
}
else //$count is equal to 0 => User is not in the database.
{
    $_SESSION['msg'] = 'There is a problem with your account. Please contact the administrator of the website.';
    header('location: ../?page=home');
}
<?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?>
<div class="alert alert-success" role="alert">
    <strong><?php echo $_SESSION['msg'] ?></strong>
</div>
<?php unset($_SESSION['msg']);
endif; ?>

需要考虑的事情:-


1.
session_start()会话
,则需要在该页面顶部添加code>。因此,请将它添加到您的两个页面中(刚刚启动
您是吗?index.php中的
session\u start
在哪里?另一个文件中。这是一个包含的文件。编辑:是的,会话已启动。您需要在每个文件的顶部启动session\u start()。在某些情况下,您正在重定向(头…)在你设置会话MSG之前。谢谢你Waterlomatt!!!这是我的问题,我没有设置会话\u启动我的操作文件!谢谢大家!谢谢你们,我记下了。编辑:对于第3点,这只是一个私人评论,不要注意:)@Finalmix6很高兴帮助你。:)
header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';
$_SESSION['msg'] = 'Your account is already activated !';
header('location: ../?page=home');