user1341676
user1341676

Reputation:

PHP session variables life

Newbie question, but I'm wondering if I'm missing something elementary here.

If I register a session variable in a page - isn't this variable supposed to be accessible from another page on the same site?

First, I register a variable in the file session_var_register.php:

<?php
    $_SESSION["myusername"] = 'user';
    if (isset($_SESSION['myusername'])) {
        echo 'Session var myusername is set to '.$_SESSION['myusername'];
    }
?>

When I open this page, it writes:

Session var myusername is set to user

As expected.

Then I open another tab and another page, check_session_var.php:

<?php
if (isset($_SESSION['myusername'])) {
    echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>

This page is blank.

Isn't the point of a session variable that it should be accessible in the browser session, until the session is programatically destroyed or the browser closed?

I'm using IE 8 and Firefox 24, btw. Identical results.

Upvotes: 0

Views: 623

Answers (5)

Daniele Vrut
Daniele Vrut

Reputation: 2873

You forgot

session_start() 

On top, before using

$_SESSION

PS: Remember to call session_start() in every page you want to use $_SESSION.

Upvotes: 2

Mandip Darji
Mandip Darji

Reputation: 775

First Of all start session on that page

session_start();

your page like this way

<?php

session_start();

if (isset($_SESSION['myusername'])) {
    echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>

Upvotes: 0

Tech Tech
Tech Tech

Reputation: 354

Method

session_start();

Description

session_start() creates a session or resumes the current one based on a session identifier >passed via a GET or POST request, or passed via a cookie.

Usage in your case (and in the most of cases): Put it before the $_SESSION usage.

Reference: session_start()

Upvotes: 0

Artur
Artur

Reputation: 7257

Your session variables will be available on different pages of the same site but on top of each of these pages you must have at least:

session_start();

It works but not in all cases. You must also use the same session name (essentially a cookie name that stores id of your session) on all pages. Moreover cookies (which are essential (mostly) for sessions to work) may be made visible only in specific directory. So if for example you share the same host with other guys that use sessions too you do not want to see their variables and vice versa so you may want to have sth like that:

1) session_name( 'my_session_id' );
2) session_set_cookie_params( 0, '/my_dir', $_SERVER['HTTP_HOST'], false, true );
3) session_start();

You may also want to see your session variables on other servers and in such case custom session handlers may be useful. Take a day or two to implement yourself - great way to understand how sessions work hence I recommend.

Upvotes: 0

Scott Helme
Scott Helme

Reputation: 4799

The PHP docs state that you must call session_start() to start or resume a PHP session. This must be done before you try to access or use session variables. Read more here.

session_start();

Upvotes: 2

Related Questions