954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SESSION variable problem

This is content of in.header.php

if(!isset($session_id) || empty($session_id)) {
session_start();
$_SESSION["Inputs"]=array();
}


This is index.php which also has form objects that POSTs other values to step1.php

require_once 'inc.header.php'
$_SESSION["Inputs"]["name"]="MyName";
$_SESSION["Inputs"]["surname"]="MySurname";


This is step1.php

require_once 'inc.header.php'
$_SESSION["Inputs"]["age"]=$_POST["textboxAge"];
print_r($_SESSION["Inputs"]);


Problem is print_r prints only Array () . I cannot see name, surname and age.

What do i miss?

Thanks in advance

veledrom
Master Poster
754 posts since Apr 2008
Reputation Points: 42
Solved Threads: 0
 

Hi,

If you haven't already read the function description for session_start(), read it here .

You have to call session_start at the beginning of every new page to continue the previous session. You may also find it beneficial to use session_name.

Check out the examples on the link I included.

R.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 
if(!isset($session_id) || empty($session_id)) {
session_start();
$_SESSION["Inputs"]=array();
}


It's not clear where $session_id is declared if at all, therefore the code will always run regardless of whether you have a $_SESSION variable. In addition, $_SESSION["Inputs"]=array(); may overwrite previous $_SESSION variables, leaving you with an empty array.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,755 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,074
 

Solved.
inc.header.php

<?php
session_start();
?>


index.php

<?php
require_once 'inc.header.php';
$_SESSION["Inputs"]["name"]="MyName";
$_SESSION["Inputs"]["surname"]="MySurname";

echo "<pre>";
print_r($_SESSION["Inputs"]);
?>

<form action="next.php" method="post">
<input type="textbox" name="age" value="15" />
<input type="submit" name="submit" value="send" />
</form>


next.php

<?php
require_once 'inc.header.php';
$_SESSION["Inputs"]["age"]=$_POST["age"];;

echo "<pre>";
print_r($_SESSION["Inputs"]);
?>
veledrom
Master Poster
754 posts since Apr 2008
Reputation Points: 42
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You