从表单传递的 php SESSION 变量


php SESSION variables passed from a form

我在尝试将表单变量传递给生成多个页面的脚本时遇到了问题。

我的表单在以下脚本中使用 $_POST 提供县名称和语言作为 2 个单独的字段。当查询是硬编码的时,该脚本起作用,对于 page=1 结果.php当通过 $_POST 传递变量时,该脚本有效。但是,后续页面会丢失 $_POST 变量,我知道此问题的解决方案涉及 $_SESSION 命令,但我无法让它工作:(。事实上,尽管参数已传递给这些函数,但现在不会显示任何结果。

这是我的代码

<?php
$_SESSION['county']=$_POST['county'];
$_SESSION['language']=$_POST['language'];
$speak=$_POST['language'];
$area=$_POST['county']
?>
<table class="bda" width="100%" align = "center"><tr><td class="hrak">Open test db file on lcn</td></tr></table>
<?php
$conn = connect();
/*******************************************************************************************
The variable '$showrecs' refers to the number of rows on each page - this can be changed.
'$pagerange refers to blocks of records, again can be changed.
*******************************************************************************************/
 $area=$_POST['county'];
 $speak=$_POST['language'];
 $showrecs = 20;
 $pagerange = 10;
 $page = @$_GET["page"];
 if (!isset($page)) $page = 1;
 select();
 mysql_close($conn);
 ?>
 <table class="bda" width="100%"><tr><td class="hrak">Powered by MySQL</td></tr></table>
 </body>
 </html>
 <?php
 /******************************************** - START OF FUNCTIONS  *************************************/
 ?>
 <?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  $res = sql_select();
$count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
      $pagecount = intval($count / $showrecs) + 1;
  }
  else {
      $pagecount = intval($count / $showrecs);
  }
      $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
  ?>
     <table class="bd" border="0" cellspacing="1" cellpadding="4" width="100%" align = "center">
     <tr><td class="hrak" align="center" width="100%">Records shown <?php echo $startrec + 1 ?> -        <?php echo $reccount ?> of <?php echo $count ?></td></tr>
    </table>
 <?php showpagenav($page, $pagecount); ?>
 <br>
 <table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
 <tr>
 <!- ***************************************
    HEADER row of the table - HTML
  **************************************** ->
  <td>
  </td>
  </tr>
<?php
   for ($i = $startrec; $i < $reccount; $i++)
   {
     $row = mysql_fetch_assoc($res);
     $mem13 = ($row["title"]);
     if ($mem13 == 'Mr')
         {
         $style = "dr";
         $styler = "drr";
      if ($i % 2 != 0) 
         {
         $style = "sr";
         $styler = "srr";
         }
    }
   else
    {
         $style = "ndr";
         $styler = "ndrr";
      if ($i % 2 != 0) 
    {
        $style = "nsr";
        $styler = "nsrr";
    }
   }
?>
<tr>
 <!-- *********************************************
This is where I populate the DATA rows of the table  - HTML
 ********************************************** -->

 <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id"]) ?></td>
 </tr>
<?php
    }
   /*** mysql_free_result($res); ***/
 ?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>
<?php function showpagenav($page, $pagecount)
{
?>
  <table class="bd" border="0" cellspacing="1" cellpadding="4" >
  <tr>
<?php if ($page > 1) { ?>
 <!- /*************************************************************************
 This generates 'results.php' and the sub pages results results.php?page=2...
 *****************************************************************************/
 -->
 <td class="hrak1" align="center"><a href="results.php?page=<?php echo $page - 1 ? >">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
   global $pagerange;
   if ($pagecount > 1) {
   if ($pagecount % $pagerange != 0) {
     $rangecount = intval($pagecount / $pagerange) + 1;
   }
   else {
      $rangecount = intval($pagecount / $pagerange);
        }
  for ($i = 1; $i < $rangecount + 1; $i++) {
     $startpage = (($i - 1) * $pagerange) + 1;
     $count = min($i * $pagerange, $pagecount);
     if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
       for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
 ?>
  <td><b><?php echo $j ?></b></td>
 <?php } else { ?>
 <!--
 /*****************************************************************************************************
 results.php?pagen  so that the required page number is generated.
 *****************************************************************************************************/
-->
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $startpage ?>"><?php echo  $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td class="hrak1" align="center">&nbsp;<a href="results.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>
<?php 
/******************************************** - FUNCTION - SQL_SELECT *************************************/
function sql_select()
{
/******************************************************************************
SELECT statement for results to be displayed
******************************************************************************/
   $area=$_POST['county'];
   $speak=$_POST['language'];
   global $conn;
   $sql = "SELECT * FROM test2 WHERE language='$speak' AND county='$area' ORDER BY id";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   return $res;
}
/******************************************** - FUNCTION - SQL_GETRECORDCOUNT *************************************/
function sql_getrecordcount()
{
  $area=$_POST['county'];
  $speak=$_POST['language'];
  $sql = "SELECT COUNT(*) FROM test2 WHERE language='$speak' AND county='$area'";
    $res = mysql_query($sql, $conn) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    reset($row);
    return current($row);
}
/******************************************** - FUNCTION - CONNECT *************************************/

我知道代码无需使用 $_POST 传递参数即可工作,但我无法让它处理多个结果页面(results.php?page=n)。我错过了什么?

问候

鲍勃

您必须

session_start()放在每个使用 $_SESSION 变量的脚本的开头。

session start()添加到使用会话变量的所有页面

阅读 PHP 中的手动会话处理

试试这个,我希望它会起作用:-

session_start();
$_SESSION['county']=$_POST['county'];
$_SESSION['language']=$_POST['language'];

在不启动会话的情况下使用 $_SESSION 变量?在脚本中使用session_start();

感谢您的输入。我已经通过在调用页面上设置 cookie 解决了这个问题,然后在结果页面上使用了 cookie:

<?php
    $area  = $_COOKIE['Carea'];
    $speak  = $_COOKIE['Cspeak'];
?>

然后,我将这些变量作为函数调用中的参数传递。

function sql_select($area,$speak)

它有效。

鲍勃