Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using session variables - Simple but I can't get it to work 2

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
I have read the online docs and reviewed lots of samples on this forum and else where. No matter what I try, I cannot get it to work.

I have written a PHP page which basically uses mostly functions. This method leaves me with the variable scope problem. No problem, I said, as I figured I can always use session variables. Well, I was mistaken.

To give you an idea, this is some what the PHP page structure:

This function loads data grid to screen
Code:
<?PHP
function ShowDataGrid() { ... }

As user clicks on link to expand a record, new window is opened with that record information. This is done by passing variables on URL. The opened PHP document goes out and loads the data as needed. Within this document, user ha options to [ ] Add, [ ] Edit, [ ] Delete. Each of these triggers the same PHP page passing an action trigger variable which is then used to determine proper course of action.

The action page at this point has several functions and it is written kind of like this:

Code:
<?PHP
function GetDataInput ( varA, varB, varC ...) 
{
  .... do some stuff ...
}

function ValidateDataInput ( varA, varB, varC ...)
{
 ...  check data here ...
 ... if not good, build varERR and return it with error ...
}

function LoadDefaultData ( varA ...  )
{
 ... gets data from in-house application.  Data is written
 ... to text file which is then read-in to a variable, this
 ... variable is then returned to calling routine
}
function WriteData ( varA )
{
 ... Write data to text file and call in-house app to
 ... process it
}
?>

<?PHP
// Here is where the actual work begin and functions are 
// called

/*  I use a sequence of if(){...}else{...} statements to
/*  check for $_GET[] and $_POST[].  This is what happens in
/*  a nutshell

if ($_POST['submit' != "" )   // user submitted form
{ $ckERR = ValidateData( ... vars to validate ...)
  if $ckERR != "" { GetDataInput( ... variables ...); }
  WriteData( ... variables ... );
} else {
 LoadDefaultData( ... variables ... );
 GetDataInput ( ... variables ... );
}
This is pretty much the code structure. No matter where I place session_start() I get the following error:

Code:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/websites/axzas/html/josel/aff_attp.php:1) in /home/websites/axzas/html/josel/aff_attp.php on line 151

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/websites/axzas/html/josel/aff_attp.php:1) in /home/websites/axzas/html/josel/aff_attp.php on line 151

I do not know why the error shows twice, that got me worried even more.

Your help, as usual, will be greatly appreciated.


Jose Lerebours

PS: Please excuse the lengthly message but I figure I needed to be as detailed as possible.



KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Hi

Well, the common way to send session identifiers is in cookies. Cookies are sent in HTTP header. So the [tt]session_start()[/tt] must be placed before sending out any content data.

You get two error messages for the two attempts for calling [tt]header()[/tt] : for [tt]Set-Cookie[/tt] and for [tt]Cache-Control[/tt].

Feherke.
 
Quoting feherke

"session_start() must be placed before sending out any content data"

Does this mean that I need to session_start() on the very first document or at the very top of the current document?

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Hi

Jose said:
Does this mean that I need to session_start() on the very first document or at the very top of the current document?
Yes, in every document which is directly requested, but no, not in all documents accessed with [tt]include[/tt] or [tt]require[/tt].

Feherke.
 
feherke,

I have done as you suggested, I no longer get the errors but I can't reference my variables as I jump from function to function.

I am not new to session variables, I use them all the time in ColdFusion. I hate my inability to use them, they are so good to you when writing programs.

Again, I have multiple functions:
functionA() { gets input and submit form }
functionB() { validates submited data }
functionC() { writes data to disk }
functionD() { resets variables to blank or closes window }

After all these functions are defined, I then have a few lines of code where I check for logical data flow. If the form has been submitted, data is validated, if no error, data is written, if error, user is put back on form ...

The thing is that none of the variables are available in any of the called functions.

I am registering all of the variables using session_register.

What am I doing wrong? I do not want to pass the variables as arguments (I am really trying to stay away from that).

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Hi

I can not help you with that, because I never used [tt]session_register()[/tt]. I can only recommend to not use it.
PHP doc said:
Use of session_register() is deprecated
[gray](...)[/gray]
Use of $_SESSION is preferred
If you use [tt]session_register()[/tt] your data is still stored in simple global variables, to access them inside the [tt]function[/tt]s you have to use [tt]global[/tt].

If you would use [tt]$_SESSION[/tt], that is a super-global, so its elements will be accessible everywhere.

Feherke.
 

Sorry if I write something stupid but why not simply use $GLOBALS["myvarname"] inside all your functions? Thus you wouldn't have to pass the variables as arguments.
 
feherke,

Thanks so much for the link. After taking the time to read through with a cleared mind and knowing that I needed to read every line and pay attention to every example, I finally got it.

Have you ever attempted to assemble something prior to reading the "assembly instructions"? Well, that is the way I feel, I just tried to put together a series of routines using something I had not "fully read and fully understood" its instructions. Not to say that I did not read anything at all, I guess I just did not pay close attention to the details.

Sleidia,

Nothing stupid about your question/suggestion ... I simply did not know enough about it. Thanks!

---------
Having said all that, I must confess that coming from a different background, when I read "global" I am thinking of variables that are available across multiple sessions (more like an application type of scope) and feared leakage. I'm still not fully relaxed about this not being the case :(

Now, after reading Variable Scope papers as per link provided by feherke, I managed to get what I was looking for. Since I have so many variables, I simply created a PHP document that simply declares these variables as global and I am including this document in every function where variables are relevant ('include', the latest aspirin).

Going forward, I just might start using $GLOBAS[] to simplify things. This time around, after close to 700 lines of code and heavy variable propagation, I'm not sure I want to change variables every where they are used.


Thank you both so very much!!!!

You both an star (feherke, you get a second because twice you provided helpful information - not to mention that you stuck with through it all :: Thank you dude!!!).

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Thanks :)

One thing that you might want to try : if you have a great amount of variables you could store them all in a single array. Thus, to keep track of everything at instant T, simply print the array to view all the variables at once.

Example :

$GLOBALS["my_funcs"]["func_1"]["var1"]
$GLOBALS["my_funcs"]["func_1"]["var2"]
$GLOBALS["my_funcs"]["func_2"]["var1"]
$GLOBALS["my_funcs"]["func_2"]["var2"]

I do the same with sessions and I find it handy.

You can rename all your vars with a simple search and replace tool.
 
Funny, the use of arrays are next in my "wish I can do that" list. Remember, I've only started coding in PHP (two or less weeks).

Ironically, I have a function which displays in a table the content of $varName and $_POST['$varName']. I did this as a way to troubleshoot my ordeal with sessions - Kind of to see if the variables where holding values ...

Thanks for the suggestion. I will look into using arrays in the very near future.

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top