简体   繁体   English

如何使“登录”页面识别用户类型?

[英]How do I make the Login page identify the type of users?

I am using html, PHP and MySQL(phpmyadmin). 我正在使用html,PHP和MySQL(phpmyadmin)。 This might be a very simple question but I am a beginner so this is very new to me. 这可能是一个非常简单的问题,但是我是一个初学者,所以这对我来说是个新手。 My system has has 4 types of users. 我的系统有4种类型的用户。 Lets say user type 1 has a username Tom, user type 2 has a username Alice, user type 3 has a username Mike and user type 4 has a username Mary. 假设用户类型1的用户名是Tom,用户类型2的用户名是Alice,用户类型3的用户名是Mike,用户类型4的用户名是Mary。 I want the Login page to identify which type of user it is when the username is written. 我希望“登录”页面能够识别出写入用户名时的用户类型。 For instance, if it is Tom, I want the system to identify that he is user type 1 and redirect him to a specific page. 例如,如果是Tom,我希望系统识别出他是用户类型1,并将其重定向到特定页面。 Likewise if it is Alice, her user type should be identified and she should be redirected to another page. 同样,如果是Alice,则应识别她的用户类型,并应将她重定向到另一个页面。 Not the same page as user type 1. Please let me know of the simplest ways this could be achieved. 与用户类型1不同的页面。请告知我最简单的方法。 Thank you so much in advance. 提前非常感谢您。

This is what I have done so far. 到目前为止,这是我所做的。 But it is not working. 但这是行不通的。 Please let me know what I have to do. 请让我知道我该怎么办。

if (isset($_POST['username'])) {
$username = $_POST ['username'];
$password = $_POST ['password'];
$usertype = $_POST ['user_type'];
$sql = "SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."' LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 1) {
    if ($username ['user_type'] == admin) {
        header('location: localhost/adminhomepage.php');
    }
    else if ($username ['user_type'] == po) {
        header('location: localhost/pohomepage.php');
    }
    else if ($username ['user_type'] == pw) {
        header('location: localhost/pwhomepage.php');
    }
    else if ($username ['user_type'] == ps) {
        header('location: localhost/pshomepage.php');
    }
    else{
        echo "error determining user type!";
        exit();
    }
}
else {
    echo "Invalid login information. Please try again.";
    exit();
}

} }

Try this, create a separate column like role in mysql.Based on the usertype put values in that column like if usertype 1=role is 1 ,usertype 2=role is two... 尝试此操作,在mysql中创建一个像角色一样的单独列。基于usertype在该列中输入值,例如usertype 1 = role为1,usertype 2 = role为两个...

On particular login based on value in column name role,redirect user to the with respective page. 根据列名角色中的值进行特定的登录时,将用户重定向到具有相应页面的。

Thanks 谢谢

EDIT: This is based on your last code addition edit 编辑:这基于您上一次的代码添加编辑

// Needs to be at the top of every page
// This is used to recall your user status
session_start();

if (isset($_POST['username'])) {
    // You need to sanitize this or you have a potential security issue
    $username = mysql_real_escape_string($_POST ['username']);

    // You should not be storing in plain text. You should have this encrypted
    // You should store it with PHP's latest encryption functions or at least do
    // a salt + hash.....but at least do a hash
    // hash("sha512", $_POST['password']);
    $password = mysql_real_escape_string($_POST['password']);

    // I am not sure of the relevance of this field
    $usertype = $_POST['user_type'];

    $sql = "SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."' LIMIT 1";

    // You should be using mysqli_ or PDO with prepared statements
    $query = mysql_query($sql);

// You need to fetch the results. All you have done is check if user exists
 if (mysql_num_rows($query) == 1) {
    // Fetch results
    $user = mysql_fetch_assoc($query);

    // You should set results to a session for later checking
    $_SESSION['user_type'] = $user['user_type'];
    $_SESSION['username'] = $user['username'];

    // You have to quote the checks or they are invalid
    if ($_SESSION['user_type'] == 'admin') {
        header('location: localhost/adminhomepage.php');
    }
    else if ($_SESSION['user_type'] == 'po') {
        header('location: localhost/pohomepage.php');
    }
    else if ($_SESSION['user_type'] == 'pw') {
        header('location: localhost/pwhomepage.php');
    }
    else if ($_SESSION['user_type'] == 'ps') {
        header('location: localhost/pshomepage.php');
    }
    else{
        echo "error determining user type!";
        exit();
    }
}
else {
    echo "Invalid login information. Please try again.";
    exit();
}

} }

Add a new column in your database for user type. 在数据库中为用户类型添加新列。 It usually makes sense to create the column with a default value unless they will always be unique. 通常使用默认值创建列是有意义的,除非它们将始终是唯一的。

/*/your SQL query/*/

//if you need to validate type throughout the site, setup sessions as @Rasclatt suggested//

if ($user['type'] == 1) {
    header('location: http://domain.com/page1.html');
}else if ($user['type'] == 2) {
    header('location: http://domain.com/page2.html');
}else if ($user['type'] == 3) {
    header('location: http://domain.com/page3.html');
}else if ($user['type'] == 4) {
    header('location: http://domain.com/page4.html');
}else{
    echo "error determining user type!";
    exit;
}

add a column in your user table named user_type. 在用户表中添加名为user_type的列。 add user type while user registration. 在用户注册时添加用户类型。 after that while log in you can check by your query which user type is logging in. 之后,在登录时,您可以通过查询检查正在登录的用户类型。

and if you want to show your user type on next page, you can store user_type in session. 如果要在下一页显示用户类型,则可以将user_type存储在会话中。

feel free to ask any further problems. 随时问任何其他问题。

I hope this can help you. 希望对您有所帮助。

First of all you need a MySQL skills to get what you want, because the user type/role depends on different MySQL table's. 首先,您需要具备MySQL技能才能获得所需的内容,因为用户类型/角色取决于不同的MySQL表。 for example... In almost every website you notice that there is a admin, user and also some has their one sub admin or many. 例如...在几乎每个网站中,您都注意到有一个管理员,用户,还有一些拥有一个或多个子管理员。 All have their different rights and when they try to login then each one redirect to their specified page, like user will be redirected to home page, admin's will redirect to admin panel index etc.. 所有人都有不同的权限,当他们尝试登录时,每个人都会重定向到其指定的页面,例如用户将被重定向到主页,管理员将被重定向到管理面板索引等。

Now you need to create different table for each user in MySQL and assign them different rights, or you can also create different column based on Boolean values like 0 for some action and 1 for some action... Hope it will help you... 现在您需要为MySQL中的每个用户创建不同的表并为其分配不同的权限,或者您还可以基于布尔值创建不同的列,例如0表示某些操作,1表示某些操作...希望对您有所帮助...

暂无
暂无

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

相关问题 如何防止登录用户访问登录页面? - how do i prevent logged in users from accessing the login page? 首次登录后30天,如何使WordPress删除订户用户 - How do i make WordPress delete subscriber users 30 days after their first login CakePHP 3-如何阻止未登录用户的页面并将其重定向到登录页面? - CakePHP 3 - How do I block pages from non logged in users and redirect them to the login page? 成功登录后如何将用户重定向到新的PHP页面? - How do I redirect users to new PHP page after successful login? 成功登录后,如何让我的虚假登录页面显示“已授予访问权限”而没有其他内容? - How do I make my fake login page display “Access Granted” with no other content after a successful login? 如何使管理员在laravel中将特定类型的用户(学生)与另一类型的用户(他们各自的指导者)匹配 - How do I make it possible for Admins to match a particular type of users(students) to another type of users (their respective mentors) in laravel 如何确保没有登录就无法访问除登录页面之外的其他页面? - How do I make sure that no other page apart from login page can be accessed without logging in? 用户登录自定义帖子类型存档页面 - users login for custom post type archive page 如何确保只有登录用户才能访问页面? - How do I make sure only logged-in users can acces a page? 如何在我的网站上为许多用户创建链接但指向相同的模板页面? - How do I make a link on my website for many users but pointing to the same template page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM