PHP-Auth icon indicating copy to clipboard operation
PHP-Auth copied to clipboard

session is empty after redirect

Open sadalsuud opened this issue 1 year ago • 8 comments

I have a login form on login.php that action's form go to LoginController.php In LoginController.php I have this:

require_once '../vendor/autoload.php';

$db = \Delight\Db\PdoDatabase::fromDsn(new \Delight\Db\PdoDsn('mysql:dbname=blabla;host=localhost;charset=utf8mb4', 'blabla', 'blabla'));

$auth = new Auth($db);
$auth->loginWithUsername($_POST['username'], $_POST['password']);

 session_start();
 $_SESSION['auth'] = $auth;    

And I redirect to another php file: home.php and there I have:

session_start();
$auth = $_SESSION['auth'];
var_dump($_SESSION);

But the $_SESSION is empty ... that var_dump prints : array(0) { }

The login is working but when I redirect and I stay on home.php no data session, but session_id is the same in LoginController and home.php

What is wrong? Help please, I am undertanding how to use this great library thanks

sadalsuud avatar Feb 22 '24 18:02 sadalsuud

I don't know if it will fix the problem, but session_start(); must be called before anything else, before require_once. Is error_reporting on?

eypsilon avatar Feb 23 '24 22:02 eypsilon

No nothing, It so same. Do you have a example, how do you pass data from script to another script? I think I am doing something wrong

sadalsuud avatar Feb 24 '24 17:02 sadalsuud

Do you have a example

Well, it should work, like you are doing it. Try, if your SESSION works at all, save a string in $_SESSION['test'] = 'Test var';, redirect and check the SESSION. The target after the redirect needs also a session_start().

eypsilon avatar Feb 24 '24 21:02 eypsilon

What you say I did that already. The sesión works if I do not use login() or loginWithUsername() methods then, how I do go to use the library ??

sadalsuud avatar Feb 24 '24 23:02 sadalsuud

Sorry, you don't need to do the SESSION-handling, it's done internally by the lib. And the target-page (or URL) you redirect to needs to require the library, too.

eypsilon avatar Feb 26 '24 15:02 eypsilon

Then on the Target-page I do require_once '../vendor/autoload.php'; But how I do access the $auth object on the Target-page ? I try it and Tell you

sadalsuud avatar Feb 26 '24 16:02 sadalsuud

Chack the SESSION on the target Page, it should be filled after the require

require_once '../vendor/autoload.php';

var_dump($_SESSION);

eypsilon avatar Feb 26 '24 20:02 eypsilon

Library automatically calls for session when it is needed. You don't need to initiate it.

ponasromas avatar Oct 11 '24 07:10 ponasromas

Thanks for helping here!

Just in case anyone has a similar problem:

  • There is no need to call session_start() usually, as the library does this automatically. But if you need to call session_start() before using this library to access some of your own application data in the session, that’s also fine.
  • There is no need to store the Auth instance in the $_SESSION array, i.e. don’t write $_SESSION['auth'] = $auth, because any data needed by the library is managed by the library and stored there, and otherwise, you only need to store your own application data there. The Auth instance, on the other hand, needs to be created once per script or per request and accessed wherever you need it. Pass it to functions, or make it a global variable, if that’s what you need. But in another script, on another page, or for another request, create a new Auth instance again.

ocram avatar May 20 '25 14:05 ocram