How to use database to implement PHP save session_php Tutorial

Source: Internet
Author: User
Tags php and mysql

PHP saves the session by default is the way to save the file, which is only available on Windows with a small amount of space on the file, but if we adopt Uinx or Liux file system, the file space overhead of such file system is very large, However, the session is to be used all the times, a large number of users will create a lot of session files, so that the entire server brings performance problems.

On the other hand, if the server is clustered, it cannot maintain session consistency, so we are ready to use the database to save the session, so that no matter how many servers are used at the same time, Just save their session on a database server to save the session complete, how to implement please continue to see.

PHP Save session By default is the use of the file method to save, we in the PHP configuration file php.ini can see such a line, session.save_handler= "files", this means to use the file to save the session , to use the database to save, we need to modify the user mode, renamed session.save_handler= "use" on it, but this is just to show that we do not file the way to store the session, we also have to select the database and build the database table.

To build the database and database table structure, we can use any PHP can be used by the database, because the combination of PHP and MySQL is best, I use MySQL to do the case, of course, according to your needs can be renamed to other databases, and because MySQL has no function of things, It's faster than other databases, but saving the session doesn't require anything, and I think it's better here.

Create database ' session '; Create a table structure of ' session ' (ID char () NOT NULL, ' user ' char (+), data char (+), parmiry by (' id '));

PHP Save session Write PHP file

 
 
  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 the work of our PHP save session has been completed, as long as you need to use the session, the Session_user_start. Php. Included, note that this file must be included in the first line of the file, and then used as a method of using the file's session.


http://www.bkjia.com/PHPjc/446131.html www.bkjia.com true http://www.bkjia.com/PHPjc/446131.html techarticle PHP Save session By default is the way to save the file, which is only available on Windows with a small space overhead, but if we use Uinx or Liu ...

  • 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.