In laravel, you can use the check method of Auth to check whether the user is logged in. If logged in, true will be returned; the syntax "use Illuminate\Support\Facades\Auth;if (Auth::check() ){//Login operation}".
The operating environment of this tutorial: Windows 7 system, Laravel 6 version, Dell G3 computer.
Determine whether the current user is logged in (authenticated)
You can use the check
method of the Auth
facade to check the user Whether it has been certified. If authenticated, will return true:
use Illuminate\Support\Facades\Auth; if (Auth::check()) { // 用户已经登录了... }
Although you can use the
check
method to confirm whether the user is authenticated, before allowing the user to access certain routes/controllers, Often middleware is still used to verify that the user is authenticated.
Extended knowledge:
Many applications require logging in before they can be operated. Laravel provides an auth tool to implement user authentication. And there is a config/auth.php to configure the auth tool.
Let’s take a look at the common methods of auth tools:
Auth::check();// 判断当前用户是否未登录 Auth::guest();// 判断当前用户是否未登录,与 check() 相反 Auth::guard();// 自定义看守器 默认为 `web` Auth::user();// 获取当前的认证用户,一个提供者的模型 Auth::id();// 获取当前的认证用户的 ID(未登录情况下会报错) Auth::attempt(['email' => $email, 'password' => $password],true);// 通过给定的信息来尝试对用户进行认证(成功后会自动启动会话),第一个数组就是认证的参数,第二个参数true就是'记住我'功能 Auth::login(User::find(1), $remember = false);// 登录一个指定用户到应用上,一般是登陆的参数通过后,执行login方法,保存session等登陆成功的操作 Auth::logout();// 使用户退出登录(清除会话)
Related recommendations: The latest five Laravel video tutorials
The above is the detailed content of How to determine whether to log in in laravel. For more information, please follow other related articles on the PHP Chinese website!