【问题标题】:PHP not passing SOME session variables to another scriptPHP没有将一些会话变量传递给另一个脚本
【发布时间】:2015-03-20 04:48:15
【问题描述】:

我疯了,试图理解为什么下面的脚本(我们称之为 script1 将会话变量 $_SESSION['emptystcode'] 传递给 bulkstOLD.php 但不是其他两个:$_SESSION['dlstrest'] 和$_SESSION['dllist']。我在被调用脚本的第一个命令行上有 session_start();。

此脚本1 所做的只是$_GET 餐厅和列表名称,并显示一个表单,用户可以在其中填写代码字段并提交它。

更糟糕的是,我创建了另一个脚本(此调用者脚本的简化版本),它只设置会话变量并调用相同的 bulkstOLD.php,它工作正常。我想知道这是否与 $_GET 有关。任何帮助将不胜感激。谢谢!

代码如下:

<?php
session_start();
include ('logged.php'); 
require_once ('functions.php');
$acisversion = $_SESSION['acisversion']; 
reset_all();
$cyear = date(Y);
$cmonth = date(M);
$cday = date(d);
$configs = include('config.php');
$_SESSION["module"] = 'st';
$_SESSION['favstlists_ref'] = 0;
$strest = $dlstrest = $_GET['dlstrest'];
$favstlist = $dllist = $_GET['dllist'];
$restname = 'restname_'.$strest;
$stlistrest = $configs[$restname]; 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>ACIS - Edit Lists</title>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script> 
<script type="text/javascript" src="stmenu.js"></script>
<style type="text/css">
@import url(acis.css);
</style>

<script type="text/javascript" src="js.js"></script>

</head>
<body>

    <div id="content">
<?php

  $_SESSION['stcodesub'] = $stcodesub = $stcode = ($_POST["stcodein"]);

  dbfile_init() ; 
   $_SESSION['stcode'] = $stcode;

if(isset($_POST["editSave"]))
      { 

            connect_db ();
            if (!empty($stcode))
            {                  
               check_code_for_st_list();

                $emptystcode = 0;
            }
            else
            { 
                $emptystcode = 1;                
            }

            $_SESSION['dlstrest'] = $dlstrest;
            $_SESSION['dllist'] = $dllist;
            $_SESSION['emptystcode'] =  $emptystcode;  

            if ((!$emptystcode and $codestok) or $emptystcode)
                { 
                    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=bulkstOLD.php">';
                    exit;  
                }
     }

?>
<table width="100%" border="0" align="center">
  <tr>
     <td width="223" align="left"></td>
     <td width="111" align="right"></td>
  </tr>
</table>


</style>
<form method="post" id="editLists" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<table width="739" border="0" align="center" cellspacing="0" bgcolor="#171717">
     <tr>
    <td colspan="3" 
    style="color:#00CC00;
    text-align: left;
    padding: 0;
    background: #2e2e2e url(images/nav_bar.jpg) repeat-x;
    font-size: 0.9em;
    color: white;
    height:16px;"><script type="text/javascript" src="mainmenu.js"></script></td>
    </tr>
  <tr>
    <td width="502"><h2 style="color:red;"> Atualizar Listas Favoritas de Pratos</h2></style></td>
    <td width="231" colspan="2" align="right"><?php echo $configs[footer];?></td>
    </tr>
  <tr>
    <td colspan="3" align="right"  ><table width="100%" border="0" align="center">
      <tr>
        <td align="center">Restaurante:</td>
        <td width="84" align="center" valign="bottom">Adicionar Código</td>
        <td width="350" align="center">Lista a ser atualizada:</td>
        <td width="150" align="left" valign="bottom"><span class="error"><?php echo $favErr;?></span></td>
        </tr>
      <tr>

        <td width="135" align="center"><b><?php echo $stlistrest; ?></b></td>
        <td align="center">
<?php
         echo $stcodeline;
?>
        </td>
        <td align="center"><b><?php echo $dllist; ?></b></td>
        <td align="right"><input type="submit" name="editSave" style="background-color: #009900; color: #FFFFFF;" value="Submeter" /></td>
        </tr>
      </table></td>
  </tr>

</table>
</form>



<?php


$_SESSION["module"] = 'st';

?>

</div>


</body>
</html>

【问题讨论】:

  • session_start(); 必须在所有使用会话的文件中。是吗?如果没有,请执行。
  • 我将它们作为每个脚本的第一个命令
  • $dlstrest; 第一次出现在$_SESSION['dlstrest'] = $dlstrest;$dlstrest 来自哪里?既然您提到了 $_GET:以防万一您依赖像 ....?dlstrest=xyz&amp;.... 这样的 GET 参数作为全局变量导入 - 自 2002 年以来默认情况下并非如此,无论如何您都必须检查该参数的存在。
  • 如果那些会话值没有被设置,那么,那些会话值没有被设置。该脚本与“有效”的简化脚本有什么不同?当你调试这个时,它在哪里失败了?
  • 我赞同 VolkerK 的评论。 $dlstrest 和 $dllist 仅在您设置会话变量时出现在您的代码中。它们是否在其他地方初始化?如果没有,那是你的问题

标签: php session


【解决方案1】:

对您的问题唯一可能的解释是,您可能没有在一开始就写 &lt;?php session_start();?&gt;,在任何 HTML 标记(甚至是 doctype)之前。将其写在每个相关的 php 文档之上。

【讨论】:

  • 我做到了....被调用的脚本能够恢复会话变量之一,但不能恢复其他两个
  • 是的..这就是我感到困惑的原因...我认为与 $_GET 和表单提交有关...只是猜测..
  • 几年后,这实际上与我的情况有关,哈哈-谢谢!向你大声喊叫
【解决方案2】:

查看您的所有文件是否都保存为没有 BOM 的 UTF-8,而不仅仅是 UTF-8。 此 BOM 禁用会话。

并查看是否所有文件都在同一个子域中被调用。

www.domain.com/file1.php,
ww2.domain.com/file2.php,
domain.com/file3.php

... 使用不同的子域不应该工作。

【讨论】:

  • 我/我通过 WEBMATRIX + LOCALHOST 运行脚本......会话变量在所有其他脚本中都可以正常工作......不知何故,在这个中它通过 $_GET 获取变量,但值以某种方式当我提交它时被擦除(执行 bulkstOLD.php)。也许这与 SESSION 无关,而是通过 $_GET 获得的变量发生了一些事情,因为这两个正在消失...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 2019-10-22
  • 2012-11-15
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多