PHP - Notice: Undefined index

-1
public function get_session() 
    {
        $id = $_SESSION['login'];
        if(isset($id))
        {
            return $id;
        }
    }

Result

  

Notice: Undefined index: login in   C: \ xampp \ htdocs \ PROINSOR \ inc \ link.class.php on line 47

    
asked by Diego Sagredo 20.10.2016 в 03:28
source

3 answers

7

The problem occurs in the allocation id = $_SESSION['login']; because $_SESSION['login'] is not defined. It does not matter if you do the isset because it is being done AFTER using $_SESSION['login'] .

The solution is to move the isset to the condition if (and already you can simplify it since you will not need the variable id ). Something like this:

public function get_session() 
{
    if(isset($_SESSION['login']))
    {
        return $_SESSION['login'];
    }
}
    
answered by 20.10.2016 в 04:00
3

Hi, try the following:

public function get_session() 
    {
        $id = isset($_SESSION['login'])?$_SESSION['login']:0;
        if($id!==0)
        {
            return $id;
        }
    }

Greetings.

    
answered by 20.10.2016 в 03:55
1

The problem is that you are assigning the value of $ _SESSION ['id'] to $ id without having verified that the session variable comes with data, you can solve it with the following code and without using the variable $ id .

public function get_session() 
    {
        if( isset($_SESSION['login']) )
        {
            return $_SESSION['login'];
        }
    }
    
answered by 09.09.2018 в 02:18