mrooks1984 Posted October 20, 2011 Share Posted October 20, 2011 i am trying to sort out a session now, i normally use the folllowing code to check if someone is logged in and if they are not to forward them to the login page, but i am getting this message: Notice: Undefined index: username in code: if ($_SESSION['username']) { }else{ include 'login.php'; die(""); } can someone help please. Quote Link to comment https://forums.phpfreaks.com/topic/249463-more-issues/ Share on other sites More sharing options...
floridaflatlander Posted October 20, 2011 Share Posted October 20, 2011 Did you set username at login? Have you tried to echo $_SESSION['username']; to see if you have a session with that name? Quote Link to comment https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280843 Share on other sites More sharing options...
AyKay47 Posted October 20, 2011 Share Posted October 20, 2011 that won't effect your code.. as it is a notice.. basically it is telling you that the specified index doesn not exist in the session array.. but you probably want to use empty or isset to check the session Quote Link to comment https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280844 Share on other sites More sharing options...
mrooks1984 Posted October 20, 2011 Author Share Posted October 20, 2011 thanks for your responces this is before i even login. the full code is <?php //start a seesion session_start(); // Include Main Core File. include '../_class/core.php'; // Include Config Files include '../config/config.php'; include '../config/db.php'; // Use Database $obj->connect(); if ($_SESSION['username']) { }else{ include 'login.php'; die(""); } ?> when i do this: <?php //start a seesion session_start(); // Include Main Core File. include '../_class/core.php'; // Include Config Files include '../config/config.php'; include '../config/db.php'; // Use Database $obj->connect(); echo $_SESSION['username']; ?> i get : Notice: Undefined index: username in and no echo of the username could you give me a example of how its checked as well please thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280852 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 As AyKay47 pointed out, you probably want to use isset() or empty() but because $_SESSION['username'] won't be set until a user logs in I would use isset(). if (isset($_SESSION['username'])) { }else{ include 'login.php'; die(""); } The problem I see with this setup (not knowing what login.php includes) is that this more than likely includes a form and the processing code which also sets the "username" to session. This not only is happening after the "if (isset($_SESSION['username']))" is called but I assume after content, i.e form etc is sent to the browser. I would make a page called login.php that has the form in the page "body"and all processing done before the html tags on this page. If username, password checks out you set the session. On all other pages I would have if (isset($_SESSION['username'])) { }else{ header ("location: login.php"); die(""); } Quote Link to comment https://forums.phpfreaks.com/topic/249463-more-issues/#findComment-1280924 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.