Php = $ _SESSION =>无法显示消息

时间:2017-04-28 18:34:19

标签: php session display msg

我无法在$_SESSION文件中使用index.php显示自定义消息..

尽管有一个简单的警报,但效果很好。

这是我的action.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');
}

这是我的index.php的一部分

<?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; ?>

一切都很好,事情是,我只是无法在index.php中显示我的信息......

1 个答案:

答案 0 :(得分:2)

需要考虑的事项: -

如果您打算在该页面上处理session_start();,则每个php页面顶部需要

1。SESSION。所以将它添加到你的两个页面中(刚开始<?php后)。

2

header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';

这是不正确的,因为您已经通过header()进行了重定向。母鸡下一行不会执行。

所以它需要如下所示: -

$_SESSION['msg'] = 'Your account is already activated !';
header('location: ../?page=home');

3.我认为这种语法: - $user_id => $getId = $_GET['log'];不正确。它必须是$user_id = $getId = $_GET['log'];。 (我不确定,因为我从来没有见过像你那样使用的语法)

4.您只需使用<?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?>

,而不是<?php if (!empty($_SESSION['msg'])): ?>