Forum Moderators: coopster
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?
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?
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.