我在Laravel 8中,我想在用户输入错误的凭据以login.blade.php登录到他的帐户时显示错误消息.

因此,在控制器中,我有以下内容:

if(Auth::attempt($credentials)){
    // log user in
}else{
    Session::flash('email', 'wrong credentials');
    return redirect()->back();
}

然后在Blade 式服务器中:

 @if(\Session::has('email'))
    @dd(1)
@endif

但不知何故,dd(1)美元并不奏效.这意味着会议邮箱没有提交.

我还在Blade 末尾添加了@php dump(session()->all()); @endphp以查看会话,但它是[]

那么,这里出了什么问题呢?我怎么才能解决这个问题呢?

以下是前config/session.php名:

<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Session Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the default session "driver" that will be used on
    | requests. By default, we will use the lightweight native driver but
    | you may specify any of the other wonderful drivers provided here.
    |
    | Supported: "file", "cookie", "database", "apc",
    |            "memcached", "redis", "dynamodb", "array"
    |
    */

    'driver' => env('SESSION_DRIVER', 'file'),

    /*
    |--------------------------------------------------------------------------
    | Session Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes that you wish the session
    | to be allowed to remain idle before it expires. If you want them
    | to immediately expire on the browser closing, set that option.
    |
    */

    'lifetime' => env('SESSION_LIFETIME', 120),

    'expire_on_close' => false,

    /*
    |--------------------------------------------------------------------------
    | Session Encryption
    |--------------------------------------------------------------------------
    |
    | This option allows you to easily specify that all of your session data
    | should be encrypted before it is stored. All encryption will be run
    | automatically by Laravel and you can use the Session like normal.
    |
    */

    'encrypt' => false,

    /*
    |--------------------------------------------------------------------------
    | Session File Location
    |--------------------------------------------------------------------------
    |
    | When using the native session driver, we need a location where session
    | files may be stored. A default has been set for you but a different
    | location may be specified. This is only needed for file sessions.
    |
    */

    'files' => storage_path('framework/sessions'),

    /*
    |--------------------------------------------------------------------------
    | Session Database Connection
    |--------------------------------------------------------------------------
    |
    | When using the "database" or "redis" session drivers, you may specify a
    | connection that should be used to manage these sessions. This should
    | correspond to a connection in your database configuration options.
    |
    */

    'connection' => env('SESSION_CONNECTION', null),

    /*
    |--------------------------------------------------------------------------
    | Session Database Table
    |--------------------------------------------------------------------------
    |
    | When using the "database" session driver, you may specify the table we
    | should use to manage the sessions. Of course, a sensible default is
    | provided for you; however, you are free to change this as needed.
    |
    */

    'table' => 'sessions',

    /*
    |--------------------------------------------------------------------------
    | Session Cache Store
    |--------------------------------------------------------------------------
    |
    | While using one of the framework's cache driven session backends you may
    | list a cache store that should be used for these sessions. This value
    | must match with one of the application's configured cache "stores".
    |
    | Affects: "apc", "dynamodb", "memcached", "redis"
    |
    */

    'store' => env('SESSION_STORE', null),

    /*
    |--------------------------------------------------------------------------
    | Session Sweeping Lottery
    |--------------------------------------------------------------------------
    |
    | Some session drivers must manually sweep their storage location to get
    | rid of old sessions from storage. Here are the chances that it will
    | happen on a given request. By default, the odds are 2 out of 100.
    |
    */

    'lottery' => [2, 100],

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Name
    |--------------------------------------------------------------------------
    |
    | Here you may change the name of the cookie used to identify a session
    | instance by ID. The name specified here will get used every time a
    | new session cookie is created by the framework for every driver.
    |
    */

    'cookie' => env(
        'SESSION_COOKIE',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Path
    |--------------------------------------------------------------------------
    |
    | The session cookie path determines the path for which the cookie will
    | be regarded as available. Typically, this will be the root path of
    | your application but you are free to change this when necessary.
    |
    */

    'path' => '/',

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => env('SESSION_DOMAIN', null),

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you when it can't be done securely.
    |
    */

    'secure' => env('SESSION_SECURE_COOKIE'),

    /*
    |--------------------------------------------------------------------------
    | HTTP Access Only
    |--------------------------------------------------------------------------
    |
    | Setting this value to true will prevent JavaScript from accessing the
    | value of the cookie and the cookie will only be accessible through
    | the HTTP protocol. You are free to modify this option if needed.
    |
    */

    'http_only' => true,

    /*
    |--------------------------------------------------------------------------
    | Same-Site Cookies
    |--------------------------------------------------------------------------
    |
    | This option determines how your cookies behave when cross-site requests
    | take place, and can be used to mitigate CSRF attacks. By default, we
    | will set this value to "lax" since this is a secure default value.
    |
    | Supported: "lax", "strict", "none", null
    |
    */

    'same_site' => 'lax',

];

推荐答案

  1. 转到App/Http/Kernel.php文件.
  2. 将此行\Illuminate\Session\Middleware\StartSession::class添加到$middleware数组中.请查看下面的截图.

screenshot

就这样.现在让我们测试一下会话.


如果您仍然有问题,请try 以下方法来存储数据. 我希望这能帮上忙.

try 使用全局session()助手函数,如下所示.

-- To Store An Array

Session::push('key', $array);

//or

session()->push('key', $array);

-- Store Session By a Key

Session::put('session_key', 'value');

//or

session(['key'=>'value']);

-- Retrieving Session By Key

Session::get('key');

//or

session('key');

-- Get All Data From The Session

Session::all();

//or

session()->all();

Php相关问答推荐

如何更改数据表行背景 colored颜色

如何减少Laravel分页中的链接数

允许在WooCommerce管理员优惠券列表中显示自定义优惠券类型

在Laravel中修改JSON输出

PHP Laravel Web应用启动时间&>15秒

按列值将二维数组排序为不超过N个的递增组

如何在WordPress REST API中判断应用程序密码

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

订单完成后创建 WooCommerce 促销代码

主机中未检索用户表数据

将自定义保存金额移至 Woocommerce 简单产品中的价格以下

使用产品自定义字段作为 WooCommerce 中特定用户角色的折扣百分比

为什么 php 只能在我的 Nginx Web 服务器的某些目录中工作?

使用用户元数据填写 woocommerce checkout 字段

如何搜索和替换 XML 文件中的一段文本?

使用 PHP 的 OAuth 2.0 MAC

php如何删除括号内的百分比值

使用 Process facade 在外部 Laravel 项目上运行 Artisan 命令会返回主项目的数据库错误

PHP Loop 在迭代中使用修改后的数据

在 EasyAdmin 中添加全局操作,将我的按钮放在表格下方而不是上方