Page 3 of 4
Posted: Sun Mar 13, 2005 6:59 pm
by bigbunnyboy
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.
Posted: Sun Mar 13, 2005 7:03 pm
by Ambush Commander
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).
Posted: Sun Mar 13, 2005 7:28 pm
by bigbunnyboy
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.
Posted: Sun Mar 13, 2005 7:32 pm
by Ambush Commander
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.
Posted: Sun Mar 13, 2005 7:43 pm
by bigbunnyboy
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?
Posted: Sun Mar 13, 2005 7:49 pm
by Ambush Commander
OH!
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';
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...
Posted: Sun Mar 13, 2005 7:51 pm
by feyd
in your main index file you include menu.php. In turn menu.php includes login.php, correct? Login.php and the main index both define displayloginpage() .. yet there is no fatal error? Make sure you are including the proper menu.php
Posted: Sun Mar 13, 2005 8:03 pm
by bigbunnyboy
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.
Posted: Sun Mar 13, 2005 8:14 pm
by Ambush Commander
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?
Posted: Sun Mar 13, 2005 8:25 pm
by bigbunnyboy
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.
Posted: Sun Mar 13, 2005 8:28 pm
by Ambush Commander
Maybe a better idea would have been to create a "Function" that returned the HTML necessary for a login page (and then echoing it afterwards).
Posted: Sun Mar 13, 2005 8:37 pm
by bigbunnyboy
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!!
Posted: Sun Mar 13, 2005 8:40 pm
by Ambush Commander
Sortof, but what you're really doing is after you've declared the function, you use it on the exact same page. Have another page use the function.
Posted: Sun Mar 13, 2005 9:00 pm
by feyd
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.
Posted: Sun Mar 13, 2005 10:01 pm
by bigbunnyboy
I think that you may have got it!!!
I was using a URL path for linking the files, I am just in the process of modifying things, but preliminary results seem good!!