Mangs
Mangs

Reputation: 1210

Using session variable to use info on different pages

i'm having a bit of a problem. I'm trying to set up a simple webpage with only three .php pages. I want a session variable $_SESSION['userID'] to be set when a user is logged in and I want the index page to show extra info if someone is logged in.

On index.php I want to show some info, if a user is logged in I want to show some extra info. login.php - simple log in form. login_exe.php - takes care of database connection and verification.

So this was my idea:

On index.php, check if session is started, if not: start.

<?php

if (!isset($_SESSION)) {
  session_start();
  echo "session started";
}

later on, check if $_SESSION['userID'] contains a value, if so: print a string

if($_SESSION['userID'] != null){
   echo "User logged in";
}

On login_exe.php i've almost the same code:

<?php

if (!isset($_SESSION)) {
  session_start();
    echo "session started";
}

in verification function:

$_SESSION['userID'] = $data['userID'];
header("Location: index.php");

The problem is that a new session is started on every page. How can I fix this and only start the session once? Thanks in advance

Upvotes: 1

Views: 104

Answers (3)

Str.
Str.

Reputation: 1439

You should not ask new answers (edit: questions) in comments. Be as systematic here as you are in coding.

How to end a session:

This gives room for discussion, because there is the session cookie, which is client side, and the session data, which is server side.

I recommend:

$_SESSION = null;

Reason: this will clear all login and other associated data immediately. It leaves the cookie intact, which is normally of no concern, since all associated data is gone.

Upvotes: 0

user1299518
user1299518

Reputation:

You should just put session_start() on top of documents that using sessions. Say, if you have 5 .php files that using sessions, then put 5 times the session_start() on top of them.

This is because session_start() sends headers and headers must be sent before any output (for example, any echo or whitespace).

Then, you should use something like isset($_SESSION["foo"]) and not just the entire $_SESSION array, where foo is something you set previously.

If you dont want sessions at all or need to reset the entire array, just call session_destroy() which effectively destroy the current session. Use unset($_SESSION["foo"]) when you want to get rid of a key.

Finally, you might get weird cases where you cannot read session key you write at. In these cases check what is the path of sessions and if they're writeable, or change their path:

$path = session_save_path();        // what is the path

is_writable($path);                 // can i write to it?

session_save_path("my/new/path");   // change the darn path; 
                                    // put -even- before session_start()!

:) glad i help

Upvotes: 1

Str.
Str.

Reputation: 1439

I think the PHP manuals are really good compared to ...ahm, so just read about session_start(). It says:

session_start() creates a session or resumes the current one (...)

so all you need is session_start() very early in your code. This must be executed on every request (maybe as include).

Your code checking the userId looks fine, one important hint here: you should know exactly what isset(), empty() and the like mean in PHP, so always have the comparision of comparison at hand.

Upvotes: 0

Related Questions