且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果未在管理面板上登录,则需要有关如何重定向用户的指南

更新时间:2023-11-27 09:37:40

***的方法是使用sessions. 在login.php中执行以下操作

Best way to do that is with sessions. In the login.php do something like this

if (mysql_num_rows($result) == 0) {
    header("Location: admin_login.php");
} else   {
    header("Location: admin_control_panel.php");
    session_start();
    $_SESSION['user'] = $_POST['username'];
}

现在在文件顶部的admin_control_panel.php中,只需添加此php代码以检查$ _SESSION ['user']是否存在.

Now in the admin_control_panel.php at the top of the file, just add this php code to check if $_SESSION['user'] exists.

<?php
if (! isset($_SESSION['user'])) {
  header("Location: admin_login.php");
}
?>

基本上,使用此代码可以在登录正确的情况下使用用户数据创建会话.如果不是,默认情况下,他将被重定向到登录页面. 现在,当有人尝试访问admin_control_panel页面时,我们将首先检查是否设置了会话.如果是真的,他可以访问该页面,否则,他将被重定向到登录名.

Basically with this code you will create session with user data if login is correct. If it's not, he will by default get redirected to the login page. Now when someone tries to access admin_control_panel page, we will first check if session is set. If it's true, he can access the page, if not, he will get redirected to the login.

有关会话的更多信息,请参见: PHP.net会话手册

For more read about session: PHP.net Session manual and w3schools.com Session manual

*注意.要注销,您必须销毁会话,使用session_destroy();函数即可.

*Note. To logout, you gotta destroy session, to do that use session_destroy(); function.