home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / session.inc.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  4.1 KB  |  128 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * session handling
  5.  *
  6.  * @version $Id: session.inc.php 10942 2007-11-25 13:33:54Z lem9 $
  7.  * @todo    add failover or warn if sessions are not configured properly
  8.  * @todo    add an option to use mm-module for session handler
  9.  * @see     http://www.php.net/session
  10.  * @uses    session_name()
  11.  * @uses    session_start()
  12.  * @uses    ini_set()
  13.  * @uses    version_compare()
  14.  * @uses    PHP_VERSION
  15.  */
  16.  
  17. // verify if PHP supports session, die if it does not
  18.  
  19. if (!@function_exists('session_name')) {
  20.     PMA_fatalError('strCantLoad', 'session');
  21. } elseif (ini_get('session.auto_start') == true && session_name() != 'phpMyAdmin') {
  22.     // Do not delete the existing session, it might be used by other 
  23.     // applications; instead just close it.
  24.     session_write_close();
  25. }
  26.  
  27. // disable starting of sessions before all settings are done
  28. // does not work, besides how it is written in php manual
  29. //ini_set('session.auto_start', 0);
  30.  
  31. // session cookie settings
  32. session_set_cookie_params(0, PMA_Config::getCookiePath() . '; HttpOnly',
  33.     '', PMA_Config::isHttps());
  34.  
  35. // cookies are safer
  36. ini_set('session.use_cookies', true);
  37.  
  38. // but not all user allow cookies
  39. ini_set('session.use_only_cookies', false);
  40. ini_set('session.use_trans_sid', true);
  41. ini_set('url_rewriter.tags',
  42.     'a=href,frame=src,input=src,form=fakeentry,fieldset=');
  43. //ini_set('arg_separator.output', '&');
  44.  
  45. // delete session/cookies when browser is closed
  46. ini_set('session.cookie_lifetime', 0);
  47.  
  48. // warn but dont work with bug
  49. ini_set('session.bug_compat_42', false);
  50. ini_set('session.bug_compat_warn', true);
  51.  
  52. // use more secure session ids (with PHP 5)
  53. if (version_compare(PHP_VERSION, '5.0.0', 'ge')
  54.   && substr(PHP_OS, 0, 3) != 'WIN') {
  55.     ini_set('session.hash_function', 1);
  56.     ini_set('session.hash_bits_per_character', 6);
  57. }
  58.  
  59. // some pages (e.g. stylesheet) may be cached on clients, but not in shared
  60. // proxy servers
  61. session_cache_limiter('private');
  62.  
  63. // start the session
  64. // on some servers (for example, sourceforge.net), we get a permission error
  65. // on the session data directory, so I add some "@"
  66.  
  67. // See bug #1538132. This would block normal behavior on a cluster
  68. //ini_set('session.save_handler', 'files');
  69.  
  70. $session_name = 'phpMyAdmin';
  71. @session_name($session_name);
  72. // strictly, PHP 4 since 4.4.2 would not need a verification
  73. if (version_compare(PHP_VERSION, '5.1.2', 'lt')
  74.  && isset($_COOKIE[$session_name])
  75.  && eregi("\r|\n", $_COOKIE[$session_name])) {
  76.     die('attacked');
  77. }
  78.  
  79. if (! isset($_COOKIE[$session_name])) {
  80.     // on first start of session we will check for errors
  81.     // f.e. session dir cannot be accessed - session file not created
  82.     ob_start();
  83.     $old_display_errors = ini_get('display_errors');
  84.     $old_error_reporting = error_reporting(E_ALL);
  85.     ini_set('display_errors', 1);
  86.     $r = session_start();
  87.     ini_set('display_errors', $old_display_errors);
  88.     error_reporting($old_error_reporting);
  89.     unset($old_display_errors, $old_error_reporting);
  90.     $session_error = ob_get_contents();
  91.     ob_end_clean();
  92.     if ($r !== true || ! empty($session_error)) {
  93.         setcookie($session_name, '', 1);
  94.         PMA_fatalError('strSessionStartupErrorGeneral');
  95.     }
  96. } else {
  97.     @session_start();
  98. }
  99.  
  100. /**
  101.  * Token which is used for authenticating access queries.
  102.  * (we use "space PMA_token space" to prevent overwriting)
  103.  */
  104. if (!isset($_SESSION[' PMA_token '])) {
  105.     $_SESSION[' PMA_token '] = md5(uniqid(rand(), true));
  106. }
  107.  
  108. /**
  109.  * tries to secure session from hijacking and fixation
  110.  * should be called before login and after successfull login
  111.  * (only required if sensitive information stored in session)
  112.  *
  113.  * @uses    session_regenerate_id() to secure session from fixation
  114.  * @uses    session_id()            to set new session id
  115.  * @uses    strip_tags()            to prevent XSS attacks in SID
  116.  * @uses    function_exists()       for session_regenerate_id()
  117.  */
  118. function PMA_secureSession()
  119. {
  120.     // prevent session fixation and XSS
  121.     if (function_exists('session_regenerate_id')) {
  122.         session_regenerate_id(true);
  123.     } else {
  124.         session_id(strip_tags(session_id()));
  125.     }
  126. }
  127. ?>
  128.