-2

I am trying to pass a value using a session from one page to another, but it says:

Notice: Undefined index: user_id in path\file.php on line 61

The file where I am creating a session is as follows:

// Checking for session, if not then start session
if (!isset($_SESSION)) {
    session_start();
}
// Assigning user_id to session var
$_session['user_id'] = $row['user_id'];

This is the code by which, I am accessing the session variables:

if (!isset($_SESSION)) {
    session_start();
}
// Trying to access session var (previously defined)
$user_id = $_session['user_id'];

Where I am making a mistake. How can I fix this?

Note: To those who thinks that this might be a duplicate question, my answer is: It might be, but none among that solved my issue. As mine is because of case sensitivity for global variable and rest others are not this issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raja Gopal
  • 1,845
  • 1
  • 17
  • 34

4 Answers4

3

It should be $_SESSION not $_session.

 $_SESSION['user_id']=$row['user_id'];

 $user_id = $_SESSION['user_id'];

$_SESSION is a global variable and should be written in capital. When you are using the variable with small letters, in that case it doesn't get the variable because its never been defined and doesn't have the key user_id and hence you get the error. Hope this helps.

Kinshuk Lahiri
  • 1,468
  • 10
  • 23
2

The error is to vague, the user_id undefined index might be apart of $_SESSION or $row you can do some debugging with var_dump() to check which is which. Here are some tips for sessions as well.

A common mistake with sessions is forgetting to include session_start() at the top of each file and it is not needed to check if $_SESSION is set. Start it anyway!

Also, to avoid this error you should do a check to make sure that the index user_id exists. something like:

if ( isset( $_SESSION['user_id'] ) )

Make sure to use capital letters for SESSION, $_SESSION, not sure if it makes a difference but it is very common standard for server variables.

2
$_SESSION['user_id'] = $row['user_id'];

and in receiving side

$user_id = $_SESSION['user_id'];
RJParikh
  • 4,096
  • 1
  • 19
  • 36
Dhaval Naphade
  • 555
  • 2
  • 21
2

Working with SESSION requires that a session is active on all scripts that need access to the session data. This implies you should do something like this at the top of all the scripts needing access to any session data:

<?php // Notice there is no space before <?php
    // First check if the session exist before starting it.
    // Do this at the very top of each script that needs access to $_SESSION data
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

Then you may work with the session like so:

<?php
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

    // Some code...

    $_SESSION['user_id'] = $row['user_id'];

    // Some code...

And then again; you may get the session data like so:

<?php
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

    // Some code...

    $user_id = $_SESSION['user_id'];

    // Some code...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Poiz
  • 7,611
  • 2
  • 15
  • 17