Forum Moderators: coopster

Message Too Old, No Replies

maintaining state through functions #2

         

dmmh

2:00 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Ive asked this before...hope someone can help me out here

I use a function to display coherent error messages throughout my site. Because some of the items on my site rely on the user being logged in to be shown, the effect is quite dramatic when the script thinks they arent.

I call the function in various ways, but Ill use this as an example:

if (isset($_SESSION['username']) && $_SESSION['access_level']!= 'administrator'){
$err_msg = "Your current access level doesn't allow you access to this area";
$err_msg_title = "Access denied";
func_echo_error_msg($err_msg, $err_msg_title, $_SESSION['username']);
}

the function itself:

function func_echo_error_msg($err_msg, $err_msg_title, $session_valid)
{
ini_set("session.cookie_domain"," .domain.com");
session_start();
if (isset($session_valid)){
$_SESSION['username'] = $session_valid;}?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>domain.com - <? echo $err_msg_title;?></title>
<? include ('http://www.domain.com/inc1.php');?>
<? include ('http://www.domain.com/inc2.php');?>
<? include ('http://www.domain.com/css.php');?>
</head>
<body>
<div align="center">
<? include ('http://www.domain.com/inc3.php');?>
<? include ('http://www.domain.com/inc4.php');?>
<br/>
<strong>
<? echo $err_msg;?></strong>
<br/>
<br/>
<? include ('http://www.domain.com/footer.php');?>
</div>
</body>
</html>
<? }

this just doesn't work, it will still seem like the current user doesnt have a valid session. any ideas?

StupidScript

5:45 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you asking why
if (isset($_SESSION['username']) && $_SESSION['access_level']!= 'administrator'){

always matches? Your function should only fire when the above (or whatever you're using to check for permissions) matches, so if the function is always firing, you'd need to look at your qualifying statements, instead of inside the function.

Is this what you are inquiring about?

dmmh

7:19 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



no...the example executes the function and displays the error messages, but the page 'inside' the function will not maintain state eg: no session is maintained, even though it should

coopster

9:35 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You should at least be getting E_NOTICE errors right now. You obviously must have a session started before you get to your function because you are checking session variables and assigning one to a passed parameter in the function. Then in the function you are starting a session again.


As of PHP 4.3.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.

I have found it to be a good practice to start my sessions in one place, and one place only, usually within a function and typically that is the authentication function.

Also, you have this within your second function ...

if (isset($session_valid)){
... that variable is always going to be set. You probably want to change the logic there to check if it is empty or not rather than isset.

dmmh

10:19 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



no error message whatsoever, if only that had been the case, it would mean the thing would be able to work perhaps with some more work :)

last comment was a valid one, silly, thanks :)

dmmh

10:24 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



the funny thing is, the session variables are set just fine, but somehow it still doesnt quite work. var_dump($_SESSION) inside the function contains everything that should be there.