3.0 the changes are relatively large. session supports redis, memcache, files, and database. This is the simplest. Here we will talk about how to store sessions using databases and redis.
First, because the manual is not updated, the manual describes how to use version 2.0. 3.0 is not used in this way. Here we use the database method. The biggest change is that the database has changed.
The code is as follows: |
Copy code |
Create table if not exists 'ci _ session '( 'Id' varchar (40) not null, 'IP _ address' varchar (45) not null, 'Timestamp' int (10) unsigned DEFAULT 0 not null, 'Data' blob not null, Primary key (id ), KEY 'ci _ sessions_timestamp' ('timestamp ') ); |
Please use this database creation method. The database storage method should be used according to its configuration instructions.
Of course, if you have reconstructed its database operation class, especially the addition of automatic master-slave read/write splitting, you need to reconstruct this session_database.php. If you have any questions, you can talk about it and it is very easy. The benefits of ci can be easily reconstructed.
2. Use redis
I only know how to use this code.
Check this file. Session_redis_driver.php
The code is as follows: |
Copy code |
Public function _ construct (& $ params) { Parent: :__ construct ($ params ); If (empty ($ this-> _ config ['SAVE _ path']) { Log_message ('error', 'session: No Redis save path configured .'); } Elseif (preg_match ('#(? : Tcp ://)? ([^:?] + )(? : \: (\ D + ))? (\?. + )? # ', $ This-> _ config ['SAVE _ path'], $ matches )) { Isset ($ matches [3]) OR $ matches [3] = ''; // Just to avoid undefined index notices below $ This-> _ config ['SAVE _ path'] = array ( 'Host' => $ matches [1], 'Port' => empty ($ matches [2])? NULL: $ matches [2], 'Password' => preg_match ('# auth = ([^ \ s &] +) #', $ matches [3], $ match )? $ Match [1]: NULL, 'Database' => preg_match ('# database = (\ d +) #', $ matches [3], $ match )? (Int) $ match [1]: NULL, 'Timeout' => preg_match ('# timeout = (\ d + \. \ d +) #', $ matches [3], $ match )? (Float) $ match [1]: NULL ); Preg_match ('# prefix = ([^ \ s &] +) #', $ matches [3], $ match) & $ this-> _ key_prefix = $ match [1]; } Else { Log_message ('error', 'session: Invalid Redis save path format: '. $ this-> _ config ['SAVE _ path']); } If ($ this-> _ config ['match _ IP'] = TRUE) { $ This-> _ key_prefix. = $ _ SERVER ['remote _ ADDR '].': '; } } |
According to this constructor, we found that the connection information of apsaradb for redis is in the configuration item save_path. So we can configure it like this
The code is as follows: |
Copy code |
$ Config ['sess _ save_path '] = '2017. 0.0.1: 127? Auth = admin & prefix = ci_sess: & database = 2 & timeout = 60 '; |
You can see the ip address and port according to the code. Then the password is followed by the matched auth =, and then it is very simple. Prefix is the prefix of session storage, database is the database number, and redis is the database sharding. Timeout connection timeout