I'm using sessions in my project.I do not need to keep any session records in my database. What I simply need is to display a page when the admin logged in. I do not have much knowledge of using sessions but I was able to have my sessions as I want when the admin logs into the system.I'm displaying the username on my header.My problem is that it throws an error from the session variable when nobody is logged in.. please help me to find my mistake..
//I start my sessions from the login functionality as below
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$a = $_POST['name'];
$b = $_POST['password'];
$c = authorize($a, $b);
if($c == "admin")
{
session_start();
$_SESSION['name']=$_POST['name'];
$_SESSION['password']=$_POST['password'];
header('Location:index.php?page=ap');
}
//Here is the place I'm accessing it. //header.php
<?php
if (!isset($_SESSION['name'])) {
session_start();
function __construct() {
parent::__construct();
$this->load->library('session');
$username= $_SESSION['name'];
$_SESSION['name'] = $this->session->userdata('name');
}
}
?>
//where I'm accessing for the session in the same page
<?php
if($_SESSION['name'] != null){//throws the error from here when not logged in
?>
<div id='ABC'>
<div style="padding-top: 120px;padding-left: 800px;color: #fff;font-weight: bold;font-size: 14px; ">
Hello <?php echo $_SESSION['name'] ?>
<a class="ABC" style="color: #fff; font-weight: bold;font-size: 14px;" href="<?php echo base_url(); ?>index.php?page=logout"> Logout</a>
<?php session_destroy(); ?>
</div>
</div>
<?php } ?>
//here is my error A PHP Error was encountered Severity: Notice Message: Undefined index: name Filename: common/header.php Line Number: 145