Jump to content

Sidebar on site (Controlled by PHP sessions?


KingBeau

Recommended Posts

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.

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';
}

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
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.