登录中断并使用PHP/MySQL返回页面
我目前正在使用PHP/MySQL,我想知道最好的方法是让未经验证的用户登录,然后让他们自动重定向到他们登录的页面 例如: 1) 用户试图打开安全页面。登录中断并使用PHP/MySQL返回页面,php,mysql,login,scripting,Php,Mysql,Login,Scripting,我目前正在使用PHP/MySQL,我想知道最好的方法是让未经验证的用户登录,然后让他们自动重定向到他们登录的页面 例如: 1) 用户试图打开安全页面。 2) 他们被引导并提示登录。 3) 在他们成功登录后,他们将返回到他们试图访问的URL 目前,在我的“安全”页面上,我设置了以下PHP代码:(甚至不确定这是否是实现“安全”的正确方法): 如果您对如何提供此功能(并可能使我的“安全”代码更好)有任何建议,我将不胜感激 谢谢 您可以在/login/index.php文件中检索referer UR
2) 他们被引导并提示登录。
3) 在他们成功登录后,他们将返回到他们试图访问的URL 目前,在我的“安全”页面上,我设置了以下PHP代码:(甚至不确定这是否是实现“安全”的正确方法):
如果您对如何提供此功能(并可能使我的“安全”代码更好)有任何建议,我将不胜感激
谢谢 您可以在/login/index.php文件中检索referer URI,并将其作为隐藏字段提供给登录表单。用户登录后,您只需将他重定向到他以前试图访问的页面 诸如此类: /login/index.php:
<?php
// if user submitted login form
if(isset($_POST['login'])) {
// do your login actions here
// and after...
header("Location: " . $_POST['returnto']);
}
//
$user_came_from = htmlspecialchars( $_SERVER['HTTP_REFERER'] );
?>
<form name="my_login_form" action="" method="post">
user: <input type="text" name="user" value="" />
pass: <input type="password" name="pass" value="" />
<input type="hidden" name="returnto" value="<?php echo $user_came_from ?>" />
<input type="submit" name="login" value="login" />
</form>
用户:
通过:
您可以将请求的URL存储在会话变量中
$_SESSION['destination'] = $_SERVER['REQUEST_URI'];
然后,登录后,您可以执行以下操作:
if (isset($_SESSION['destination'])) {
header('Location: ' . $_SESSION['destination']);
exit;
}
而且,这不是一个好主意
print" <script>
window.location=\"../login/index.php\"
</script> ";
print" <script>
window.location=\"../login/index.php\"
</script> ";
header('Location: ../login/index.php');
exit; // Call exit to make sure the rest of your code never executes