duangou6446 2015-11-02 15:06
浏览 20

用户未登录时的会话错误,在php [关闭]

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">&nbsp;&nbsp;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

  • 写回答

2条回答 默认 最新

  • doujia9833 2015-11-02 15:13
    关注

    In general you have to Start a session before you attempt to use a session variable

    So change this code from

    if (!isset($_SESSION['name'])) {
        session_start();
    

    to

    session_start();
    if (!isset($_SESSION['name'])) {
    

    Also you seem to be trying to create a class method but you have not created it inside a class. In fact that is completely the wrong place to try and create a class.

    i.e. this code

     function __construct() {
         parent::__construct();
         $this->load->library('session');
         $username= $_SESSION['name'];
         $_SESSION['name'] = $this->session->userdata('name');
    }   
    

    BUT, and more importantly, as you appear to be using codeigniter you dont need to do anything to be able to access the session. However codeigniter provides a wrapper for all session access. So I suggest you actually read the manual, its A radical idea I realise, but when all else fails!!!

    Read The Manual

    评论

报告相同问题?