tags:

views:

240

answers:

4

I am having many files with multple includes one into another like below,

File1 is included in File2 and then File2 is under File3

Now I want to declare a session variable site_user_conutry from File1 and then I am checking on File2 that if there is any no value in the session variable then Only I am including File1.php

I have added session_start(); in each [age but still its not working ??

Please tell me how can I make session work in the above condition.

+2  A: 

Within File2

if ($_SESSION["site_user_country"] != null) 
{
    include_once(File3);
}
Goblyn27
+1  A: 

How are you setting the session variable? You should try setting it like so:

$_SESSION['site_user_conutry'] = 'United-Kingdom';

Have you placed session_start() at the very top of the document, or at least before you output any information?

xenon
Yes, I am declaring the session as you demonstrated. and also I am adding session_start() at the top of every page.
Prashant
What do you get if you var_dump($_SESSION)? Completely empty?
xenon
+1  A: 

Assuming that you are including files that have code execution, not functions.

The code that includes File1 and sets the session variables must be executed before the code on File2 on the first iteraction.

In the next iteractions the session will hold the values for you.

To set an session variable use the $_SESSION global array.


$_SESSION["name"] = "value";
Renato Aquino
+1  A: 

Do session_start in first lines of code just once.

Documentation says: As from version 4.4.3, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

If file1 is included in file2 that code from file2 is done before file1. So if you declare something in file1 may not be seen in file2 (depends on code...), anyway it is not modern way pf php programming. (see oo and __autoload)

fornve