Hello all,
I have a problem with sessions. In the past I used hidden fields to store variables that I need for my forms, but now I need to get everything to work with the session array $_SESSION. A short example:
1) first I retrieve a record from the database from which only parts are showed in the form, the rest is for validation/identification/verification matters.
2) now the whole record is first completely saved in hidden fields in the form, the the input fields are populated with the values that the user needs to get presented.
3) the user makes changes on one or more values and submits the form.
4) an appropriate function or code block is called and the changed values are saved to the database, sometimes they don't need to be saved because it just calls a calculation or formatting code block or function
Basically I have separated everything into three parts:
1) HTML-output /test.php
2) PHP-functions /include/testfuncs.inc
2) session management /include/sess.inc
test.php
<?php
error_reporting(E_ALL &~E_NOTICE);
if (!empty($_POST)){
extract($_POST);
}
if (!empty($_GET)){
extract($_GET);
}
if(!file_exists("./include/sess.inc")){
$warn .= "session file does not exist";
$hide_form = 1;
}else{
include_once("./include/sess.inc");
}
if(!file_exists("./include/testfuncs.inc")){
$warn .= "function file does not exist";
$hide_form = 1;
}else{
include_once("./include/testfuncs.inc");
}
?>
<?
//don't care about HTML heading code, it is there
if(!empty($warn))
echo $warn;
echo "<form name='test' action='test.php' method='post'>";
if($hide_form != 1){
//OK this is what I did before and want to get rid of
//many of them are multidimensional arrays
if(is_array($data) && !empty($data)){
foreach ($data as $key => $value){
echo "<input type='hidden' name='data[$key]' value='$value'>";
}
}
//say now that just two values are editable from let's say 5
echo "<input type='text' name='data[param_one]' value='".$data['param_one']."'><input type='text' name='data[param_two]' value='".$data['param_two']."'><input type='submit' name='btn_do_something' value='Do it'>";
}
echo "</form>";
//don't care about HTML code, it is there
?>
testfuncs.inc
<?php
error_reporting(E_ALL &~E_NOTICE);
if (!empty($_POST)){
extract($_POST);
}
if (!empty($_GET)){
extract($_GET);
}
if(!file_exists("./include/sess.inc")){
$warn .= "session file does not exist";
$hide_form = 1;
}else{
include_once("./include/sess.inc");
}
if(isset($btn_do_something)){
unset($btn_do_something);
//block to verify, format, calculate or save the data
}
if($gd == 1){
$gd = 0;
//retrieve the record from database and populate the $data array
}
?>
sess.inc
<?php
error_reporting(E_ALL &~E_NOTICE);
if (!empty($_POST)){
extract($_POST);
}
if (!empty($_GET)){
extract($_GET);
}
session_start();
if(!is_array($_SESSION["important_info"]) || empty($_SESSION["important_info"])){
unset($_SESSION);
session_destroy();
header("Location: login.php");
}else{
//do something else
}
?>
Well, basically this is how it was before.
Info: PHP-Version is 4.3.8
In my first try chaanging everything over to using $_SESSION for storing the data array I deleted the foreach loop that creates the hidden fields. Then I changed the part that retrieves the record from the database to not just populate the data array but also the session variable. Like this:
$data = $record;
$_SESSION["form"]["data"] = $record;
Now when I change a visible value and press "Do it" the values in the data array were sometimes overwritten and sometimes the supplemental values that were stored in the hidden fields before were missing, depending on the place I extracted the $SESSION["form"]["data"].
OK, just extracting $SESSION["form"]["data"] at the beginning of the test.php overwrites the changed values (normal behaviour),
extracting it just before HTML-output, means after having called the code block $btn_do_something in testfuncs.inc, leaves me with a data array containing just the two variables showed in the form (also normal).
The problem is I need all of them.
The only way I saw to solve the problem was to write very complicated loops that compared $SESSION["form"]["data"] against $data to change the values in the session array and passing $SESSION["form"]["data"] to $data after comparison completion and located these loop at the beginning of test.php right after sess.inc inclusion.
Well, this worked most of the times, but the more arays I needed or the more dimensions the array had, the more complicated everything became.
At the end I switched back to the hidden fields.
Today I was working on one of my scripts that behaved really strange, First I couldn't find the problem because I didn't see a reason why it should behave the way it did.
Since I was still storing some static data in the $SESSION array under $SESSION["data"] (this has nothing to do with the name in my example) first I didn't see what was wrong. But after a few tests I found out.
In one of my functions in an included file I used a temporary array called $data to store some values before thy were saved to the database. First I couldn't believe what was happening. After a few tests I found out that once $_SESSION["data"] was created, it automatically adopted the values from the temporary $data array. I changed the name of that temporary array and the script behaved like expected.
Now I thought to give it a new try (trying to get rid of hidden fields).
The only thing I had to do was to initialize the sub-array after retrieving the record from database and the record gets stored in the session variable.
Eg:
1. get record from database
2. $_SESSION["record"] = array();
Now I thought it might automatically change the values when changed and submitted in the form. But they don't.
I tried a few things, but nothing helped.
Now I hope somebody here knows how I could maybe get this to work.
What I need is to keep all values that I retrieved from the database without having to use hidden fields and I need the record in the $_SESSION variable to be changed before the array is passed to a function or block of code.
Can anybody give me a hint or an example on how I could get this to work?
Thanks in advance
greetz
gaucho