Forum Moderators: coopster

Message Too Old, No Replies

mainting state inside a function?

         

dmmh

7:36 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



lo folks :)

I have a question which no doubt will pose you people with nor problem whatsoever to help me with

I have written a function to display coherent error messages throughout my entire site

I pass it 2 vars, which contain only text
one is for the document's title, one for the error message in the body (ofcourse there is formatting etc, but thats all inside the function and CSS)

problem is, when the function gets called, it loses state, ie.: the generated page doesnt start the session properly, even though I do this inside the function

what can I do to prevent this?
some elements of the site are only shown when a user is logged in, so the effects are rather dramatic

any help would be appreciated

demnetia

9:42 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Start the session outside the function.
Access the session variables using the superglobal $_SERVER variable.

That's all I can provide with the information you gave us.

demnetia

9:42 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Sorry, I meant $_SESSION instead of $_SERVER

dmmh

9:44 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



well, Im already doing that ofcourse
I session_start() on each page...even the ones that contain the error function call somewhere.

even though alll works well, as soon as the error displays...i seem to be logged out (not the case ofcourse)

demnetia

9:57 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



What exactly in your script is determining whether the user is logged in or not? Is it a variable? a function?
Are you checking whether the user is logged in or not inside that function or not?
Is that function called when the user is logged in? When he is logged it?

We have to see some code dude.

dmmh

10:07 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



its not that complicated now is it? really, I just check if a valid session exist on each page that requires it...

but to make it easier:
if (isset($_SESSION('valid_user') and func_status_check($_SESSION['uid'])!= 'admin'){
code
$var1 = "Access denied";
$var2 = "You arent allowed to access this part of the site";
func_error_msg($var1, $var2);}

thats it...al theres to it. I dont have a problem with my sessions..perhaps you should take more time to read. ofcourse the current user is logged in.....I said that in my previous didnt I?

It just doesnt maintain state when the fucntion is called

the function is very simple

function func_error_msg($var1, $var2)
{
session_start(); //doesnt make a difference?>
<html>
<title><? echo $var1;?></title>
<body> <? echo $var2;?>
</body>
</html<
<? }

theres more html in the fucntion...but thats it, left out most to give you an idea

edit: no I am not checking inside the fucntion if the use is logged in...im doing that right at the start of the page....so I just figured it would be in scope

the function is included once via a sitewide include file

[edited by: dmmh at 10:22 pm (utc) on Jan. 16, 2005]

demnetia

10:16 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



I don't know about you, but if a non-empty $_SESSION['valid_user'] means that the user is logged in , then there's absolutely no problem at all with your code. And that is because the error function is called when the user is not logged in. Therefore, it is essential that whenever you see that error message you be logged out.

You should also drop the session_start() inside the function if you had already started it in the page. There's absolutely no reason for you do start it again.

dmmh

10:20 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



edited above code to reflect changes

you are right...but I was just using it as an example.

Like I said....user is logged in. I just used it as a way to make clear to you in general form how the fucntion is called. Bad example

But like I said, I use this function for a great many scenarios (i have lots of check etc, if someone can edit posts, topics, is allowed access to admin ctrl panel, stuff like that, if not, I use this function)

and I know I dont have to session_start() again, just threw it in there in case someone was wondering if it would work :p

demnetia

10:41 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Bottomline is that the function doesn't affect the session in anyway; it's just a function. The problem is somewhere else in the code.

You need fix this, since it is the condition that triggers the error: isset($_SESSION('valid_user') and func_status_check($_SESSION['uid'])!= 'admin'

Debug the page using var_dump($_SESSION) and see the result

RedScourge

5:42 am on Jan 17, 2005 (gmt 0)

10+ Year Member



also, make sure you use square brackets on $_SESSION, and i think you cant output anything to the page before the session_start() too if that matters, like with Header()

dmmh

8:27 am on Jan 17, 2005 (gmt 0)

10+ Year Member



vardump just return my username, the uid I have (1) and some site preferences which determine how many threads in the forums I wanne see per page etc...nothing wrong there