No puedo mostrar mi mensaje personalizado con $_SESSION dentro de mi archivo index.php .
Sin embargo, funciona muy bien con una simple alerta.
Aquí está mi acción.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'); }Y aquí hay una parte de mi 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; ?>Todo funciona muy bien, la cuestión es que simplemente no puedo mostrar mi mensaje dentro de mi index.php...
las cosas deben tenerse en cuenta: -
1. session_start(); necesario en la parte superior de cada página php si va a tratar con SESSION en esa página. así que agréguelo en ambas páginas (justo después de iniciar <?php ).
2.
header('location: ../?page=home'); $_SESSION['msg'] = 'Your account is already activated !'; Es incorrecto porque ya redirigió a través de header() . La siguiente línea de Hens no se ejecutará.
Por lo tanto, debe ser como a continuación: -
$_SESSION['msg'] = 'Your account is already activated !'; header('location: ../?page=home'); 3.Creo que esta sintaxis: - $user_id => $getId = $_GET['log']; Es incorrecto. Debe ser $user_id = $getId = $_GET['log']; . (No estoy seguro porque nunca vi una sintaxis como esa que usaste)
4. En lugar de <?php if (isset($_SESSION['msg']) && $_SESSION["msg"] !== 0): ?> simplemente puede usar <?php if (!empty($_SESSION['msg'])): ?>