How to use the database to save the session in PHP

Source: Internet
Author: User

By default, the PHP session is saved as a file, which can be used only on windows with a low file space overhead, however, if we use a file system on the uinx or liux, the file space overhead of such a file system is very large, but the session must be used all the time, A large number of users need to create a lot of session files, which brings performance problems to the entire server.

On the other hand, if the server starts to adopt the cluster mode, the session consistency cannot be maintained. Therefore, we are ready to use the database mode to save the session. In this way, no matter how many servers are used at the same time, as long as their sessions are saved on a database server, they can save the complete session. Continue with the implementation of the session.

By default, PHP stores session files. We use PHP to prepare the PHP file. you can see this row in ini, session. save_handler = "files", which means to use files to save sessions. To use databases to store sessions, we need to change them to the user mode and rename them as sessions. save_handler = "use". However, this only means that we have not stored the session in the file mode. We also need to select a database and create a database table.

To create a table structure for databases and databases, we can use any database that PHP can use. Because PHP and mysql have the best combination, I will use mysql as an example, of course, you can change the name of another database based on your needs. At the same time, because mysql has no transaction function, it is faster than other databases. However, it does not need to be processed to store sessions. I think it is better here.

Create database 'session '; create table 'session' (id char (30) not null, 'user' char (30), data char (3000 ), parmiry by ('id '));

PHP saves session and writes PHP files

 
  1. < ?PHP  
  2. $con =mysql_connection("127.0.0.1"
    ,"user" , "pass");  
  3. mysql_select_db("session");  
  4. function open($save_path, $session_name)   
  5. {  
  6. return(true);  
  7. }  
  8. function close()   
  9. {  
  10. return(true);  
  11. }  
  12. function read($id)   
  13. {  
  14. if($result = mysql_query("select 
    * from session where id='$id'"))  
  15. {  
  16. if($row = mysql_felth_row($result ))  
  17. { return $row["data"]; }  
  18. }  
  19. else  
  20. {  
  21. return "";  
  22. }  
  23. }  
  24. function write($id, $sess_data)   
  25. {  
  26. if($result = mysql_query("update session 
    set data='$sess_data' where id='$id'"))  
  27. {  
  28. return true;  
  29. }  
  30. else  
  31. {  
  32. return false;  
  33. }  
  34. }  
  35. function destroy($id)   
  36. {  
  37. if($result = mysql_query("delete * 
    from session where id='$id'"))  
  38. {  
  39. return true;  
  40. }  
  41. else  
  42. {  
  43. return false;  
  44. }  
  45. }  
  46. function gc($maxlifetime)   
  47. {  
  48. return true;  
  49. }  
  50. session_set_save_handler("open", 
    "close", "read", "write", "destroy", "gc");  
  51. session_start();  
  52. // proceed to use sessions normally  
  53. ?> 

Save as session_user_start.PHP.

Now, we have completed saving the session in PHP, as long as you need to use the session, set session_user_start.PHP. This file must be included in the first line of the file, and can be used in the same way as the session of the file.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.