我无法设法在我的$_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中显示我的信息.
发布于 2017-04-28 18:47:03
需要考虑的是:-
1.如果要处理页面上的session_start();,则需要在每个php页面的顶部设置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'])): ?>
https://stackoverflow.com/questions/43686800
复制相似问题