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

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等