Author: Shiannizhilu public number: Zhixing Research Institute
16.1 using cookies
1. First, there are two ways to obtain cookies, as follows:
// Use request()->cookie to obtain decrypted cookie information
return request()->cookie('laravel_session');
// Use cookies :: also available, introduce Illuminate\Support\Facades\ cookies;
return Cookie::get('laravel_session');
Copy the code
Note:
Cookies in Laravel are encrypted, and the native cookie method can only get encrypted information
If you want a cookie is not encrypted, the Middleware folder Settings: Http/Middleware/milldelEncryptCookies. PHP
protected $except = [
//
'name'
];
Copy the code
2, use the response() method to create a cookie:
// The response() method writes a cookie
return response('Hello Cookie')->cookie('name'.'Mr.Lee'.10);
Copy the code
Parameter 3 is the expiration time in minutes. The value must return, otherwise it cannot be written
You don’t usually use this.
Use Cookie::queue() to write cookies.
// I recommend this. It is much cooler
Cookie::queue('age'.100.10);
Copy the code
4, use helper function cookie() to create cookie instances, and then write, more flexible;
// Helper function to create an instance to make writing more flexible
$cookie = cookie('gender'.'male'.10);
$cookie = $cookie
// Finally write
Cookie::queue($cookie);
Copy the code
Helper function full version:
// Full version, the latter four: path, domain name, HTTPS, only HTTP
cookie($name.$value.$minutes.$path.$domain.$secure.$httpOnly)
Copy the code
16.2 use Session
1, first, start the Web, the default session, through the following code to get all;
// Use request() :
return request()->session()->all();
// You can also use:
return Session::all();
Copy the code
Return result:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []}}Copy the code
Use get() to get a session;
// Get one of the sessions
return request()->session()->get('_token');
// The following method also works
return Session::get('_token');/ / output: RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC
// For parameter 2, the closure is set to default
return request()->session()->get('name'.function () {
return 'no session name';
});
// Output: no session name
Copy the code
3. The helper function session(), which can be fetched and default set
// Get the session value
return session('_token');/ / output: RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC
// Get the session value and set the default value
return session('name'.'no session name');// Output: no session name
Copy the code
4. There are two ways to determine whether a session exists:
// Check whether it exists and is not null
return Session::has('name');
return request()->session()->has('name'); // Support request() mode;
// Check whether it exists, even if it is null
return Session::exists('name');
return request()->session()->exists('name'); // Support request() mode;
Copy the code
5. You can use helper functions to pass an array or put() to store session values.
// Use helper functions to pass the array
session(['name1'= >'Joe']);
// Use the put() method
Session::put('name2'.'bill');
// Support request() storage
request()->session()->put('name3'.'Cathy');
// Look at the results
return Session::all();
Copy the code
Results:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []},"name1": "Zhang"."name2": "Bill"."name3": "Fifty"
}
Copy the code
6, use the push() method, can store arrays, support request() mode;
// Session array
Session::push('info.name'.'Joe'); Request ()->session()->push('info.name', 'three ')
Session::push('info.name'.'bill'); Request ()->session()->push('info.name', 'id ')
Session::push('info.name'.'Cathy'); Request ()->session()->push('info.name', 'zhao 6 ')
// Look at the results
return Session::all();
Copy the code
Results:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []},"info": {
"name": [
"Zhang"."Bill"."Fifty"]}}Copy the code
8, the use of flash() method, automatically delete after obtaining, support request() mode;
// The stored session can only be retrieved once and then automatically deleted. Flash is also called flash data
Session::flash('name'.'Joe');
// Look at the results
return Session::all();
Copy the code
At this point, the execution result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": [
"name"]},"name": "Zhang"
}
Copy the code
Then refresh the page by commenting the above code to create Flash. The result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": [
"name"]."new": []},"name": "Zhang"
}
Copy the code
Then refresh again, and the result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []}}Copy the code
That’s the process.
9. If flash data is used, do not delete the data immediately. Use reflash() instead;
// This request to obtain, do not delete data, to the next request to delete, this is to save all flash data
Session::reflash();
// Save the individual delete data
Session::keep(['name']);
Copy the code
Example:
First create two flash data:
Session::flash('name'.'Joe');
Session::flash('id'.'10001');
// Look at the results
return Session::all();
Copy the code
The result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": [
"name"."id"]},"name": "Zhang"."id": "10001"
}
Copy the code
Modify the code as:
//Session::flash('name', 'name');
//Session::flash('id', '10001');
// Save the individual delete data
Session::keep(['name']);
// Look at the results
return Session::all();
Copy the code
The result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": {
"1": "id"
},
"new": [
"name"]},"name": "Zhang"."id": "10001"
}
Copy the code
Then change the code to:
//Session::flash('name', 'name');
//Session::flash('id', '10001');
// Save the individual delete data
//Session::keep(['name']);
// Look at the results
return Session::all();
Copy the code
The result is:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": [
"name"]."new": []},"name": "Zhang"
}
Copy the code
If I refresh again, my name is gone. Well, that’s the process.
10. Forget () is used to delete one or more session data. Request () is supported.
// Delete a piece of data
Session::forget('name'); Session::forget(['name'])
return Session::get('name');
// Delete a piece of data and return
Session::pull('info');
// Delete all data
Session::flush();
Copy the code
Example:
// Add some test data:
Session::put('id'.'10001');
Session::push('info.name'.'Joe');
Session::push('info.name'.'bill');
Session::push('info.name'.'Cathy');
// Look at the results
return Session::all();
Copy the code
Output:
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []},"id": "10001"."info": {
"name": [
"Zhang"."Bill"."Fifty"]}}Copy the code
Test 1:
Session::forget('id');
// Look at the results
return Session::all();
Copy the code
Test 1 result (id deleted) :
{
"_token": "RA5s7slX29z04Yoq5B1P7ZJWHecjfRppacaKTsZC"."_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []},"info": {
"name": [
"Zhang"."Bill"."Fifty"]}}Copy the code
Test 2:
return Session::pull('id');
Copy the code
Test 2 Results:
10001
Copy the code
Here a piece of data is deleted and the deleted data is returned
Test 3:
// Delete all data
Session::flush();
// Look at the results
return Session::all();
Copy the code
Test 3 Results:
[]
Copy the code
All that’s left is an empty array ha ha ha
11. Regenerate session ids with regenerate().
It clears the session and regenerates it:
// regenerate the SessionID
Session::regenerate();
// Look at the results
return Session::all();
// obtain the SessionID,
return Cookie::get('laravel_session');
Copy the code
Output:
{
"_previous": {
"url": "http://127.0.0.1:8000/session/index"
},
"_flash": {
"old": []."new": []},"_token": "Gr28KwYuEgDvs0k0N4RAWSkrnO7zDEQCnNCYS3S3"
}
Copy the code
Compared with the above, the token has changed.
The above.