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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.