我遇到了一些麻烦.我正在研究一个小型的cms.当我登录一切都很好.但如果我坐在那里会议似乎要求我在3分钟后再次登录.所以我试着实现一个记住我的功能.而且也没有运气.它还需要我登录.
function logged_in(){ if(isset($_SESSION['email']) || isset($_COOKIE['email'])){ return true; } else { return false; } }
然后我创建了另一个函数,如果页面需要登录而你没有登录.它将重定向.
function require_loggin(){ if (logged_in()) {} else { redirect(ROOT_URI); } }
<?PHP require_loggin(); ?>
$email = clean($_POST['email']); $password = clean($_POST['password']); $remember = isset($_POST['remember']);
最后我的登录.
function login_user($email,$password,$remember){ $active = 1; $connection = dbconnect(); $stmt = $connection->prepare('SELECT user_pwd,user_email,uid,username FROM users WHERE user_email = ? AND active= ?'); $stmt->bind_param('ss',$email,$active); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 1) { $row = $result->fetch_array(); $db_password = $row['user_pwd']; if (password_verify($password,$db_password)) { if($remember == "on") { setcookie('email',time() + 86400); } $_SESSION['uid'] = $row['uid']; $_SESSION['email'] = $row['user_email']; $_SESSION['username'] = $row['username']; return true; } else { return false; } return true; } else { return false; } }
The issue is that once they login the default session dies in about a 4 minutes if they are not clicking links. and the remember me function wont work.. I read some where that a default session should last about 30 minutes. but the session requires login after 4 minutes of not moving through the site.
有人向我提到垃圾收集,但我不得不承认我完全失去了它.
我建议创建一个应用程序配置文件..将其命名为config.PHP并将其包含在页面顶部.就像您的应用程序出现一样简单,我假设您没有使用自动加载器.在其中包含以下snippit:
<?PHP /** * File: config.PHP * This file should be included in every PHP script to configure the session. Like this: * require_once('config.PHP'); */ /* * This is 30 minutes. The length only depends on the requirements of * your application. */ $sessionLength = 30 * 60; ini_set(’session.gc_maxlifetime’,$sessionLength); ini_set(‘session.gc_maxlifetime’,30); session_set_cookie_params($sessionLength,"/","yourdomain.com") session_name('PHPSESSION'); session_start(); //This will force the cookie to reset with a new timeout on every page load. setcookie( session_name(),session_id(),time() + $sessionLength ); ?>