Php = $_SESSION => 无法显示消息
Php = $_SESSION => cannot display the message
我无法在我的 index.php
文件中显示带有 $_SESSION
的自定义消息..
尽管有一个简单的警报,但效果很好..
这是我的 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.session_start();
如果您要处理该页面上的 SESSION
,则需要在每个 php 页面的顶部。所以将它添加到您的两个页面中(刚开始 <?php
)。
2.
header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';
这是不正确的,因为您已经通过 header()
重定向了。 Hens 下一行将不会执行。
所以它需要像下面这样:-
$_SESSION['msg'] = 'Your account is already activated !';
header('location: ../?page=home');
3.I 认为此语法:- $user_id => $getId = $_GET['log'];
不正确。它需要是 $user_id = $getId = $_GET['log'];
。 (我不确定,因为我从来没有见过你使用的语法)
4.Instead of <?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?>
你可以简单地使用 <?php if (!empty($_SESSION['msg'])): ?>
我无法在我的 index.php
文件中显示带有 $_SESSION
的自定义消息..
尽管有一个简单的警报,但效果很好..
这是我的 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.session_start();
如果您要处理该页面上的 SESSION
,则需要在每个 php 页面的顶部。所以将它添加到您的两个页面中(刚开始 <?php
)。
2.
header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';
这是不正确的,因为您已经通过 header()
重定向了。 Hens 下一行将不会执行。
所以它需要像下面这样:-
$_SESSION['msg'] = 'Your account is already activated !';
header('location: ../?page=home');
3.I 认为此语法:- $user_id => $getId = $_GET['log'];
不正确。它需要是 $user_id = $getId = $_GET['log'];
。 (我不确定,因为我从来没有见过你使用的语法)
4.Instead of <?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?>
你可以简单地使用 <?php if (!empty($_SESSION['msg'])): ?>