Translated using Weblate (Norwegian Bokmål)
[phpmyadmin.git] / libraries / session.inc.php
blobb39aaeb90114b3f32b99c3724f416fab7eff826e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * session handling
6 * @todo add an option to use mm-module for session handler
8 * @package PhpMyAdmin
9 * @see https://secure.php.net/session
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 require_once 'libraries/session.lib.php';
17 // verify if PHP supports session, die if it does not
19 if (!@function_exists('session_name')) {
20 PMA_warnMissingExtension('session', true);
21 } elseif (! empty(ini_get('session.auto_start')) && session_name() != 'phpMyAdmin' && !empty(session_id())) {
22 // Do not delete the existing non empty session, it might be used by other
23 // applications; instead just close it.
24 if (empty($_SESSION)) {
25 /* Ignore errors as this might have been destroyed in other request meanwhile */
26 @session_destroy();
27 } elseif (function_exists('session_abort')) {
28 /* PHP 5.6 and newer */
29 session_abort();
30 } else {
31 session_write_close();
35 // disable starting of sessions before all settings are done
36 // does not work, besides how it is written in php manual
37 //ini_set('session.auto_start', '0');
39 // session cookie settings
40 session_set_cookie_params(
41 0, $GLOBALS['PMA_Config']->getRootPath(),
42 '', $GLOBALS['PMA_Config']->isHttps(), true
45 // cookies are safer (use @ini_set() in case this function is disabled)
46 @ini_set('session.use_cookies', 'true');
48 // optionally set session_save_path
49 $path = $GLOBALS['PMA_Config']->get('SessionSavePath');
50 if (!empty($path)) {
51 session_save_path($path);
54 // use cookies only
55 @ini_set('session.use_only_cookies', '1');
56 // strict session mode (do not accept random string as session ID)
57 @ini_set('session.use_strict_mode', '1');
58 // make the session cookie HttpOnly
59 @ini_set('session.cookie_httponly', '1');
60 // do not force transparent session ids
61 @ini_set('session.use_trans_sid', '0');
63 // delete session/cookies when browser is closed
64 @ini_set('session.cookie_lifetime', '0');
66 // warn but don't work with bug
67 @ini_set('session.bug_compat_42', 'false');
68 @ini_set('session.bug_compat_warn', 'true');
70 // use more secure session ids
71 @ini_set('session.hash_function', '1');
73 // some pages (e.g. stylesheet) may be cached on clients, but not in shared
74 // proxy servers
75 session_cache_limiter('private');
77 // start the session
78 // on some servers (for example, sourceforge.net), we get a permission error
79 // on the session data directory, so I add some "@"
82 function PMA_sessionFailed($errors)
84 $messages = array();
85 foreach ($errors as $error) {
87 * Remove path from open() in error message to avoid path disclossure
89 * This can happen with PHP 5 when nonexisting session ID is provided,
90 * since PHP 7, session existence is checked first.
92 * This error can also happen in case of session backed error (eg.
93 * read only filesystem) on any PHP version.
95 * The message string is currently hardcoded in PHP, so hopefully it
96 * will not change in future.
98 $messages[] = preg_replace(
99 '/open\(.*, O_RDWR\)/',
100 'open(SESSION_FILE, O_RDWR)',
101 htmlspecialchars($error->getMessage())
106 * Session initialization is done before selecting language, so we
107 * can not use translations here.
109 PMA_fatalError(
110 'Error during session start; please check your PHP and/or '
111 . 'webserver log file and configure your PHP '
112 . 'installation properly. Also ensure that cookies are enabled '
113 . 'in your browser.'
114 . '<br /><br />'
115 . implode('<br /><br />', $messages)
119 // See bug #1538132. This would block normal behavior on a cluster
120 //ini_set('session.save_handler', 'files');
122 $session_name = 'phpMyAdmin';
123 @session_name($session_name);
125 // Restore correct sesion ID (it might have been reset by auto started session
126 if (isset($_COOKIE['phpMyAdmin'])) {
127 session_id($_COOKIE['phpMyAdmin']);
130 // on first start of session we check for errors
131 // f.e. session dir cannot be accessed - session file not created
132 $orig_error_count = $GLOBALS['error_handler']->countErrors(false);
134 $session_result = session_start();
136 if ($session_result !== true
137 || $orig_error_count != $GLOBALS['error_handler']->countErrors(false)
139 setcookie($session_name, '', 1);
140 $errors = $GLOBALS['error_handler']->sliceErrors($orig_error_count);
141 PMA_sessionFailed($errors);
143 unset($orig_error_count, $session_result);
146 * Disable setting of session cookies for further session_start() calls.
148 @ini_set('session.use_cookies', 'true');
151 * Token which is used for authenticating access queries.
152 * (we use "space PMA_token space" to prevent overwriting)
154 if (empty($_SESSION[' PMA_token '])) {
155 PMA_generateToken();
158 * Check for disk space on session storage by trying to write it.
160 * This seems to be most reliable approach to test if sessions are working,
161 * otherwise the check would fail with custom session backends.
163 $orig_error_count = $GLOBALS['error_handler']->countErrors();
164 session_write_close();
165 if ($GLOBALS['error_handler']->countErrors() > $orig_error_count) {
166 $errors = $GLOBALS['error_handler']->sliceErrors($orig_error_count);
167 PMA_sessionFailed($errors);
169 session_start();
170 if (empty($_SESSION[' PMA_token '])) {
171 PMA_fatalError(
172 'Failed to store CSRF token in session! ' .
173 'Probably sessions are not working properly.'