Hello, I am a brand new subscriber to this list, and am fairly new to PHP as well. I started using it back in June to put together an online survey for my dissertation.
Though I learned a few things the hard way, I have done mostly OK since then, until I changed an increasingly complex script to using functions. That's when my big problem started. Yes, I eventually figured out that global variables aren't accessible inside a function unless they are declared as such. Everything I have read tells me that $_SESSION is equally accessible both outside and inside functions. For a while today, I was beginning to doubt that, but then I realized that the problem I was experiencing was due to the placement of a particular statement within the script. This particular script calls three other scripts. One (background.php) should be called the first time a survey participant logs on. Whether or not the background had been collected yet was stored in a mysql table and was used to control whether background.php got called or not. Before the script was changed to use functions, calling background.php happened just as I expected, first thing each time a new user logged on. But when I changed the script to use functions, I could no longer get that background.php to get called -- UNLESS I placed echo statements after it to show the values in $_SESSION -- then it worked EVERY TIME. I spent countless hours today trying to figure out why the $_SESSION value which was controlling the call to background.php was apparently not accessible, only to realize that that wasn't the problem at all -- instead the problem was placement within the code of the call to background.php. Here's the basic question for those of you more experienced than I: Why was I able to call background.php just as I wanted to when the script didn't have functions, but not able to make the very same call when the script was changed to use functions? Some code snippets: 1) before code was changed to use functions (background.php was called as expected): //include DB connection include('./db.php'); //start session session_start(); $_SESSION["session_uid"]=$uid; //what person name pair will this user judge? $query="SELECT * FROM diss_pretesters WHERE uid='".$_SESSION["session_uid"]."'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); //assign values from query to session variables $data=mysql_fetch_array($result); $_SESSION["session_set"]=$data['setid']; $_SESSION["session_sets_array_member"]=$data['last_setid']; $_SESSION["session_curr_array_member"]=$data['last_said_member_eval']; $_SESSION["session_background_info_recorded"]=$data['background_info_recorded']; $_SESSION["session_sets_assigned"]=$data['sets_assigned']; if($_SESSION["session_background_info_recorded"]=='N') { header("location: background.php"); //collect background information before any arrays are advanced } //create current array member if($_SESSION["session_curr_array_member"]=='') { $_SESSION["session_curr_array_member"]=0; } else { $_SESSION["session_curr_array_member"]++; } [snip some intervening code where some of the below values were set] $current_said_field = $_SESSION["session_curr_array_member"]+1; $current_said_fieldname = "said".$current_said_field; //Retrieve current said value from the randomized table $query="SELECT $current_said_fieldname FROM diss_user_set_randomize WHERE uid='$uid' AND setid='".$_SESSION["session_set"]."'"; //echo "QUERY: $query"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); //assign values from query to session variables $data=mysql_fetch_array($result); $current_said=$data[$current_said_fieldname]; $_SESSION["session_current_said"]=$current_said; if($_SESSION["session_curr_array_member"]<$_SESSION["session_set_size"]) { header("location: judgeFrameset.php"); //send user to relevant frameset } else { header("location: posttask.php"); } 2) Code from initial stab at functions, which didn't work in that background.php was never called -- unless followed immediately by uncommented echo statements; instead either judgeFrameset.php or posttask.php were called: ///////////////////////////////////////////////////////////////////////////////////////////// function GetBasicInfo () { global $db, $uid; $query="SELECT * FROM diss_pretesters WHERE uid='$uid'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $_SESSION["session_set"]=$data['setid']; $temp_setid = $_SESSION["session_set"]; $_SESSION["session_sets_array_member"]=$data['last_setid']; $_SESSION["session_curr_array_member"]=$data['last_said_member_eval']; $_SESSION["session_background_info_recorded"]=$data['background_info_recorded']; $_SESSION["session_sets_assigned"]=$data['sets_assigned']; if ($temp_setid!='') { $query="SELECT * FROM diss_users_assign WHERE uid='$uid' AND setid='$temp_setid'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $_SESSION["session_set_shuffled"]=$data['Shuffled']; $_SESSION["session_set_completed"]=$data['Completed']; } return 0; } ///////////////////////////////////////////////////////////////////////////////////////////// [some more functions] //include DB connection include('./db.php'); //start session session_start(); $_SESSION["session_uid"]=$uid; $dummy = GetBasicInfo(); if($_SESSION["session_background_info_recorded"]=='N') { header("location: background.php"); //collect background information before doing anything else } //Set assignment if($_SESSION["session_sets_assigned"]=='N') { // first time through for this user $dummy = FindNewSet(); $dummy = GetSets(); $dummy = GetBasicInfo(); } elseif($_SESSION["session_set_completed"]=='Y') { // user just finished last set; need to start a new one $dummy = FindNewSet(); $dummy = GetSets(); $dummy = StartNewSet(); $dummy = GetBasicInfo(); } // first time through only; randomize the order of name pairs shown to the participant if ($_SESSION["session_set_shuffled"]=='N') { $dummy = RandomizeSet(); } //create current array member if($_SESSION["session_curr_array_member"]=='') { $_SESSION["session_curr_array_member"]=0; } else { $_SESSION["session_curr_array_member"]++; } $current_said_field = $_SESSION["session_curr_array_member"]+1; $current_said_fieldname = "said".$current_said_field; //Retrieve current said value from the randomized table $query="SELECT $current_said_fieldname FROM diss_user_set_randomize WHERE uid='$uid' AND setid='".$_SESSION["session_set"]."'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $current_said=$data[$current_said_fieldname]; $_SESSION["session_current_said"]=$current_said; if($_SESSION["session_curr_array_member"] < 15) { header("location: judgeFrameset.php"); //send user to relevant frameset } else { header("location: posttask.php"); } 3) Code after changing the location within the script of the call to background.php (finally works!): ///////////////////////////////////////////////////////////////////////////////////////////// function GetBasicInfo () { global $db, $uid; $query="SELECT * FROM diss_pretesters WHERE uid='$uid'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $_SESSION["session_set"]=$data['setid']; $temp_setid = $_SESSION["session_set"]; $_SESSION["session_sets_array_member"]=$data['last_setid']; $_SESSION["session_curr_array_member"]=$data['last_said_member_eval']; $_SESSION["session_background_info_recorded"]=$data['background_info_recorded']; $_SESSION["session_sets_assigned"]=$data['sets_assigned']; if ($temp_setid!='') { $query="SELECT * FROM diss_users_assign WHERE uid='$uid' AND setid='$temp_setid'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $_SESSION["session_set_shuffled"]=$data['Shuffled']; $_SESSION["session_set_completed"]=$data['Completed']; } return 0; } ///////////////////////////////////////////////////////////////////////////////////////////// [some more functions] //include DB connection include('./db.php'); //start session session_start(); $_SESSION["session_uid"]=$uid; $dummy = GetBasicInfo(); //Set assignment if($_SESSION["session_sets_assigned"]=='N') { // first time through for this user $dummy = FindNewSet(); $dummy = GetSets(); $dummy = GetBasicInfo(); } elseif($_SESSION["session_set_completed"]=='Y') { // user just finished last set; need to start a new one $dummy = FindNewSet(); $dummy = GetSets(); $dummy = StartNewSet(); $dummy = GetBasicInfo(); } // first time through only; randomize the order of name pairs shown to the participant if ($_SESSION["session_set_shuffled"]=='N') { $dummy = RandomizeSet(); } //create current array member if($_SESSION["session_curr_array_member"]=='') { $_SESSION["session_curr_array_member"]=0; } else { $_SESSION["session_curr_array_member"]++; } $current_said_field = $_SESSION["session_curr_array_member"]+1; $current_said_fieldname = "said".$current_said_field; //Retrieve current said value from the randomized table $query="SELECT $current_said_fieldname FROM diss_user_set_randomize WHERE uid='$uid' AND setid='".$_SESSION["session_set"]."'"; $result=mysql_db_query("$db","$query") or die(query_fail("$query")); $data=mysql_fetch_array($result); $current_said=$data[$current_said_fieldname]; $_SESSION["session_current_said"]=$current_said; if($_SESSION["session_background_info_recorded"]=='N') { header("location: background.php"); //collect background information before doing anything else } elseif($_SESSION["session_curr_array_member"] < 15) { header("location: judgeFrameset.php"); //send user to relevant frameset } else { header("location: posttask.php"); } *************************************************************** Before I figured this out, I was going to write and ask why the h*** PHP couldn't access the value of $_SESSION["session_background_info_recorded"]. Now that I know this is not the problem, my question becomes why does it matter where in the code the call to background.php is placed? Why does it work in the top of the script before using functions, and cease to work at the top of the non-function part of the script after adding functions? I'm at a loss to understand why. -- Mary Taffet -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php