Home > Backend Development > PHP Tutorial > Discussion on the method of writing session to database in php

Discussion on the method of writing session to database in php

WBOY
Release: 2016-07-25 08:59:08
Original
1050 people have browsed it
  1. CREATE DATABASE 'session';
复制代码

创建表结构:

  1. CREATE TABLE 'session'( id CHAR(30) NOT NULL , 'user 'CHAR(30), data CHAR(3000) ,PARMIRY BY ('id') );
复制代码

保存session的文件session_start.php:

  1. $con =mysql_connection("127.0.0.1","user" , "pass");
  2. mysql_select_db("session");
  3. function open($save_path, $session_name)
  4. {
  5. return(true);
  6. }
  7. function close()
  8. {
  9. return(true);
  10. }
  11. function read($id)
  12. {
  13. if($result = mysql_query("SELECT * FROM session WHERE id='$id'"))
  14. {
  15. if($row = mysql_felth_row($result ))
  16. { return $row["data"]; }
  17. }
  18. else
  19. {
  20. return "";
  21. }
  22. }
  23. function write($id, $sess_data)
  24. {
  25. if($result = mysql_query("UPDATE session SET data='$sess_data' WHERE id='$id'"))
  26. {
  27. return true;
  28. }
  29. else
  30. {
  31. return false;
  32. }
  33. }
  34. function destroy($id)
  35. {
  36. if($result = mysql_query("DELETE * FROM session WHERE id='$id'"))
  37. {
  38. return true;
  39. }
  40. else
  41. {
  42. return false;
  43. }
  44. }
  45. /*********************************************
  46. * WARNING - You will need to implement some *
  47. * sort of garbage collection routine here. *
  48. *********************************************/
  49. function gc($maxlifetime)
  50. {
  51. return true;
  52. }
  53. session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
  54. session_start();
  55. // proceed to use sessions normally
  56. ?>
复制代码

使用方法,在需要将session保存进数据库的页面中,引入文件:session_user_start.php 即可。 注意,此文件一定要在文件的第一行。



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template