I cannot manage to display my custom message with $_SESSION inside my index.php file..
It works great with a simple alert though..
Here is my 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');
}
And here is a part of my 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; ?>
Everything is working great, the thing is, I just cannot show my message inside my index.php...
things need to consider:-
1.session_start(); needed on top of each php page if you are going to deal with SESSION on that page. so add it in your both page (just after starting <?php).
2.
header('location: ../?page=home');
$_SESSION['msg'] = 'Your account is already activated !';
It's incorrect because you have already redirected through header(). Hens next line will not execute.
So it need to be like below:-
$_SESSION['msg'] = 'Your account is already activated !';
header('location: ../?page=home');
3.I think this syntax:- $user_id => $getId = $_GET['log']; is incorrect. It need to be $user_id = $getId = $_GET['log'];. (I am not sure because i never seen syntax like that what you used)
4.Instead of <?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?> you can simply use <?php if (!empty($_SESSION['msg'])): ?>