首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Php = $_SESSION =>无法显示消息

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

Stack Overflow用户
提问于 2017-04-28 18:34:18
回答 1查看 1.1K关注 0票数 1

我无法设法在我的$_SESSION文件中显示带有index.php的自定义消息。

不过,只要有一个简单的警报就行了..

这里是我的action.php

代码语言:javascript
运行
复制
<?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的一部分:

代码语言:javascript
运行
复制
<?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中显示我的信息.

EN

Stack Overflow用户

回答已采纳

发布于 2017-04-28 18:47:03

需要考虑的是:-

1.如果要处理页面上的session_start();,则需要在每个php页面的顶部设置SESSION。因此,将其添加到您的两个页面中(在启动<?php之后)。

2.

代码语言:javascript
运行
复制
header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';

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

所以它需要如下所示:

代码语言:javascript
运行
复制
$_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'])): ?>

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43686800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档