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

jericopulvera's avatar

How do I get the ID of the authenticated user using plain php

how do I get the id of the authenticated user using plain php functions instead of Auth::user()->id; ?

0 likes
11 replies
jlrdw's avatar

Depends if you use cookie or session. An example of checking a login

public function chklog()
    {
        if (Session::get('loggin') == false) {
            Url::redirect('admin');
        }
    }

Example from another framework. But you could google php login script and get some good ideas.

1 like
jekinney's avatar

Look in the auth class source code. It uses plan php to keep tabs on the auth user.

1 like
jericopulvera's avatar

@jekinney when I tried to go to auth definition this is the code I get.

<?php

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Auth\AuthManager
 * @see \Illuminate\Contracts\Auth\Factory
 * @see \Illuminate\Contracts\Auth\Guard
 * @see \Illuminate\Contracts\Auth\StatefulGuard
 */
class Auth extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'auth';
    }
}
Snapey's avatar

I believe this works also $request->user()

but I am concerned why you ask. If there is some reason you cannot use the facade then maybe you are hoping to get the authenticated user in some separate php file, ie, not part of the framework. If this is the case then you will be disappointed.

1 like
jericopulvera's avatar

@Snapey yeah that's what I'm trying to do. my professor told me not to use any libraries in the algorithm i'm making for my newsfeed. so I was wondering how would I get the authenticated user ID without using any libraries.

Snapey's avatar
Snapey
Best Answer
Level 122

Sorry, but the user only exists because the framework is bootstrapped when index.php is loaded.

PHP does not natively support the concept of a user, although of course it does support sessions.

Without a library, you would need to start sessions, receive the login form from the user, validate it then store the user identifier in the session.

Next time the user returns, get the id from the session storage.

You will need some pretty old tutorials to cover this without using a framework because in real life, noone codes this from the ground-up any more. This might give you an idea.. https://www.formget.com/login-form-in-php/

1 like

Please or to participate in this conversation.