Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session is empty after redirect #300

Open
sadalsuud opened this issue Feb 22, 2024 · 7 comments
Open

session is empty after redirect #300

sadalsuud opened this issue Feb 22, 2024 · 7 comments

Comments

@sadalsuud
Copy link

sadalsuud commented Feb 22, 2024

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

@eypsilon
Copy link

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?

@sadalsuud
Copy link
Author

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

@eypsilon
Copy link

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().

@sadalsuud
Copy link
Author

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 ??

@eypsilon
Copy link

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.

@sadalsuud
Copy link
Author

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

@eypsilon
Copy link

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

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

var_dump($_SESSION);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants