简体   繁体   English

多级登录页面根据用户角色php将用户重定向到其他页面

[英]Multi level login page redirect users to different pages based on user role php

I need to redirect users to different pages based on the roles given to them in the database. 我需要根据数据库中赋予他们的角色将用户重定向到不同的页面。 Only the username and password is submitted on the login page. 在登录页面上仅提交用户名和密码。

it show me the error : Your login session data is not on record in the database. 它向我显示错误:您的登录会话数据未记录在数据库中。 please help me how to fix this problem. 请帮助我如何解决此问题。

Login page 登录页面

       <?php
       session_start();
       if(isset($_SESSION["admin"])){
        header("location: index.php");
        exit();
       }
       ?>
       <?php
       if(isset($_POST["username"]) && isset($_POST["password"])){
       $admin = preg_replace('#[^A-Za-z0-9]#i', '',$_POST["username"]);
       $password = preg_replace('#[^A-Za-z0-9]#i', '',$_POST["password"]);


        include"include/connect_to_mysql.php";// Connect to server and select databse.
        $sql=mysql_query("SELECT * FROM admin WHERE username='$admin' AND     password='$password' LIMIT 1");
        $exitCount=mysql_num_rows($sql);
        if($exitCount==1){
        $row = mysql_fetch_array($sql);
        $id = $row['id'];
        $role = $row['user_role'];

        if($role =='admin'){
        $link = 'index.html';
        }
        elseif($role =='hsr'){
        $link = 'http://www.google.com';
        }
        $_SESSION["id"] = $id;
        $_SESSION["admin"] = $admin;
        $_SESSION["password"] = $password;
        $_SESSION["role"] = $role;
        header("location: ".$link."");
        exit();
        }else {
       echo "Wrong Username or Password";
       }
       }
       ?>

index page 索引页

       <?php
        session_start();
        if(!isset($_SESSION["admin"])){
        header("location: login.php");
        exit();
       }
       $adminID = preg_replace('#[^0-9]#i','',$_SESSION["id"]);
       $admin = preg_replace('#[^A-Za-z0-9]#i', '',$_SESSION["admin"]);
       $password = preg_replace('#[^A-Za-z0-9]#i', '',$_SESSION["password"]);
       $role = preg_replace('#[^A-Za-z0-9]#i', '',$_SESSION["role"]);

       include"include/connect_to_mysql.php";
       $sql=mysql_query("SELECT * FROM admin WHERE id='$adminID' AND username='$admin'    AND password='$password' LIMIT 1");
       $exitCount=mysql_num_rows($sql);
       if($exitCount==0){
        echo "Your login session data is not on record in the database.";
        exit();
        }
        ?>

The code seems fine, it's most likely a problem with the query results. 代码看起来不错,查询结果很可能是一个问题。 My advice would be to output the values of your code step-by-step starting with the query until you find where the problem stems from: 我的建议是从查询开始逐步输出代码的值,直到找到问题的根源:

$query = "SELECT * FROM admin WHERE id='$adminID' AND username='$admin' AND password='$password' LIMIT 1";
echo 'Query: ' . $query;
exit;

This will tell you if the query is fine and running it on the database will ensure a user exists with the given criteria. 这将告诉您查询是否正确,并在数据库上运行该查询将确保用户存在给定条件。 From there you can see if it is a data issue or database issue, and work forward to a solution. 从那里,您可以查看这是数据问题还是数据库问题,然后继续寻找解决方案。

PS: It's frowned upon to store passwords in their original form, in case your database is exposed. PS:如果您的数据库暴露在外,以原始形式存储密码是不受欢迎的。 So potentially consider hashing your passwords and comparing the hashes on login. 因此,有可能考虑对您的密码进行哈希处理并比较登录时的哈希值。

PPS: It's always bad to store too much data in the user session, the id should suffice. PPS:在用户会话中存储太多数据总是不好的,id应该足够。 And extra bad for storing the password in there. 而且在其中存储密码也很糟糕。

Goodluck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysql根据角色php将用户重定向到不同的页面 - mysql redirect users to different pages based on role php 根据用户的登录信息将用户重定向到不同的页面 - Redirect users to different pages based on their login informations 用于根据登录角色状态将用户定向到不同页面的按钮 - Button to direct users to different pages based on login role status 如何使用同一登录页面将基于角色的不同用户重定向到不同页面? - How to redirect different users based on roles to different pages using same login page? 使用不同的角色登录(根据用户类型重定向到不同的页面) - Login with different role (redirect to different page according to user type ) 根据他们的登录名将两个不同的用户重定向到单独的页面 - Redirect two different users to separate pages based on their login name 基于用户角色的登录始终重定向到同一页面 - User-role based login always redirect to the same page 登录后根据用户角色重定向到其他页面 - After login redirect to different page according to user role 如何基于多用户系统防护将用户重定向到其他页面? - How to redirect user to different page based on the guard for multi user system? PHP登录重定向到模板页面,以引入不同的用户数据 - PHP Login redirect to template pages pulling in different user data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM