简体   繁体   English

PHP Laravel:如何设置或获取会话数据?

[英]PHP Laravel: How to set or get Session Data?

I want to store some data in session for some testing purpose.我想在会话中存储一些数据用于某些测试目的。 I have written the session code also in controller.我也在控制器中编写了会话代码。 But in console.log ->resources -> session I couldn't find any value which I stored.但是在 console.log ->resources -> session 我找不到我存储的任何值。 If anyone help me to find the mistake which I have done in my controller please.如果有人帮我找到我在控制器中犯的错误,请。

Here is my controller:这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Department;
use App\Http\Requests;
use Cookie;
use Tracker;
use Session;

 public function postdepartmentSave(Request $request)
    {
         $this->validate($request,[
            'code' => 'required|min:2|max:7|unique:departments',          
            'name' => 'required|unique:departments',          
            ]);
            $department = new Department();           
            $department->code = $request->Input(['code']);            
            $department->name = $request->Input(['name']);
              $name=   $department->name;
         Session::put('name', $name);
        dd($name);
            $department->save();            
            return redirect('departmentSavePage');          
    }

Storing data存储数据

To store data, you can use:要存储数据,您可以使用:

Session::put('variableName', $value);

There is also another way, through the global helper:还有另一种方式,通过全局助手:

session(['variableName' => $value]);

Getting data获取数据

To get the variable, you'd use:要获取变量,您可以使用:

Session::get('variableName');

You haven't made a coding mistake.你没有犯编码错误。 The issue is that you're getting a little confused between server side sessions and HTML5's sessionStorage.问题是您在服务器端会话和 HTML5 的 sessionStorage 之间有点混淆。

The Resources->Session Storage view on the Chrome Developer Tools is only showing you the information stored in the HTML5 sessionStorage object. Chrome 开发人员工具上的Resources->Session Storage视图仅向您显示存储在 HTML5 sessionStorage 对象中的信息。 This is client side session information that is accessed via client side javascript, and is completely separate from any type of server side session information.这是通过客户端javascript访问的客户端会话信息,与任何类型的服务器端会话信息完全分开。

For example, run sessionStorage.setItem('key', 'value');例如,运行sessionStorage.setItem('key', 'value'); in your javascript console, and you will see that data show up in the Resources->Session Storage view.在您的 javascript 控制台中,您将看到该数据显示在Resources->Session Storage视图中。

Your PHP/Laravel code is dealing with server side sessions.您的 PHP/Laravel 代码正在处理服务器端会话。 This information is not going to show up in any type of Developer Tools view because it is not meant to be accessed by client side tools.此信息不会显示在任何类型的开发人员工具视图中,因为客户端工具不打算访问它。 By default, Laravel uses file based session storage, which means your session information is stored in files on your server (in storage/framework/sessions ).默认情况下,Laravel 使用基于文件的会话存储,这意味着您的会话信息存储在服务器上的文件中(在storage/framework/sessions )。 Even if you change the session storage to use cookies, the session information will be encrypted so that the client side cannot read the data.即使您将会话存储更改为使用 cookie,会话信息也会被加密,客户端无法读取数据。 This is for security purposes.这是出于安全目的。

You can read more about Laravel sessions here .您可以在此处阅读有关Laravel 会话的更多信息。
You can read more about HTML5 sessionStorage here .您可以在此处阅读有关HTML5 sessionStorage 的更多信息。

if you have existing value on that session variable, first you need to clear it.如果您在该会话变量上有现有值,首先您需要清除它。 then you can replace new value like this.那么你可以像这样替换新值。

 session()->forget('key');
 session()->put('key', 'value');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM