KingBeau Posted July 19, 2012 Share Posted July 19, 2012 I'm after a code to let me have a sidebar show up over the right side of a page only when there's an active php session. Currently I have a site running and inside that site there's an "Admin page" running this code to authenticate users: <?php session_start(); if(!session_is_registered(username)){ header("location:login.php"); } ?> What can i do to make it so that i can have a sidebar show up on all of my pages, but only if there's an active session? I only create sessions for admin. The rest of the users are anonymous. Link to comment https://forums.phpfreaks.com/topic/265931-sidebar-on-site-controlled-by-php-sessions/ Share on other sites More sharing options...
scootstah Posted July 19, 2012 Share Posted July 19, 2012 First of all, session_is_registered() is deprecated as of PHP5.3 and removed as of PHP5.4. You should be using the $_SESSION superglobal now. So, it should be: if (isset($_SESSION['username'])). On to your problem. Ideally, you should have some reusable code to verify an active login - maybe a class method, or just a simple function. You could then use that to figure out if you should display the sidebar or not. if (is_admin()) { require 'sidebar.php'; } Link to comment https://forums.phpfreaks.com/topic/265931-sidebar-on-site-controlled-by-php-sessions/#findComment-1362640 Share on other sites More sharing options...
Donald. Posted July 19, 2012 Share Posted July 19, 2012 The function session_is_registered() was deprecated in PHP 5.3 and removed in 5.4 so I'd stay away from that. I'd use something else assign sessions. Something like this will work. <?php session_name('loginSession'); session_start(); $_SESSION['username'] = "Admin"; //Sets the sesision username var to Admin //Now we can use this to display content if the there is a session or username or by username //We can use it for everyone logged in or just a particular user if($_SESSION['username']) { //Your sidebar here for everyone logged in } if($_SESSION['username'] == "Admin") { //Your sidebar here for user with username Admin } ?> Link to comment https://forums.phpfreaks.com/topic/265931-sidebar-on-site-controlled-by-php-sessions/#findComment-1362641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.