dongshen2903 2011-12-22 16:02
浏览 55
已采纳

PHP If / Else语句

I have a mysql table called users with the following fields:

username - password - role

The role consists of normal user and admin.

What I want is so when an admin logs in it gets redirected to another page and not the same page a normal user would log in. My code is as follows:

<?php
include("../includes/db.php");

$username=$_POST['username'];
$password=$_POST['password'];

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM Users WHERE username='$username' and password='$password' And role = 'normaluser'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("username");
session_register("password");
header("location:myaccount.php");
}
?>

I have the code working for the normal user but I just don't know how to write the code for the admin part.

  • 写回答

4条回答 默认 最新

  • duanqian8867 2011-12-22 16:07
    关注

    I'd say

    $sql="SELECT role FROM Users WHERE username='$username' and password='$password'";
    $result=mysql_query($sql);
    while ($row = mysql_fetch_object($result)) {
        //one hit i hope?!
        //DONT save your apssword in the session!!!!
        //session_register("username");
        //session_register("password");
    
        $role = $row->role;
    
    }
    if($role == "user"){
        redirect1
    }elseif($role == "admin"){
       redirect2;
    }else{
      redirect panic
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程