0

I can't access session data. In index.php I have this code and $_SESSION['token'] is not empty:

<?php
include "classes/session.php";
include "classes/token.php";
// include "classes/login.php";

use Biboletin\Session;
use Biboletin\Token;
Session::start();
var_dump($_SESSION);
?>

Down in the code in the same file i also have line

<input type="hidden" name="token" value="<?php echo Token::generate(); ?>" />

This is the method generate() In token.php:

public static function generate()
{
    Session::start();
    return Session::set('token', bin2hex(random_bytes(32)));
}

The action of the form is pointing to login.php where I var_dump($_SESSION) and there is nothing. I have Session::start() in all files.

And this is the method start():

public static function start() 
{ 
  if (!self::$instance) { 
    self::$instance = new self(); 
  }
  return self::$instance;
}
Tiffany
  • 680
  • 1
  • 15
  • 31
biboletin
  • 21
  • 5
  • Have you tried `session_start();` instead of `Session::start();` ? – Zak Sep 27 '18 at 20:51
  • I'm guessing `Session::start()` is part of one of your own classes, please provide the code for the `start` method in your `Session` class. – Tiffany Sep 27 '18 at 20:54
  • Are you getting "Headers already sent" warnings in `login.php`? If so, you have to fix that. – Barmar Sep 27 '18 at 21:02
  • Did you start your session in login.php? – dontanios Sep 27 '18 at 21:06
  • I'll try tomorrow with session_start(); – biboletin Sep 27 '18 at 21:24
  • And this is the method start(): ` public static function start() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; }` – biboletin Sep 27 '18 at 21:25
  • 1
    You should edit your question and add the code to your question, instead of as a comment. With that said, you need `session_start` in there, dude. Sessions in PHP won't start without it. – Tiffany Sep 27 '18 at 21:37
  • I'm guessing `session_start()` is in the `Session` constructor – Phil Sep 27 '18 at 22:49
  • Possible duplicate of [Session variables not working php](https://stackoverflow.com/questions/19692157/session-variables-not-working-php) – Martin Sep 28 '18 at 13:51
  • @Martin no its different problem and the link provided dont fixed my problem – biboletin Sep 29 '18 at 13:55
  • 1
    You need to show *why* it's a different problem. You code looks completely wrong. You should show us your `phpinfo` output as well as your full `Session` class , please. – Martin Sep 29 '18 at 15:08
  • https://github.com/biboletin/php/blob/master/session.php this is the session class(please dont laught at it :D ) i saw the class in one site - i think it was stack exchange and i get the idea and i improvise with the code a little – biboletin Sep 30 '18 at 14:41

0 Answers0