VincentVegas Posted September 16, 2012 Share Posted September 16, 2012 Hello. I have built a website where every page starts with: session_start(); include_once "sqlsettings.inc.php"; include_once "funcs.inc.php"; $user_id = $_SESSION['user_id']; In "sqlsettings.inc.php" I define and open a mysql database that is used by UserNick() (see below) to get the users nick. function ConnectDB() { GLOBAL $host, $user, $pass, $conn_id, $db; $conn_id = mysql_connect($host , $user , $pass) OR DIE ("ERROR:".$php_errormsg); mysql_select_db($db , $conn_id) OR DIE ("Connection failed:".$php_errormsg); } ConnectDB(); In "funcs.inc.php" I define a bunch of small functions like these two: function UserLoggedin() { return !empty($_SESSION['user_id']); } function UserNick($id) { $sql = "SELECT * FROM auth_user WHERE id='$id'"; $result = mysql_query($sql); if ($line = mysql_fetch_array($result)) return $line['user']; else return ""; } $user_id obviously stores the id if a user is logged in. Everything has been working fine for years, but I wanted to "optimize" the code a bit, so I copied the lines from abough into a new file called "header.inc.php" and replaced the lines with include "header.inc.php". And now the miracle starts. My menue "navi.php", where those functions defined in functions.inc.php are called, does not work when I make this change. The code that calls the functions at the top of navi.php is: <? if ( UserLoggedin() ) echo " logged in as:<br> <strong>" . UserNick($user_id) . "</strong><p>"; ?> The script seems to stop when it calls the first function, as there is no output. I made several tests and it shows that only this following code (with all original includes) works: session_start(); include_once "header.inc.php"; include_once "sqlsettings.inc.php"; include_once "funcs.inc.php"; $PHP_SELF = $_SERVER['PHP_SELF']; $user_id = $_SESSION['user_id']; Without session_start(); in navi.php, I get a menue but with no session variables, so no "logged in" output at all like when you are not logged in. With session_start(); but without include_once "sqlsettings.inc.php"; I get a menue but UserNick() returns "", so the database does not seem to be active. The included php files in header.inc.php do not seem to be included at all. From what I have read it is allowed to use include in an included file, so where lies the error? Regards Thomas Quote Link to comment https://forums.phpfreaks.com/topic/268442-session-with-included-include-does-not-work/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.