Snickbrack
Snickbrack

Reputation: 976

PHP 5.6 SESSION handling (writing, reading)

Im getting this notice on my executing website:

Notice: Undefined variable: _SESSION in *PATH* on line 25 which is this line of code $_SESSION["Registration_Error"] = TRUE; why is the $_SESSION-Variable unkown?

Do they changed the way one can save data in the Session-Variable/Cookie?

Haven't found something good on Google. Just this http://php.net/manual/de/book.session.php#116217 and I dont think that this helps much.

Ideas?

EDIT:

Full Code

<?php

session_start();

function __autoload($class_name) {
    include "../backend/classes/$class_name.php";
}

$Connection = new DB_Connect();
$User_Functions = new User_Functions();

$UserName = filter_input(INPUT_POST, "UserName");
$Password = filter_input(INPUT_POST, "Password");
$Mail = filter_input(INPUT_POST, "Mail");
$date = new DateTime();
$Registration_Date = $date->format("Y-m-d H:i:s");

if (!empty($UserName) && !empty($Password) && !empty($Mail)) {
    if (!$User_Functions->User_Exists($UserName)) {
        $Password_hashed = password_hash($Password, PASSWORD_DEFAULT);

        $Query = "INSERT INTO wordsusers (`wordsUserName`, `wordsUserPassword`, `wordsUserMail`, `wordsUserRegDate`) VALUES (:wordsUserName, :wordsUserPassword, :wordsUserMail, :wordsUserRegDate)";
        $Parameter = array(":wordsUserName" => $UserName, ":wordsUserPassword" => $Password_hashed, ":wordsUserMail" => $Mail, ":wordsUserRegDate" => $Registration_Date);
        $Registered = $Connection->Execute_PDO_Command($Query, $Parameter);
    } else {
        $_SESSION["Registration_Error"] = TRUE;
        header("Location: ../index.php");
    }
}

Upvotes: 1

Views: 3771

Answers (2)

fabtrons
fabtrons

Reputation: 11

The fundamental features and purposes of the $_SESSION, has not changed across the PHP versions after it deprecated $HTTP_SESSION_VARS in version 4, it's still properly maintained, persistent across pages and a Super Global variable. However its more fault tolerant in the lower versions of PHP, from 5.4 downward than from 5.5 upwards with 5.6 inclusive.

General rule, do not output anything before calling the session_start() functions, which initiates the creation of the session variable or re-initiation of the existing session variable.

However, the lower versions of PHP can tolerate a black gap before the session_start() statement and your scripts will still run just fine, but the higher versions does not allow this. So to answer your questions make sure that your scripts always starts like this.

before any other code, use the @ to suppress any warning, encase you have initiated it on a script that is linked to your current script.

Upvotes: 1

ALEXANDER LOZANO
ALEXANDER LOZANO

Reputation: 2034

allow all users to read and write to the directory. Fix the ownership and permissions so that www-data can both read and write (rwx). For instance:

chgrp www-data /var/lib/php5/sessions

chmod g+rwx /var/lib/php5/sessions

Upvotes: 2

Related Questions