Check session variable in php

Asked 10 years, 3 months ago

Viewed 79k times

I want to do something like this:

if [$_SESSION['errors'] exists]
{
    //Do stuff
}

I want to create a session on page1 and then go to page2 where it will check for errors, if there are errors it returns to page1 with the errors.

But page1 will give errors if the variable hasn't been created yet on page 2.

If I do $_SESSION['errors'] == "" on page1 it will reset the variable so that's no good.

asked Jun 1, 2012 at 19:05

if [isset[$_SESSION['errors']]]
{
    //Do stuff
}

answered Jun 1, 2012 at 19:07

AnasAnas

5,4865 gold badges40 silver badges69 bronze badges

1

use isset[] and empty[] php function.

if [isset[$_SESSION['errors']] && !empty[$_SESSION['errors']]] {
    // ...
} 

answered Jun 1, 2012 at 19:10

if [!isset[$_SESSION['id']] || [trim[$_SESSION['id']] == '']] {
     // do stuff
}

answered May 15, 2015 at 4:02

Asked 10 years, 3 months ago

Viewed 79k times

I want to do something like this:

if [$_SESSION['errors'] exists]
{
    //Do stuff
}

I want to create a session on page1 and then go to page2 where it will check for errors, if there are errors it returns to page1 with the errors.

But page1 will give errors if the variable hasn't been created yet on page 2.

If I do $_SESSION['errors'] == "" on page1 it will reset the variable so that's no good.

asked Jun 1, 2012 at 19:05

if [isset[$_SESSION['errors']]]
{
    //Do stuff
}

answered Jun 1, 2012 at 19:07

AnasAnas

5,4865 gold badges40 silver badges69 bronze badges

1

use isset[] and empty[] php function.

if [isset[$_SESSION['errors']] && !empty[$_SESSION['errors']]] {
    // ...
} 

answered Jun 1, 2012 at 19:10

if [!isset[$_SESSION['id']] || [trim[$_SESSION['id']] == '']] {
     // do stuff
}

answered May 15, 2015 at 4:02

[PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8]

$_SESSIONSession variables

Description

An associative array containing session variables available to the current script. See the Session functions documentation for more information on how this is used.

Notes

Note:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Tugrul

7 years ago

Creating New Session
==========================

Getting Session
==========================

Updating Session
==========================

Deleting Session
==========================

Chủ Đề