session.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * session handling
  5. *
  6. * @todo add failover or warn if sessions are not configured properly
  7. * @todo add an option to use mm-module for session handler
  8. *
  9. * @package PhpMyAdmin
  10. * @see http://www.php.net/session
  11. */
  12. if (! defined('PHPMYADMIN')) {
  13. exit;
  14. }
  15. require './libraries/phpseclib/Crypt/Random.php';
  16. // verify if PHP supports session, die if it does not
  17. if (!@function_exists('session_name')) {
  18. PMA_warnMissingExtension('session', true);
  19. } elseif (ini_get('session.auto_start') == true && session_name() != 'phpMyAdmin') {
  20. // Do not delete the existing session, it might be used by other
  21. // applications; instead just close it.
  22. session_write_close();
  23. }
  24. // disable starting of sessions before all settings are done
  25. // does not work, besides how it is written in php manual
  26. //ini_set('session.auto_start', 0);
  27. // session cookie settings
  28. session_set_cookie_params(
  29. 0, $GLOBALS['PMA_Config']->getCookiePath(),
  30. '', $GLOBALS['PMA_Config']->isHttps(), true
  31. );
  32. // cookies are safer (use @ini_set() in case this function is disabled)
  33. @ini_set('session.use_cookies', true);
  34. // optionally set session_save_path
  35. $path = $GLOBALS['PMA_Config']->get('SessionSavePath');
  36. if (!empty($path)) {
  37. session_save_path($path);
  38. }
  39. // but not all user allow cookies
  40. @ini_set('session.use_only_cookies', false);
  41. // do not force transparent session ids, see bug #3398788
  42. //@ini_set('session.use_trans_sid', true);
  43. @ini_set(
  44. 'url_rewriter.tags',
  45. 'a=href,frame=src,input=src,form=fakeentry,fieldset='
  46. );
  47. //ini_set('arg_separator.output', '&amp;');
  48. // delete session/cookies when browser is closed
  49. @ini_set('session.cookie_lifetime', 0);
  50. // warn but dont work with bug
  51. @ini_set('session.bug_compat_42', false);
  52. @ini_set('session.bug_compat_warn', true);
  53. // use more secure session ids
  54. @ini_set('session.hash_function', 1);
  55. // some pages (e.g. stylesheet) may be cached on clients, but not in shared
  56. // proxy servers
  57. session_cache_limiter('private');
  58. // start the session
  59. // on some servers (for example, sourceforge.net), we get a permission error
  60. // on the session data directory, so I add some "@"
  61. // See bug #1538132. This would block normal behavior on a cluster
  62. //ini_set('session.save_handler', 'files');
  63. $session_name = 'phpMyAdmin';
  64. @session_name($session_name);
  65. if (! isset($_COOKIE[$session_name])) {
  66. // on first start of session we check for errors
  67. // f.e. session dir cannot be accessed - session file not created
  68. $orig_error_count = $GLOBALS['error_handler']->countErrors();
  69. $r = session_start();
  70. if ($r !== true
  71. || $orig_error_count != $GLOBALS['error_handler']->countErrors()
  72. ) {
  73. setcookie($session_name, '', 1);
  74. /*
  75. * Session initialization is done before selecting language, so we
  76. * can not use translations here.
  77. */
  78. PMA_fatalError('Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.');
  79. }
  80. unset($orig_error_count);
  81. } else {
  82. session_start();
  83. }
  84. /**
  85. * Token which is used for authenticating access queries.
  86. * (we use "space PMA_token space" to prevent overwriting)
  87. */
  88. if (! isset($_SESSION[' PMA_token '])) {
  89. $_SESSION[' PMA_token '] = bin2hex(crypt_random_string(16));
  90. }
  91. /**
  92. * tries to secure session from hijacking and fixation
  93. * should be called before login and after successfull login
  94. * (only required if sensitive information stored in session)
  95. *
  96. * @return void
  97. */
  98. function PMA_secureSession()
  99. {
  100. // prevent session fixation and XSS
  101. session_regenerate_id(true);
  102. $_SESSION[' PMA_token '] = bin2hex(crypt_random_string(16));
  103. }
  104. ?>