[SOLVED] SESSION scope issue.
Moderator: General Moderators
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Hi,
Thanks for the trial code, this works fine, the variable is output twice (first from the include and then the main file), once from the main file and the other from the include. If I try putting the same echo into the header or menu file and no session var is output!
Could this have anything to do with the location of these files in my file directory structure? Is there any problem with scope regarding this issue?
Thanks again for your advice.
Best regards.
Thanks for the trial code, this works fine, the variable is output twice (first from the include and then the main file), once from the main file and the other from the include. If I try putting the same echo into the header or menu file and no session var is output!
Could this have anything to do with the location of these files in my file directory structure? Is there any problem with scope regarding this issue?
Thanks again for your advice.
Best regards.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
OK, now let's make a "fatal error";
Wherever you have session_start() in your script, define a random function:
If you recieve any fatal errors, then you know that you're calling session start more than once, and that's not good.
Then, in the script that has the scope issue, define the function again (preserve the original ones you stuck around).
Now, if you don't have a fatal error, there's something wrong (because you need to have started the session for the variable to have been called, and that means the function would have also been declared).
Wherever you have session_start() in your script, define a random function:
Code: Select all
bugfunction() {
return "";
}Then, in the script that has the scope issue, define the function again (preserve the original ones you stuck around).
Now, if you don't have a fatal error, there's something wrong (because you need to have started the session for the variable to have been called, and that means the function would have also been declared).
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Well I placed the function, adding function to the script example that you gave, in to the main calling function. Reloaded to make sure that it was working fine, and all good. Added the same function to the include file that I created from your message previously, and Fatal Error generated, mucha s you predicted. Removed the offending function and placed it into the login.php include and NO Fatal Error produced.
So this still leaves me with the question of why? What is the difference between the referencing of my include files except their location in the file directory structure?
I am very grateful for your time, especially given the frustration of not being able to see the code directly.
So this still leaves me with the question of why? What is the difference between the referencing of my include files except their location in the file directory structure?
I am very grateful for your time, especially given the frustration of not being able to see the code directly.
Last edited by bigbunnyboy on Sun Mar 13, 2005 7:32 pm, edited 1 time in total.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Err... whoops, I actually wanted you to put the code in your script, not the sample script I had given earlier... -_-"
The Why is that functions are global: they can be accessed anywhere. So is _SESSION. However, when functions get defined again, the effect is really noticable (fatal error). Calling session_start() twice isn't so noticable.
The Why is that functions are global: they can be accessed anywhere. So is _SESSION. However, when functions get defined again, the effect is really noticable (fatal error). Calling session_start() twice isn't so noticable.
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
I did put the script in my code as well as your sample, just to see the difference, and the entry of the bugfunction function into the include file that you suggested I generate produced a fatal error, much as should be. However, when I placed the same function into my login.php script, no such error occurred, so this suggests that some sort of sickness is present. However, I cannot work out why these global session vars and this function are not being recognised as existing in these includes from other places in my file structure?
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
OH!
I've just realized something looking back at your code:
emphasis on:
This surely is not the intended result! It will cause the conditional always to be false AND Unset the $_SESSION['login'] variable
Piffle. It's these sorts of mistakes that always get you.
Fix it either by replacing the line with:
or
EDIT: Ah wait! No, it's an "!=" operator. Nevermind...
I've just realized something looking back at your code:
Code: Select all
if ($_SESSION['login']!=null)
{
header("Location: home.php");
}
else
{
include 'includes/functions.php';Code: Select all
if ($_SESSION['login']!=null)Piffle. It's these sorts of mistakes that always get you.
Fix it either by replacing the line with:
Code: Select all
if (isset($_SESSION['login']))Code: Select all
if($_SESSION['login'] == null)-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
Ambush Commander:
I have since changed the $_SESSION to use the isset function, as recommended earlier, but without alteration to the output.
feyd:
I see where you are coming from and as Ambush Commander has proved that the duplication of a function should indeed produce a fatal error, but this simply does not seem to be the case.
I am altering the script and seeing alterations occur on the site, so it must be the menu.php that is included.
I use the login.php script present in the menu.php file to login into the system and this produces accurate login for the main script and redirects the main page to the login page. however all of the includes found in another directory seem to carry on regardless, unaware of this change in the session state.
I am so sorry for this nonsense, and am realising that I will owe you most of my internal organs as payment when all this is over!! Thanks.
I have since changed the $_SESSION to use the isset function, as recommended earlier, but without alteration to the output.
feyd:
I see where you are coming from and as Ambush Commander has proved that the duplication of a function should indeed produce a fatal error, but this simply does not seem to be the case.
I am altering the script and seeing alterations occur on the site, so it must be the menu.php that is included.
I use the login.php script present in the menu.php file to login into the system and this produces accurate login for the main script and redirects the main page to the login page. however all of the includes found in another directory seem to carry on regardless, unaware of this change in the session state.
I am so sorry for this nonsense, and am realising that I will owe you most of my internal organs as payment when all this is over!! Thanks.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I'm still looking at the code... mumble mumble.
Point of personal preference: You should include the "config" file outside the function.
Another point of personal preference:
Declare the "Display Login Page" function before the checkLogin() function/
And I'm thinking... Why do you need login.php in the first place?
Point of personal preference: You should include the "config" file outside the function.
Another point of personal preference:
Declare the "Display Login Page" function before the checkLogin() function/
And I'm thinking... Why do you need login.php in the first place?
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
I will look into your comments, all greatly appreciated, thanks. 
The reasons for proceeding on the path that I am is that I want to divide the login functionality to a separate area so that I can simply plug it into the relevant area I like, so that if I want to change the look and feel of the site I can modify the template and plug in the login.php script to any old place, and Bob's your auntie, if you know what I mean. It seemed a good idea before I embarked on all this, now I am beginning to wonder....
The thing is that I am sure it really is not so difficult as I am experiencing, it is just that I am stupid and that is the bitter pill to swallow!!
I am sure I will deal with it in time however.
The reasons for proceeding on the path that I am is that I want to divide the login functionality to a separate area so that I can simply plug it into the relevant area I like, so that if I want to change the look and feel of the site I can modify the template and plug in the login.php script to any old place, and Bob's your auntie, if you know what I mean. It seemed a good idea before I embarked on all this, now I am beginning to wonder....
The thing is that I am sure it really is not so difficult as I am experiencing, it is just that I am stupid and that is the bitter pill to swallow!!
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am
That is in a sense what I am doing with the login.php script (see earlier script inclusion), no? What do you mean? I am becoming desperate!!
The thing is that I also use a header file that is located in the same folder as the menu.php and login.php files and this also cannot reference the session vars.
I am getting upset.... and it is way past my bedtime. Surely this is likely to make anyone cranky!!
The thing is that I also use a header file that is located in the same folder as the menu.php and login.php files and this also cannot reference the session vars.
I am getting upset.... and it is way past my bedtime. Surely this is likely to make anyone cranky!!
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I still think you are including either the wrong menu.php, or login.php ... here's a way to find out: use realpath() just before you call include() with the exact thing you send to it.
-
bigbunnyboy
- Forum Newbie
- Posts: 22
- Joined: Sat Mar 12, 2005 5:26 am