我有一个登录页面如下:
<form action="?" method="post" id="frm-useracc-login" name="frm-useracc-login" >
<div id="login-username-wrap" >
<div class="login-input-item left">
<div class="div-search-label left">
<div id="div-leftheader-wrap">
<p class="a-topheader-infotext left"><strong>Username: </strong></p>
</div>
</div>
<div class="login-input-content left div-subrow-style ui-corner-all">
<input type="text" tabindex="1" name="txt-username" id="txt-username" class="input-txt-med required addr-search-input txt-username left">
</div>
</div>
</div>
<div id="login-password-wrap" >
<div class="login-input-item left">
<div class="div-search-label left">
<div id="div-leftheader-wrap">
<p class="a-topheader-infotext left"><strong>Password: </strong></p>
</div>
</div>
<div class="login-input-content left div-subrow-style ui-corner-all">
<input type="password" tabindex="1" name="txt-password" id="txt-password" class="input-txt-med required addr-search-input txt-password left">
</div>
</div>
</div>
<div id="login-btn-bottom" class="centre-div">
<div id="login-btn-right">
<button name="btn-login" id="btn-login" class="btn-med ui-button ui-state-default ui-button-text-only ui-corner-all btn-hover-anim btn-row-wrapper left">Login</button>
<button name="btn-cancel" id="btn-cancel" class="btn-med ui-button ui-state-default ui-button-text-only ui-corner-all btn-hover-anim btn-row-wrapper left">Cancel</button><br /><br />
</div>
</div>
</form>
这是我的 session.controller.php 文件:
基本上,我想做的是创建第二个登录页面,自动将值传递给 session Controller 并登录。例如,如果我转到 login-guest.php,我会为用户名设置默认值和密码,然后使用 $("#btn-login").trigger('click');
问题是,如果 session 超时, session Controller 会自动返回到 login.php,我不确定如何才能实现这一点。任何帮助将不胜感激!
最佳答案
正如您在评论中提到的,您必须首先知道用户是如何登录的(登录或登录访客),因此无论如何您都需要为每个用户设置某种状态。
现在,如果您不能将 session 超时增加到无限,您可能需要将登录类型存储在其他地方,比如在 cookie 中,或者作为 url 中的查询字符串。
在 cookie 的情况下,它将是这样的:
在 login-guest.php
的登录部分:
...
$expire = 60 * 60 * 24 * 30 * 24 + time(); // 2 years
setcookie('logintype', 'guest', $expire);
这是您将用户发送到登录页面的地方:
if(isset($_COOKIE['logintype']) && $_COOKIE['logintype']=='guest'){
header('Location: login-guest.php');
} else {
header('Location: login.php');
}
我不认为 cookie 可以有无限的生命周期,所以我将有效期设置为两年,您可以更改。显然,如果用户删除 cookie 或使用其他浏览器,它不会持续存在。
关于php - 创建自动登录用户的第二个登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560870/