【发布时间】:2010-05-08 18:17:20
【问题描述】:
我有以下页面:page1.php、page2.php 和 page3.php。它们每个中的代码如下
代码:
page1.php
<script type="text/javascript">
$(function(){
$('#imgID').upload({
submit_to_url: "page2.php",
file_name: 'myfile1',
description : "Image",
limit : 1,
file_types : "*.jpg",
})
});
</script>
<body>
<form action="page3.php" method="post" enctype="multipart/form-data" name="frm1" id="frm1">
//Some other text fields
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
page2.php
<?php session_start();
$a = $_SESSION['a'];
$b = $_SESSION['b'];
$c = $_SESSION['c'];
$res = mysql_query("SELECT col FROM table WHERE col1 = $a AND col2 = $b AND col3 = $c LIMIT 1");
$num_rows = mysql_num_rows($res);
echo $num_rows; //echos 0 when in fact it should have been 1 because the data in the Session exists.
//Ok let's proceed further
//... Do some stuff...
//Store some more values and create new session variables (and assume that page1.php is going to be able to use it)
$_SESSION['d'] = 'd';
$_SESSION['e'] = 'e';
$_SESSION['f'] = 'f';
if (move_uploaded_file($_FILES['file']['tmp_name'], $file))
{
echo "success";
}
else
{
echo "error ".$_FILES['file']['error'];
}
?>
page3.php
<?php session_start();
if( isset($_POST['submit']) )
{
//These sessions are non-existent although the AJAX request
//to page2.php may have created them when called via AJAX from within page1.php
echo $_SESSION['d'].$_SESSION['e'].$_SESSION['f']; ?>
}
?>
正如代码所说,我通过 AJAX 调用从 page1.php 到 page2.php 发布了一些信息。 page2.php 应该能够使用 page1.php 中的会话值,即 $_SESSION['a']、$_SESSION['b'] 和 $_SESSION['c'] 但它不能。为什么?我该如何解决这个问题?
page2.php 在完成一些处理后正在创建更多会话,并将响应发送回 page1.php。 page1.php 上的表单提交按钮被点击,页面被 POST'ed 到 page3.php。但是,当在 page2.php 中创建的 SESSION 信息被回显时,它是空白的,表示没有使用 page2.php 中的 SESSIONS。我该如何解决这个问题?
在得出上述结论之前,我查看了很多信息并花了大约 50 个小时尝试用我的脚本做不同的事情。我的应用程序。是使用函数(不是 OOPS)定制的,不使用任何 PHP 框架,我什至不打算使用任何框架,因为我对 OOP 概念的了解有限,任何许多框架都是面向对象的。我遇到了比赛条件,但提供的解决方案并没有太大帮助。使用 DB 保存会话并从 DB 中查找和检索的另一种解决方案是我最不想做的事情,我真的想避免为一项任务创建表、编码和维护代码,就像在同一域中跨页面保持会话一样简单.
所以我的要求是:在目前的情况下,有没有办法通过简单的编码来解决上述问题?任何帮助表示赞赏。
谢谢。
编辑: 此外,我在会话中包含敏感信息,我无法使用 GET 或 POST 通过页面来回传递。所以使用 SESSIONS 对我来说真的很有帮助。因此,如果任何编码可以帮助我实现这一目标,那将非常有帮助。谢谢。
【问题讨论】: