Laravel session id does not match in database
I have a question about linking my database tables to my session table in Laravel 5.6.
I wanted to link who ever creates an email address in the user_emails
table to the session
table, so that in future I may be able to retrieve details about that transaction – like the ip address.
I am using the following code:
$email = new UserEmail();
$email->user_id = $user->id;
$email->address = $request->email;
$email->token = str_random(30);
$email->session_id = session()->getId();
$email->save();
This will save the session_id
field as (in this example) UkPhMBTVq73MX8KaS8CZy0vDG9wtDSNvSYhsRuvL
inside the user_emails table, with a user_id
of 62
.
However, when I check the sessions
table for the entry with user_id = 62
, the id
field is set to DuQq6JqcFL2HiGGLGJQ9hLNeXaxUAkdYTlJEX1Ag
.
It appears to me that the session id is changing immediately whilst saving the entry to user_emails
table. This also means I am unable to create a foreign key between user_emails.session_id
and sessions.id
as it throws a foreign key constraint issue.
Is this intended behaviour? I expected that the sessions table would create a new row for each new session, as opposed to overwriting the existing row.
Hopefully someone can shed some light on this. It would be nice to be able to link activity to the sessions
for future analysis. My logic in this case would be to check where the request originated from, should the User report the registration as as unauthorised when they receive their welcome email.
I of course have the session driver set to database, so that Laravel stores all my session data automatically.
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-session-id-does-not-match-in-database/
via Lzo Media
No comments:
Post a Comment