[php] session avec une bdd

session avec une bdd [php] - Programmation

Marsh Posté le 11-08-2001 à 21:46:07    

Salut les gens.
 
Voilà, je dois être neuneu :na: , mais j'arrive pas à adapter ca pour une bdd (c'est fait pour gerer les session avec des fichiers texte apparemment).
 
Je sais pas pourquoi mais j'ai comme un bloquage.  
 
Si quelqu'un(e) pouvait m'aider... :jap:  
 
 

Code :
  1. <?
  2. function open ($save_path, $session_name)
  3. {
  4. global $sess_save_path, $sess_session_name;
  5. $sess_save_path = $save_path;
  6. $sess_session_name = $session_name;
  7. return(TRUE);
  8. }
  9. function close()
  10. {
  11.   return(TRUE);
  12. }
  13. function read ($id)
  14. {
  15. global $sess_save_path, $sess_session_name;
  16. $sess_file = "$sess_save_path/sess_$id";
  17. if ($fp = @fopen($sess_file, "r" ))
  18. {
  19.  $sess_data = fread($fp, filesize($sess_file));
  20.  return($sess_data);
  21. }
  22. else
  23. {
  24.  return("" );
  25. }
  26. }
  27. function write ($id, $sess_data)
  28. {
  29. global $sess_save_path, $sess_session_name;
  30. $sess_file = "$sess_save_path/sess_$id";
  31. if ($fp = @fopen($sess_file, "w" ))
  32. {
  33.  return(fwrite($fp, $sess_data));
  34. }
  35. else
  36. {
  37.  return(FALSE);
  38. }
  39. }
  40. function destroy ($id)
  41. {
  42. global $sess_save_path, $sess_session_name;
  43. $sess_file = "$sess_save_path/sess_$id";
  44. return(@unlink($sess_file));
  45. }
  46. /***************************************************
  47. * ATTENTION - Vous devez implémenter une routine  *
  48. * d'entretien des sessions ici.                   *
  49. ***************************************************/
  50. function gc ($maxlifetime)
  51. {
  52. return TRUE;
  53. }
  54. session_set_save_handler ("open", "close", "read", "write", "destroy", "gc" );
  55. session_start();
  56. // utilisez vos sessions normalement
  57. ?>


 
 
je suis neuneu ! n'est-ce pas ?


---------------
oui oui
Reply

Marsh Posté le 11-08-2001 à 21:46:07   

Reply

Marsh Posté le 11-08-2001 à 22:14:58    

yo j'ai fini par trouver un truc.
 
Si ca interesse quelqu'un(e) :
 
 
http://phpbuilder.com/columns/ying20000602.php3
 

Code :
  1. <?
  2. /* ------------------------------------------------------------------------
  3. * session_mysql.php
  4. * ------------------------------------------------------------------------
  5. * PHP4 MySQL Session Handler
  6. * Version 1.00
  7. * by Ying Zhang (ying@zippydesign.com)
  8. * Last Modified: May 21 2000
  9. *
  10. * ------------------------------------------------------------------------
  11. * TERMS OF USAGE:
  12. * ------------------------------------------------------------------------
  13. * You are free to use this library in any way you want, no warranties are
  14. * expressed or implied.  This works for me, but I don't guarantee that it
  15. * works for you, USE AT YOUR OWN RISK.
  16. *
  17. * While not required to do so, I would appreciate it if you would retain
  18. * this header information.  If you make any modifications or improvements,
  19. * please send them via email to Ying Zhang <ying@zippydesign.com>.
  20. *
  21. * ------------------------------------------------------------------------
  22. * DESCRIPTION:
  23. * ------------------------------------------------------------------------
  24. * This library tells the PHP4 session handler to write to a MySQL database
  25. * instead of creating individual files for each session.
  26. *
  27. * Create a new database in MySQL called "sessions" like so:
  28. *
  29. * CREATE TABLE sessions (
  30. *      sesskey char(32) not null,
  31. *      expiry int(11) unsigned not null,
  32. *      value text not null,
  33. *      PRIMARY KEY (sesskey)
  34. * );
  35. *
  36. * ------------------------------------------------------------------------
  37. * INSTALLATION:
  38. * ------------------------------------------------------------------------
  39. * Make sure you have MySQL support compiled into PHP4.  Then copy this
  40. * script to a directory that is accessible by the rest of your PHP
  41. * scripts.
  42. *
  43. * ------------------------------------------------------------------------
  44. * USAGE:
  45. * ------------------------------------------------------------------------
  46. * Include this file in your scripts before you call session_start(), you
  47. * don't have to do anything special after that.
  48. */
  49. $SESS_DBHOST = "localhost";   /* database server hostname */
  50. $SESS_DBNAME = "sessions";    /* database name */
  51. $SESS_DBUSER = "phpsession";   /* database user */
  52. $SESS_DBPASS = "phpsession";   /* database password */
  53. $SESS_DBH = "";
  54. $SESS_LIFE = get_cfg_var("session.gc_maxlifetime" );
  55. function sess_open($save_path, $session_name) {
  56. global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH;
  57. if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) {
  58.  echo "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER";
  59.  echo "<li>MySQL Error: ", mysql_error();
  60.  die;
  61. }
  62. if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) {
  63.  echo "<li>Unable to select database $SESS_DBNAME";
  64.  die;
  65. }
  66. return true;
  67. }
  68. function sess_close() {
  69. return true;
  70. }
  71. function sess_read($key) {
  72. global $SESS_DBH, $SESS_LIFE;
  73. $qry = "SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time();
  74. $qid = mysql_query($qry, $SESS_DBH);
  75. if (list($value) = mysql_fetch_row($qid)) {
  76.  return $value;
  77. }
  78. return false;
  79. }
  80. function sess_write($key, $val) {
  81. global $SESS_DBH, $SESS_LIFE;
  82. $expiry = time() + $SESS_LIFE;
  83. $value = addslashes($val);
  84. $qry = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
  85. $qid = mysql_query($qry, $SESS_DBH);
  86. if (! $qid) {
  87.  $qry = "UPDATE sessions SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time();
  88.  $qid = mysql_query($qry, $SESS_DBH);
  89. }
  90. return $qid;
  91. }
  92. function sess_destroy($key) {
  93. global $SESS_DBH;
  94. $qry = "DELETE FROM sessions WHERE sesskey = '$key'";
  95. $qid = mysql_query($qry, $SESS_DBH);
  96. return $qid;
  97. }
  98. function sess_gc($maxlifetime) {
  99. global $SESS_DBH;
  100. $qry = "DELETE FROM sessions WHERE expiry < " . time();
  101. $qid = mysql_query($qry, $SESS_DBH);
  102. return mysql_affected_rows($SESS_DBH);
  103. }
  104. session_set_save_handler(
  105. "sess_open",
  106. "sess_close",
  107. "sess_read",
  108. "sess_write",
  109. "sess_destroy",
  110. "sess_gc" );
  111. ?>

 

[edtdd]--Message édité par art_dupond--[/edtdd]


---------------
oui oui
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed