2011-10-24 36 views
1

我对此登录方法感到困惑,特别是对于登录后某些用户的不同访问页面。php-mysql:登录时不同的访问页面

我在DB像一些用户列表:

  1. 管理
  2. 用户

我想,如果我登录的admin将直接向input.php如果我登录为user将直接拨打至index.php。我从一些网站上读到它必须使用开关盒。但是,我仍然不清楚如何使用它。

$dbc=mysql_connect(_SRV,_ACCID,_PWD) or die(_ERROR15.": ".mysql_error()); 
$db=mysql_select_db("qdbase",$dbc) or die(_ERROR17.": ".mysql_error()); 

switch(postVar('action')) { 
     case 'submitlogin': 
       submitlogin(postVar('loguser'),postVar('logpass')); 
       mysql_close($dbc); 
       exit; 
       break; 
} 

function submitlogin($loguser,$logpass){ 
     if(isset($loguser,$logpass)){ 
       $myuser= mysql_real_escape_string($loguser); 
       $mypass= mysql_real_escape_string($logpass); 
       $sql= sprintf("SELECT * FROM admin WHERE user = '".$myuser."' AND password = '".$mypass."'", $myuser,$mypass); 
       $result = mysql_result($sql) or die (_ERROR26.": ".mysql_error()); 
       if(mysql_num_rows($result) > 0){ 
         session_register("loguser"); 
         session_register("logpass"); 

     switch $myuser{       //i dont know its correct or not 
      case 'admin': 
       header("location:input.php"); 
       break; 
      case 'user': 
       header("location:index.php"); 
       break; 
      }  
?> 
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> 
<html xmlns='http://www.w3.org/1999/xhtml'> 
<head> 
     <META http-equiv='refresh' content='2; url=index.php'> 
</head> 
<title>Login success</title> 
<body> 
     <h1>You logged in !</h1> 
</body> 
</html> 
<?php 
    }else 
     { 
      header("location:log.php?msg=" . urlencode("Wrong Username or Password. Please retry")); 
     } 
    }else{ 
     header("location:log.php?msg=" . urlencode("Please enter some username and password")); 
    } 
} 
?> 

我得到这个错误:

PHP Warning: mysql_result() expects at least 2 parameters, 1 given in /home/jeinqa/www/oqc/dolog.php on line 29 // on line 17 in this page 
+0

'mysql_real_escape_string(stripsl灰烬($ loguser));这里不需要'stripslashes,应该省略。 – Johan

+0

在数据库中清楚地存储密码是一个基本罪。用SHA2代替盐渍散列。 – Johan

+0

@Johan:好的,我已经删除它了。但是我的脚本中的开关盒怎么样?我该怎么做才能让我的登录页面对每个用户有不同的访问权限? – nunu

回答

2

如果你正在尝试做的是用户的“管理”重定向而不是向其他后登录页面,

header("location:index1.php"); //i want to use switch-case in this part 

你需要类似

switch($loguser) { 
    case "admin" : 
     header("location:input.php"); 
     break; 
    default: // this branch will run for all users other than 'admin' 
     header("location:index.php"); 

} 
+0

我试过这个,但是管理员仍然直接指向index.php。为什么? – nunu

+0

@nunu然后确保$ loguser实际上是“admin”。 – Shaokan

+0

@ Shaokan:我收到了一些错误信息。 – nunu