Put session into cache (redis), DB, sessionredis
Why should SESSION be saved in cache
As far as PHP is concerned, the session supported by the language itself is saved to a disk file in the form of a file and saved in a specified folder. The saved path can be set in the configuration file or use the function session_save_path() in the program. Set it up, but there are drawbacks to doing so,
<p>第一就是保存到文件系统中,效率低,只要有用到session就会从好多个文件中查找指定的sessionid,效率很低。</p> <p>第二就是当用到多台服务器的时候可能会出现,session丢失问题(其实是保存在了其他服务器上)。</p>
Of course, saving in the cache can solve the above problem. If you use PHP's own session function, you can use the session_set_save_handler() function to easily re-control the session processing process. If you don't use PHP's session series functions, you can write a similar session function yourself. It's also possible. This is the project I'm working on now. It will calculate the hash as the sessionId based on the user's mid and login time. Every time it is requested, The sessionId must be added to be legal (it is not needed when logging in for the first time, the sessionId will be created at this time and returned to the client). This is also very convenient, concise and efficient. Of course, what I am mainly talking about in this article is "manipulating things" in PHP's own SESSION.
SESSION saved in cache
PHP saves the cache to redis. You can use the configuration file to modify the processing and saving of the session. Of course, you can also use the ini_set() function in the program to modify it. This is very convenient for testing. I will use it here. In this way, of course, it is recommended to use configuration files in a production environment.
<?<span>php </span><span>ini_set</span>("session.save_handler", "redis"<span>); </span><span>ini_set</span>("session.save_path", "tcp://localhost:6379"<span>); </span><span>session_start</span><span>(); </span><span>header</span>("Content-type:text/html;charset=utf-8"<span>); </span><span>if</span>(<span>isset</span>(<span>$_SESSION</span>['view'<span>])){ </span><span>$_SESSION</span>['view'] = <span>$_SESSION</span>['view'] + 1<span>; }</span><span>else</span><span>{ </span><span>$_SESSION</span>['view'] = 1<span>; } </span><span>echo</span> "【view】{<span>$_SESSION</span>['view']}";
Here the session.save_handler method is set to redis, and session.save_path is the address and port of redis. After setting, refresh, and then look back at redis. You will find that the sessionId generated in redis is the same as the one requested by the browser.
open(string $savePath, string $sessionName); //open is similar to a constructor and will be called when starting a session, such as after using the session_start() function
close(); //Similar to the destructor of a class, it is called after the write function is called.
will also be executed after session_write_close()read(string $sessionId); //Called when reading session
write(string $sessionId, string $data); //Called when saving data
destory($sessionId); //When destroying the session (session_destory() or session_regenerate_id()),
will be calledgc($lifeTime); //Garbage cleaning function, clean up expired data
The main thing is to implement these methods. You can set different specific methods according to different storage drivers. I have implemented two drivers for saving sessions, mysql database and redis. If necessary, you can expand it yourself. The expansion is very easy. Convenience is easy.
The following is my redis implementation (db is similar to redis, redis code is less, posted):
I used the interface method, which makes it easier to expand. I wanted to use memcached that day, so just add it directly
<?<span>php </span><span>include_once</span> __DIR__."/interfaceSession.php"<span>; </span><span>/*</span><span>* * 以db的方式存储session </span><span>*/</span> <span>class</span> redisSession <span>implements</span><span> interfaceSession{ </span><span>/*</span><span>* * 保存session的数据库表的信息 </span><span>*/</span> <span>private</span> <span>$_options</span> = <span>array</span><span>( </span>'handler' => <span>null</span>, <span>//</span><span>数据库连接句柄</span> 'host' => <span>null</span>, 'port' => <span>null</span>, 'lifeTime' => <span>null</span>,<span> ); </span><span>/*</span><span>* * 构造函数 * @param $options 设置信息数组 </span><span>*/</span> <span>public</span> <span>function</span> __construct(<span>$options</span>=<span>array</span><span>()){ </span><span>if</span>(!<span>class_exists</span>("redis", <span>false</span><span>)){ </span><span>die</span>("必须安装redis扩展"<span>); } </span><span>if</span>(!<span>isset</span>(<span>$options</span>['lifeTime']) || <span>$options</span>['lifeTime'] <= 0<span>){ </span><span>$options</span>['lifeTime'] = <span>ini_get</span>('session.gc_maxlifetime'<span>); } </span><span>$this</span>->_options = <span>array_merge</span>(<span>$this</span>->_options, <span>$options</span><span>); } </span><span>/*</span><span>* * 开始使用该驱动的session </span><span>*/</span> <span>public</span> <span>function</span><span> begin(){ </span><span>if</span>(<span>$this</span>->_options['host'] === <span>null</span> || <span>$this</span>->_options['port'] === <span>null</span> || <span>$this</span>->_options['lifeTime'] === <span>null</span><span> ){ </span><span>return</span> <span>false</span><span>; } </span><span>//</span><span>设置session处理函数</span> <span>session_set_save_handler</span><span>( </span><span>array</span>(<span>$this</span>, 'open'), <span>array</span>(<span>$this</span>, 'close'), <span>array</span>(<span>$this</span>, 'read'), <span>array</span>(<span>$this</span>, 'write'), <span>array</span>(<span>$this</span>, 'destory'), <span>array</span>(<span>$this</span>, 'gc'<span>) ); } </span><span>/*</span><span>* * 自动开始回话或者session_start()开始回话后第一个调用的函数 * 类似于构造函数的作用 * @param $savePath 默认的保存路径 * @param $sessionName 默认的参数名,PHPSESSID </span><span>*/</span> <span>public</span> <span>function</span> open(<span>$savePath</span>, <span>$sessionName</span><span>){ </span><span>if</span>(<span>is_resource</span>(<span>$this</span>->_options['handler'])) <span>return</span> <span>true</span><span>; </span><span>//</span><span>连接redis</span> <span>$redisHandle</span> = <span>new</span><span> Redis(); </span><span>$redisHandle</span>->connect(<span>$this</span>->_options['host'], <span>$this</span>->_options['port'<span>]); </span><span>if</span>(!<span>$redisHandle</span><span>){ </span><span>return</span> <span>false</span><span>; } </span><span>$this</span>->_options['handler'] = <span>$redisHandle</span><span>; </span><span>$this</span>->gc(<span>null</span><span>); </span><span>return</span> <span>true</span><span>; } </span><span>/*</span><span>* * 类似于析构函数,在write之后调用或者session_write_close()函数之后调用 </span><span>*/</span> <span>public</span> <span>function</span><span> close(){ </span><span>return</span> <span>$this</span>->_options['handler']-><span>close(); } </span><span>/*</span><span>* * 读取session信息 * @param $sessionId 通过该Id唯一确定对应的session数据 * @return session信息/空串 </span><span>*/</span> <span>public</span> <span>function</span> read(<span>$sessionId</span><span>){ </span><span>return</span> <span>$this</span>->_options['handler']->get(<span>$sessionId</span><span>); } </span><span>/*</span><span>* * 写入或者修改session数据 * @param $sessionId 要写入数据的session对应的id * @param $sessionData 要写入的数据,已经序列化过了 </span><span>*/</span> <span>public</span> <span>function</span> write(<span>$sessionId</span>, <span>$sessionData</span><span>){ </span><span>return</span> <span>$this</span>->_options['handler']->setex(<span>$sessionId</span>, <span>$this</span>->_options['lifeTime'], <span>$sessionData</span><span>); } </span><span>/*</span><span>* * 主动销毁session会话 * @param $sessionId 要销毁的会话的唯一id </span><span>*/</span> <span>public</span> <span>function</span> destory(<span>$sessionId</span><span>){ </span><span>return</span> <span>$this</span>->_options['handler']->delete(<span>$sessionId</span>) >= 1 ? <span>true</span> : <span>false</span><span>; } </span><span>/*</span><span>* * 清理绘画中的过期数据 * @param 有效期 </span><span>*/</span> <span>public</span> <span>function</span> gc(<span>$lifeTime</span><span>){ </span><span>//</span><span>获取所有sessionid,让过期的释放掉</span> <span>$this</span>->_options['handler']->keys("*"<span>); </span><span>return</span> <span>true</span><span>; } }</span>
Take a look at the simple factory pattern
<span>class</span><span> session { </span><span>/*</span><span>* * 驱动程序句柄保存 </span><span>*/</span> <span>private</span> <span>static</span> <span>$_handler</span> = <span>null</span><span>; </span><span>/*</span><span>* * 创建session驱动程序 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> getSession(<span>$type</span>, <span>$options</span><span>){ </span><span>//</span><span>单例</span> <span>if</span>(<span>isset</span>(<span>$handler</span><span>)){ </span><span>return</span> self::<span>$_handler</span><span>; } </span><span>switch</span> (<span>$type</span><span>) { </span><span>case</span> 'db': <span>//</span><span>数据库驱动session类型</span> <span>include_once</span> __DIR__."/driver/dbSession.php"<span>; </span><span>$handler</span> = <span>new</span> dbSession(<span>$options</span><span>); </span><span>break</span><span>; </span><span>case</span> 'redis': <span>//</span><span>redis驱动session类型</span> <span>include_once</span> __DIR__."/driver/redisSession.php"<span>; </span><span>$handler</span> = <span>new</span> redisSession(<span>$options</span><span>); </span><span>break</span><span>; </span><span>default</span>: <span>return</span> <span>false</span><span>; </span><span>break</span><span>; } </span><span>return</span> self::<span>$_handler</span> = <span>$handler</span><span>; } }</span>
The call is also very simple,
session::getSession('redis',<span>array</span><span>( </span>'host' => "localhost", 'port' => "6379",<span> ))</span>-><span>begin(); </span><span>session_start</span>();
The database version is also very simple to configure. If necessary, you can download the full version and demo
here
The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise we will reserve the right to pursue it. Legal liability rights.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use