Question

I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:

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>

which posts the value 'amid' 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');
  }
?>

which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php

customer_view.php

I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:

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

Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.

What I am doing wrong?

Thanks for your help!

Was it helpful?

Solution

You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:

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

or simply:

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

OTHER TIPS

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');
  }
?>

or

<?php
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $_SESSION['account_manager_id']=$_POST['amid'];
    header('Location: customer_view.php');
  }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top