会话变量在页面之间丢失

时间:2014-05-16 00:18:08

标签: php

我有很多页面需要访问变量。当用户在accounts.php:

上的表单中输入ID时,会为此变量分配值

account.php

<form action="afterlog.php" class="form" method="post">
    <input type="text" name="amid" id = "amid" class="input" />
    <input class="btn" type="submit" value="Go" />       
</form>

发布&#39;在&#39;之中to afterlog.php

afterlog.php

<?php
session_start();
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $_SESSION['account_manager_id']=$account_manager_id;
    $account_manager_id = $_POST['amid'];
    header('Location: customer_view.php');
  }
?>

检查POST,分配会话变量,并将用户重定向到customer_view.php

customer_view.php

我需要使用&#39; $ account_manager_id&#39;在此页面和之后的所有页面。这是我如何为它分配_SESSION变量的值:

<?php 
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>

不要在任何页面上保留值,包括customer_view.php。我知道它传递给afterload.php因为它打印在那个页面上,但它在那个页面之后消失了。

我做错了什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

您试图在$_SESSION['account_manager_id']之前为$account_manager_id分配一个值。您只需要切换订单:

$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];

或简单地说:

$_SESSION['account_manager_id'] = $_POST['amid'];

答案 1 :(得分:0)

in afterlog.php

<?php
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $account_manager_id = $_POST['amid'];    
    $_SESSION['account_manager_id']=$account_manager_id;

    header('Location: customer_view.php');
  }
?>

<?php
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $_SESSION['account_manager_id']=$_POST['amid'];
    header('Location: customer_view.php');
  }
?>