session_set_save_handler causing login loop
我正在尝试使用
无论如何,这是我的会话函数代码 - 我不认为这里有什么问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php // session.functions.php $db = new mysqli("127.0.0.1","user","pass","db"); // define 'openSession()' function function openSession($sessionPath, $sessionName) { return true; } // define 'closeSession()' function function closeSession() { return true; } // define 'readSession()' method function readSession($sessionId) { global $db; // escape session ID $sessionId = $db->real_escape_string($sessionId); $result = $db->query("SELECT sessiondata FROM sessions WHERE sessionid='$sessionId' AND expiry > NOW()"); if ($result->num_rows > 0) { $row = $result->fetchRow(); return $row['sessiondata']; } // return empty string return""; } // define 'writeSession()' function function writeSession($sessionId, $sessionData) { global $db; $expiry = time() + get_cfg_var('session.gc_maxlifetime') - 1; // escape session ID & session data $sessionId = $db->real_escape_string($sessionId); $sessionData = $db->real_escape_string($sessionData); $result = $db->query("SELECT sessionid FROM sessions WHERE sessionid='$sessionId'"); // check if a new session must be stored or an existing one must be updated ($result->num_rows > 0) ? $db->query("UPDATE sessions SET sessionid='$sessionId',expiry='$expiry',sessiondata= '$sessionData' WHERE sessionid='$sessionId'") or die(mysqli_error($db)) : $db->query("INSERT INTO sessions (sessionid,expiry,sessiondata) VALUES ('$sessionId','$expiry','$sessionData')") or die(mysqli_error($db)); return true; } // define 'destroySession()' function function destroySession($sessionId) { global $db; // escape session ID $sessionId = $db->real_escape_string($sessionId); $db->query("DELETE FROM sessions WHERE sessionid='$sessionId'"); return true; } // define 'gcSession()' function function gcSession($maxlifetime) { global $db; $db->query("DELETE FROM sessions WHERE expiry < NOW()"); return true; } ?> |
这是登录的会话设置:
1 |
按功能设置:
1 2 3 4 5 |
我的登录系统如何检查我是否已经登录?
1 2 3 4 5 6 7 |
原来,$_SESSION的值,根据var_dump是array(0) { }
以及检查 Web 应用程序本身:
1 2 3 4 5 6 7 8 |
现在,这就是我在顶部的 index.php 文件中的内容:
1 2 3 4 | require('includes/session.functions.php'); session_set_save_handler('openSession', 'closeSession', 'readSession', 'writeSession', 'destroySession', 'gcSession'); session_start(); include('includes/authorisation.php'); |
这是数据库:
如果我注释掉
1 2 3 | print_r(session_set_save_handler('openSession', 'closeSession', 'readSession', 'writeSession', 'destroySession', 'gcSession')); == 1 var_dump(session_set_save_handler('openSession', 'closeSession', 'readSession', 'writeSession', 'destroySession', 'gcSession')); == bool(true) |
知道我做错了什么吗?
您似乎混淆了两种不同的时间格式。在 mysql 中,
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
在 php 中,
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
在您的 sql 语句中,您正在比较两者,您可以吗?因为它们是完全不同的格式。
原来有一个额外的 session_start(),它会重置会话。