重定向丢失的开机自检/会话数据
Redirect losing POST/SESSION DATA
我在重定向方面遇到问题,我的整个代码都在工作,我唯一的问题是在此过程中丢失了 POST/SESSION 数据。 花了无数个小时使用它并尝试了很多解决方法,但它仍然不起作用,这是我唯一的问题。 这是我的代码
<?php
// First we execute our common code to connection to the database and start the session
require("common.php");
// This variable will be used to re-display the user's username to them in the
// login form if they fail to enter the correct password. It is initialized here
// to an empty value, which will be shown if the user has not submitted the form.
// This if statement checks to determine whether the login form has been submitted
// If it has, then the login code is run, otherwise the form is displayed
if(!empty($_POST)) {
// This query retreives the user's information from the database using
// their username.
if(isset($_POST['validEmail'])) {
$query = "SELECT *
FROM registered_email
WHERE email = :validEmail";
}
// The parameter values
$query_params = array( ':validEmail' => $_POST['validEmail'] );
try {
// Execute the query against the database
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex) {
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query");
}
// This variable tells us whether the user has successfully logged in or not.
// We initialize it to false, assuming they have not.
// If we determine that they have entered the right details, then we switch it to true.
$login_ok = false;
// Retrieve the user data from the database. If $row is false, then the username
// they entered is not registered.
$row = $stmt->fetch();
if($row) {
if($_POST['validEmail'] === $row['email']) {
// If they do, then we flip this to true
$login_ok = true;
}
}
// If the user logged in successfully, then we send them to the private members-only page
// Otherwise, we display a login failed message and show the login form again
if($login_ok) {
$_SESSION['sesEmail'] = $row;
// Redirect the user to the private members-only page.
if (isset($_POST['validEmail'])) {
echo "<script>location='http://www.url.com.ph/some.php'</script>";
}
} else {
// Tell the user they failed
print "Sorry to say that your Email is not Registered!.";
}
}
理想情况下,您的代码应如下所示,据我所知,它应该可以正常工作。我重构了您的代码并编辑了重定向语句。
// I am assuming you have session_start(); included in common.php
require("common.php");
if(!empty($_POST)) {
if(isset($_POST['validEmail'])) {
$query = "SELECT *
FROM registered_email
WHERE email = :validEmail";
$query_params = array( ':validEmail' => $_POST['validEmail'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex) {
die("Failed to run query");
}
$login_ok = false;
$row = $stmt->fetch();
if($row) {
if($_POST['validEmail'] === $row['email']) {
$login_ok = true;
}
}
if($login_ok) {
$_SESSION['sesEmail'] = $row;
if (isset($_POST['validEmail'])) {
// the page where you are redirecting should be linked with session as well
echo "<script>window.location.href='http://www.url.com.ph/some.php'</script>";
}
} else {
// Tell the user they failed
print "Sorry to say that your Email is not Registered!.";
}
}
else {
// Tell the user they failed
print "Sorry no POST parameters!.";
}
}
希望对您有所帮助。如果没有,请随时讨论。
相关文章:
- 如何将会话数据从集合传递到视图?(Backbone JS/Coffeescapept)
- 如何在 Blueimp jquery 文件上传器中发送 I 会话数据
- 在 javascript 函数中添加 php 会话数据
- “location.reload()” 丢失了开机自检/会话数据?(F5 / Ctrl + R保留数据?
- Windows 8.1 应用中应用数据和会话数据之间的差异
- 如何在.js文件的javascript函数中获取php会话数据
- 如何安全地存储和访问用户会话数据
- 从会话数据生成的临时 PDF 创建临时 URL
- 烧瓶:如何手动存储会话数据
- 流星 #each 和会话数据
- 为什么在php脚本中存储会话数据会干扰Ajax脚本
- 如何通过chrome扩展获取特定页面的会话数据
- 提取序列化的用户会话数据
- Socket.io授权功能未更新会话数据
- 为客户端显示存储的会话数据时,正确的事件链是什么
- Ember.js中的简单会话数据
- 在javascript中访问用户会话数据的麻烦
- 在Node JS上存储实时画布会话数据
- 在web应用程序中存储大型会话数据的最佳方式是什么?
- 如何将会话数据存储在本地HTML站点上