| Conditions | 19 |
| Paths | 294 |
| Total Lines | 65 |
| Code Lines | 34 |
| Lines | 5 |
| Ratio | 7.69 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 26 | function loadSession() |
||
| 27 | { |
||
| 28 | global $modSettings, $boardurl, $sc; |
||
|
|
|||
| 29 | |||
| 30 | // Attempt to change a few PHP settings. |
||
| 31 | @ini_set('session.use_cookies', true); |
||
| 32 | @ini_set('session.use_only_cookies', false); |
||
| 33 | @ini_set('url_rewriter.tags', ''); |
||
| 34 | @ini_set('session.use_trans_sid', false); |
||
| 35 | @ini_set('arg_separator.output', '&'); |
||
| 36 | |||
| 37 | if (!empty($modSettings['globalCookies'])) |
||
| 38 | { |
||
| 39 | $parsed_url = parse_url($boardurl); |
||
| 40 | |||
| 41 | if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1) |
||
| 42 | @ini_set('session.cookie_domain', '.' . $parts[1]); |
||
| 43 | } |
||
| 44 | // @todo Set the session cookie path? |
||
| 45 | |||
| 46 | // If it's already been started... probably best to skip this. |
||
| 47 | if ((ini_get('session.auto_start') == 1 && !empty($modSettings['databaseSession_enable'])) || session_id() == '') |
||
| 48 | { |
||
| 49 | // Attempt to end the already-started session. |
||
| 50 | if (ini_get('session.auto_start') == 1) |
||
| 51 | session_write_close(); |
||
| 52 | |||
| 53 | // This is here to stop people from using bad junky PHPSESSIDs. |
||
| 54 | if (isset($_REQUEST[session_name()]) && preg_match('~^[A-Za-z0-9,-]{16,64}$~', $_REQUEST[session_name()]) == 0 && !isset($_COOKIE[session_name()])) |
||
| 55 | { |
||
| 56 | $session_id = md5(md5('smf_sess_' . time()) . mt_rand()); |
||
| 57 | $_REQUEST[session_name()] = $session_id; |
||
| 58 | $_GET[session_name()] = $session_id; |
||
| 59 | $_POST[session_name()] = $session_id; |
||
| 60 | } |
||
| 61 | |||
| 62 | // Use database sessions? (they don't work in 4.1.x!) |
||
| 63 | if (!empty($modSettings['databaseSession_enable'])) |
||
| 64 | { |
||
| 65 | @ini_set('session.serialize_handler', 'php'); |
||
| 66 | session_set_save_handler('sessionOpen', 'sessionClose', 'sessionRead', 'sessionWrite', 'sessionDestroy', 'sessionGC'); |
||
| 67 | @ini_set('session.gc_probability', '1'); |
||
| 68 | } |
||
| 69 | elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime'])) |
||
| 70 | @ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60)); |
||
| 71 | |||
| 72 | // Use cache setting sessions? |
||
| 73 | if (empty($modSettings['databaseSession_enable']) && !empty($modSettings['cache_enable']) && php_sapi_name() != 'cli') |
||
| 74 | call_integration_hook('integrate_session_handlers'); |
||
| 75 | |||
| 76 | session_start(); |
||
| 77 | |||
| 78 | // Change it so the cache settings are a little looser than default. |
||
| 79 | if (!empty($modSettings['databaseSession_loose'])) |
||
| 80 | header('Cache-Control: private'); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Set the randomly generated code. |
||
| 84 | View Code Duplication | if (!isset($_SESSION['session_var'])) |
|
| 85 | { |
||
| 86 | $_SESSION['session_value'] = md5(session_id() . mt_rand()); |
||
| 87 | $_SESSION['session_var'] = substr(preg_replace('~^\d+~', '', sha1(mt_rand() . session_id() . mt_rand())), 0, mt_rand(7, 12)); |
||
| 88 | } |
||
| 89 | $sc = $_SESSION['session_value']; |
||
| 90 | } |
||
| 91 | |||
| 235 | ?> |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.