【发布时间】:2019-05-11 19:13:37
【问题描述】:
编辑:在做了一些反复试验后,我注意到如果我评论以下代码:
$success = $_SESSION["success"];
session_unset($_SESSION["success"]);
$to 变量按预期显示。所以我的问题是为什么不能同时使用两者?
原问题:
我正在尝试使用 mail() 函数发送电子邮件。为了做到这一点,我将变量 $_SESSION['emailfrompass'] 传递到 2 个页面,但由于某种原因,该变量始终为空。即使我通过消息发送了另一个变量并且我在接收它时没有问题,但这是唯一产生问题的变量
session_start() 在所有页面中设置。我尝试使用
ini_set('display_errors',1); error_reporting(E_ALL);
用于发现问题但没有用。变量始终为空
这是我的 enterEFPass.php 文件。最重要的是我有
<?php
session_start();
ini_set('display_errors',1);
error_reporting(E_ALL);
?>
<?php
if(isset($_SESSION["success"]))
{
$success = $_SESSION["success"];
session_unset($_SESSION["success"]); //echoing this will display the right thing
$to = "";
$to = isset($_SESSION['emailforpass']) ? $_SESSION['emailforpass'] : 'not found';
echo $to; //does not work
//mail($to, "Reset your password", $message, $headers);
?>
<div id='alert'>
<div class='alert alert-block alert-info'>
<?php
echo $success;
// echo "<script>setTimeout(\"location.href = 'login-page.php';\",2500);</script>";
?>
</div>
</div>
<?php
}?>
这是我实例化 $_SESSION 变量的 enterEFPass_route.php
<?php
session_start();
include 'db.php';
$email = "";
$conn = Connect();
if (isset($_POST['send_email_button']))
{
$email = mysqli_real_escape_string($conn, $_POST['email']); //$_POST['email'] the field where I introduce the email
}
if (empty($email)) { array_push($errors, "Please enter a valid email"); }
if (count($errors)==0) // just an array for error messages
{
$_SESSION['emailforpass'] = $email; //appears empty always
$_SESSION['success'] = "An email has been sent to the corresponding address. Please follow the instructions provided there"; //goes without problems
header("location: enterEFPass.php");
} else
{
$_SESSION['errormsgpassress']= $errors; //no problems in sending
header("location: enterEFPass.php");
}
}
?>
我希望 $to 变量可以打印在屏幕上,但它总是打印“未找到” 如果我打印 $_SESSION['success'] 那就没有问题了
【问题讨论】:
-
db.php文件的用途是什么?是否包含连接数据库的mysqli_connect()函数? -
@MatthewVanlandingham 是的,该文件用于连接
标签: php mysql session-variables