I have a login form in my laravel project and send the user login data in my controller method using ajax. While users successfully logged in I kept the user_name in session $_SESSION['name]
. But when I try to show that $_SESSION['name]
in my blade template view it gives error
local.ERROR: Undefined index: name
Here is my login controller method
public function signin(LoginFormValidation $request)
{
$user_password = $request->password;
$data = User::where('email','=',$request->email)->first();
if(Hash::check($user_password, optional($data)->password))
{
$_SESSION['name'] = $data->name;
echo "success";
}
else
{
echo "no";
}
}
Here is my blade template view
<ul class="cd-main-nav__list js-signin-modal-trigger">
<li><a class="cd-main-nav__item cd-main-nav__item--signup" href="#0">{{ $_SESSION['name'] }}</a></li>
</ul>
Why the username isn't stored in my session or if it stored why gives the error ?
Use laravel session helper function
Controller
public function signin(LoginFormValidation $request)
{
$user_password = $request->password;
$data = User::where('email','=',$request->email)->first();
if(Hash::check($user_password, optional($data)->password))
{
session()->put('name', $data->name);
echo "success";
}
else
{
echo "no";
}
}
Blade
<ul class="cd-main-nav__list js-signin-modal-trigger">
<li><a class="cd-main-nav__item cd-main-nav__item--signup" href="#0">{{ session()->get('name') }}</a></li>
</ul>
you forgot to start the session.
session_start();
Recommended way for versions of PHP >= 5.4.0
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.