'$_Session variable lost between php file
I got this error when trying to do the forgot password function and got this error: Undefined variable $_Session I start the session and in the config file but somehow the session still lost after I enter the verification code send to the email.
Here is the code of my file. If you need more detail I will add it. Thank you in advance.
// Forget Password
<?php
session_start();
ob_start();
?>
<div class="right">
<form action="" method="post" id="formDemo" class="form">
<div class="form-login">
<table>
<tr>
<?php
require_once 'classes/Users.php';
if (isset($_POST['submit'])) {
$error = array();
$email = $_POST['email'];
if ($email == '') {
$error['email'] = 'Không được để trống';
}
if (empty($error)) {
$result = $users->getUserEmail($email);
$code = substr(rand(0, 999999), 0, 6);
$title = 'Quên mật khẩu';
$content = "Mã xác nhận của bạn là: <span style='color:green'>" . $code . "</span>";
$mail->sendMail($title, $content, $email);
$_SESSION['email'] = $email;
$_SESSION['code'] = $code;
echo '<script>location.href = "./?p=verification";</script>';
}
}
?>
</tr>
<tr>
<p style="margin-bottom: 20px" class="title">Forget Password</p>
</tr>
<tr>
<input type="email" class="control-login" name="email" placeholder="Enter email">
<span style="color: red"><?php if (isset($error['email'])) echo $error['email'] ?></span><br><br>
</tr>
<tr><br>
<div class="login">
<button class="login btn btn-light" type="submit" name="submit">Submit</button>
</div>
</tr>
</table>
</div>
</form>
</div>
// Verification
<div class="right">
<form action="verification.php" method="post" id="formDemo" class="form">
<div class="form-login">
<table>
<tr>
<p style="margin-bottom: 20px" class="title">Enter verification code</p>
</tr>
<tr>
<?php if (isset($_POST['submit'])) {
$error = array();
if ($_POST['text'] != $_SESSION['code']) {
$error['fail'] = 'Mã xác nhận không hợp lệ';
} else {
echo '<script>location.href = "./?p=resetPass";</script>';
}
}
?>
</tr>
<tr>
<?php if (isset($error['fail'])): ?>
<div class="alert alert-primary" role="alert">
<?= $error['fail'] ?>
</div>
<?php else : ?>
<div class="alert alert-primary" role="alert">
Please enter the verification code we send to your email
</div>
<?php endif ?>
</tr>
<tr>
<input type="text" class="control-login" name="text" placeholder="Verification code">
</tr>
<tr><br><br>
<div class="login">
<button class="login btn btn-light" type="submit" name="submit">Send</button>
</div>
</tr>
</table>
</div>
</form>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
