1313 * @copyright 2017 Liam Kelly
1414 * @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1515 * @link https://github.com/likel/php-simple-sessions
16- * @version 1.0.0
16+ * @version 1.0.1
1717 */
1818namespace Likel \Session ;
1919
@@ -32,24 +32,56 @@ class Handler implements \ArrayAccess
3232 */
3333 function __construct ($ parameters = array ())
3434 {
35+ if (!is_array ($ parameters )) {
36+ $ parameters = array ();
37+ }
38+
3539 // Defaults
3640 $ parameters ["session_name " ] = empty ($ parameters ["session_name " ]) ? "likel_session " : $ parameters ["session_name " ];
37- $ parameters ["secure " ] = empty ($ parameters ["secure " ]) ? false : $ parameters ["secure " ];
41+ $ parameters ["secure " ] = empty ($ parameters ["secure " ]) ? false : is_bool ( $ parameters ["secure " ] === true ) ? true : false ;
3842 $ parameters ["credentials_location " ] = empty ($ parameters ["credentials_location " ]) ? __DIR__ . '/../../ini/credentials.ini ' : $ parameters ["credentials_location " ];
3943
4044 // Setup the database class variable
4145 $ this ->db = new \Likel \DB ($ parameters ["credentials_location " ]);
4246
43- // Attempt to get the secret_hash from the credentials file
44- try {
45- $ session_credentials = parse_ini_file ($ parameters ["credentials_location " ], true );
46- $ this ->secret_hash = $ this ->loadSecretHash ($ session_credentials ["likel_session " ]);
47- } catch (\Exception $ ex ) {
48- throw $ ex ;
47+ if ($ this ->db ->databaseInitialised ()) {
48+ // Attempt to get the secret_hash from the credentials file
49+ try {
50+ $ this ->secret_hash = $ this ->loadSecretHash ($ parameters ["credentials_location " ]);
51+
52+ // Start session
53+ $ this ->start_session ($ parameters ["session_name " ], $ parameters ["secure " ]);
54+ } catch (\Exception $ ex ) {
55+ echo $ ex ->getMessage ();
56+ }
4957 }
58+ }
5059
51- // Start session
52- $ this ->start_session ($ parameters ["session_name " ], $ parameters ["secure " ]);
60+ /**
61+ * Attempt to retrieve the secret_hash from the credentials file
62+ *
63+ * @param array $credentials likel_session from the credentials.ini file
64+ * @return string
65+ * @throws \Exception If credentials empty or not found
66+ */
67+ private function loadSecretHash ($ credentials_location )
68+ {
69+ if (file_exists ($ credentials_location )) {
70+ $ session_credentials = parse_ini_file ($ credentials_location , true );
71+ $ credentials = $ session_credentials ["likel_session " ];
72+
73+ if (!empty ($ credentials )){
74+ if (!empty ($ credentials ["secret_hash " ])) {
75+ return $ credentials ["secret_hash " ];
76+ } else {
77+ throw new \Exception ('The session_hash variable is empty. ' );
78+ }
79+ } else {
80+ throw new \Exception ('The likel_session parameter in the credentials file cannot be found. ' );
81+ }
82+ } else {
83+ throw new \Exception ('The credential file could not be located. ' );
84+ }
5385 }
5486
5587 /**
@@ -241,27 +273,6 @@ private function getKeyAndIv($id)
241273 }
242274 }
243275
244- /**
245- * Attempt to retrieve the secret_hash from the credentials file
246- *
247- * @param array $credentials likel_session from the credentials.ini file
248- * @return string
249- * @throws \Exception If credentials empty or not found
250- */
251- private function loadSecretHash ($ credentials )
252- {
253- if (!empty ($ credentials )){
254- if (!empty ($ credentials ["secret_hash " ])) {
255- return $ credentials ["secret_hash " ];
256- } else {
257- throw new \Exception ('The session_hash variable is empty. ' );
258- }
259-
260- } else {
261- throw new \Exception ('The credential file could not be located or is empty. ' );
262- }
263- }
264-
265276 /**
266277 * Setup and start the session
267278 *
@@ -294,6 +305,9 @@ private function start_session($session_name, $secure)
294305 session_name ($ session_name );
295306 session_start ();
296307
308+ // Put it into the DB so we don't delay
309+ $ this ->_write (session_id (), '' );
310+
297311 // Regenerate ID is recommended to reset the session every reload
298312 // Bug occurs if set to true that causes the current session to
299313 // be removed if loading pages too quickly
0 commit comments