Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

drannagg's avatar

Get userId from the session token

HI,

I am trying to get the user's Id from a session's file but I do not find the method to extract it.

Here is an example of the session file:

a:3:{s:6:"_token";s:40:"q0zxfyO5IvKOxrJHKFhicA79vtsgAa1Jpw5KKlZb";s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:50:"login_web_59ba36addc2b2f94860f014c7f58ea4e30989d";i:1;}

Could someone help me on this, please?

Thanks in advance.

0 likes
14 replies
drannagg's avatar

@vincent15000 I cannot use the request data because the user is not connected. The only information I have is the session cookie.

2 likes
vincent15000's avatar

@drannagg How is it possible to retrieve the connected user id if no user is connected ?

But if you have a session with a token, it means that a user is connected.

What are you trying to do ?

Sinnbeck's avatar

What session driver are you using? Why cant you get the user from the request? If they aren't logged in you cannot get the id

Maybe a start would be to get the session from a logged in user and try decrypting the values https://laravel.com/docs/9.x/encryption#decrypting-a-value

But explain what you are trying to do and there is probably a much better way

1 like
drannagg's avatar

I am using the File Manager for the sessions.

What I am trying to do is to make a request from my NextJS app to Laravel on the same server. Mainly, because I have to know if a user has the good privileges to access a page before rendering it and using getServerSideProps. The problem is that this request is made from the server to the server and not from the client to the server. So I can not use the classic Auth scheme. The only information I can get is the session cookie name. And I use it to make a request to a specific route like localhost/check_privileges?cookie_session=[value]

And when I decrypt the cookie name, I obtain the following pattern: b07a9cf767f4401c8b26feff3b5ef33b2b8570dd|wNH8G7HegwblPJenJLuiOjJDsAXRSMZlhMheUESa

The part after the '|' corresponds to the session file's name. I don't know for the first part.

Thanks in advance for your help! :)

1 like
Sinnbeck's avatar

@drannagg never used nextjs, but I am unsure why you cannot send a proper request with auth (I assume you using web routes with full session support)

1 like
vincent15000's avatar

@drannagg Ok so you have a back with Laravel and a front with NextJS.

What are you using to authenticate the users in the back ? Fortify ? Breeze ?

vincent15000's avatar

@drannagg With Sanctum, you only have to apply what is in the documentation and it should work.

Unless you have the back and the front on two different domains. Effectively Sanctum needs that both the back and the front are on the same domain.

drannagg's avatar

@vincent15000 @sinnbeck The problem is that Sanctum relies on sessions. And that the request that NextJS will make will start from the server and will not be able to get the sessions from the browser (I think...)

The scheme is as follows: 1 - The client connects and receives a session cookie. 2 - The client makes a request for a page. 3 - NextJS checks if the user is known and if he has enough privileges to render the page. This operation is made inside the server without asking to the client.

So I need to find a way to send information about the user from NextJS to Laravel. And when I use Sanctum, the answer is that the user is not authenticated.

1 like
jlrdw's avatar

@drannagg check the series here, I believe there are videos on nextjs. The lessons would explain the entire workflow.

1 like
drannagg's avatar

@vincent15000, @jlrdw , @sinnbeck ,

Hi to all. Thanks for you advices but my trouble was very specific and is not explained in the videos or tutorials. Finally, I created my own cookie that it is sent with the page request and use by the NextJS to identify the user, by making a request to confirm the value cookie with Laravel.

Please or to participate in this conversation.