Forum Moderators: coopster

Message Too Old, No Replies

Sessions Problem

         

kuzjph

3:23 am on Feb 22, 2005 (gmt 0)

10+ Year Member



Hi, I'm having problems implementing PHP sessions. This is what I'm trying to do. I have 3 php files A,B and C. File A has a HTML form which will send the field data a,b,c,d to File B. File B contains the following code excerpt:

$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];

$_SESSION['a'] = $a;
$_SESSION['b'] = $b;
$_SESSION['c'] = $c;
$_SESSION['d'] = $d;

File B also contains another HTML form. The design of this form is dependant upon the variable $a. This form will send the field data e and f to File C. I planned on using $_SESSION[] to get the values of a,b,c and d in File C but it doesn't work. I tried using $_SESSION[] in File B and it works just fine.

Hope that wasn't too confusing.

I have placed the session_start() above all my php files:

<?php
session_start();
header("Cache-control: private");
?>

My register_globals have been set to off and I'm currently using PHP version 4.3.4

Could someone help me please?

supermanjnk

1:49 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



page with the form on it.

<?
session_start();
header("Cache-control: private");
?>

<form method="POST" action="page.php">
<input name="input1">
<input name="input2">
<input type="submit" name="submit">
</form>

page.php

<?php
session_start();
header("Cache-control: private");

$_SESSION['a'] = $_POST['input1'];
$_SESSION['b'] = $_POST['input2'];
$input1 = $_SESSION['a'];
$input2 = $_SESSION['b'];

echo $input1;
echo $input2;

?>

this might be what you are looking for.

kuzjph

3:05 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Hi, thanks for the reply. The sample code that you wrote is exactly what I did. If you refer back to my first post, the code you wrote corresponds to my PHP file A and B. However that is not the problem I'm facing. I am having trouble getting the input data posted from file A to B into file C. I'll use an example to make this clear:

In file A, I have a HTML form which contains a text field called "first_name". I can post the data to file B without any problems.

*** excerpt code in B ***
$name = $_POST['first_name'];

Now in file B, the user needs to fill in another form which will be posted to file C. Besides the new data which will be posted, I also want to send the value saved in $name to file C. I figured that I could use sessions to register the value $name so that I could call it in file C.

*** excerpt code in B ***
$_SESSION['name'] = $name;

However, the value does not appear in file C when I call it. I get nothing.

*** excerpt code in C ***
echo $_SESSION['name'];

When I type the above code in file B, it works and displays the first name. It just won't work in file C.
Is this a bug or am I doing something wrong?

I'm relatively new to PHP sessions. I learnt up sessions from here:

[phpfreaks.com...]