session.inc.php 3.7 KB

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