session_start(); if(isset($_SESSION['user_id'])) { $user_id = $_SESSION['user_id']; // do something with user_id } else { // redirect to login page }
if(Auth::attempt(['email' => $email, 'password' => $password])) { $user = Auth::user(); $user_id = $user->id; // do something with user_id } else { // redirect to login page }In this example, the Laravel authentication system takes care of checking the user's email and password against a database of users, and if the authentication is successful, the user object is retrieved (which includes the user_id) using the `Auth::user()` method. The package/library used in this example is the Laravel authentication system.