2013-04-04 126 views
0

我想知道是否有人可以帮助我 - 我已经成功创建了一个登录系统,允许用户(学生)登录。我的系统还需要管理员登录,管理员有权查看学生没有的页面。管理员和学生信息都来自两个不同的表格。以下是我用于学生登录的代码(有两个不同的页面 - 用户和登录)。我坚持如何实施管理登录。任何帮助表示赞赏! (管理员将登录使用 'adminnum' 和 'ADMINPASSWORD'。管理员和用户从两个不同的表登录

的login.php

<?php 

include "core/init.php"; 
include "includes/content.php"; 

if (empty($_POST) === false) { 
$studentemail = $_POST ['studentemail']; 
$studentpassword = $_POST ['studentpassword']; 

if (empty($studentemail) === true || empty($studentpassword) === true) { 
    $errors[] = "You need to enter an email address and password"; 
} else if (user_exists($studentemail) === false) { 
    $errors[] = "We can't find that email address. Have you registered?"; 
} else { 

    if (strlen($studentpassword) > 32) { 
     $errors[] = 'Password too long'; 
     } 

    $login = login($studentemail, $studentpassword); 
    if ($login === false) { 
     $errors[] = 'That email/password combination is incorrect'; 
    } else { 
     $_SESSION['studentid'] = $login; 
     header('Location: index.php'); 
     exit(); 
    } 
} 
} else { 
$errors[] = 'No data received'; 
} 
include "includes/overall/overall_header.php"; 
if (empty($errors) === false) { 
?> 
<h2> We tried to log you in, but...</h2>  

<?php 
echo output_errors($errors); 
} 
?> 
    <center><input id="submit" type="submit" value="Back" onclick="location.href='Login2.php'"></center> 
<?php 
include "includes/overall/overall_footerloggedout.php"; 
?> 

users.php

<?php 
function logged_in() { 
return (isset($_SESSION['studentid'])) ? true : false; 
} 

function user_exists($studentemail) { 
$studentemail = sanitize($studentemail); 
$query = mysql_query("SELECT COUNT(`studentid`) FROM `student` WHERE `studentemail` 
     = '$studentemail'"); 
return (mysql_result($query, 0) == 1) ? true : false; 
} 

function studentid_from_student ($studentemail) { 
$studentemail = sanitize($studentemail); 
return mysql_result(mysql_query("SELECT `studentid` FROM `student` WHERE  `studentemail` = '$studentemail'"), 0, 'studentid'); 
} 
`function login($studentemail, $studentpassword) { 
$studentid = studentid_from_student($studentemail); 

$studentemail = sanitize($studentemail); 
$studentpassword = md5($studentpassword); 

return (mysql_result(mysql_query("SELECT COUNT(`studentid`) FROM `student` WHERE `studentemail` = '$studentemail' AND `studentpassword` = '$studentpassword'"), 0) == 1) ? $studentid : false; 

} 
?> 

回答

0

我建议改变你的逻辑,从两个不同的表中提取用户和管理员,只让他们进入一个表,但所有用户应包含列flag例如,其中标志= 1是ADMIN并且标志= 0是USER。

+0

为什么将列标记为“flag”而不是像“is_admin”那样更有用?名字不好的领域和写得不好的代码一样糟糕。 – TheOx 2013-04-04 13:45:28

+1

is_admin名称过于严格,对于更多类型的用户不灵活。我认为'flag'具有普遍性,但user_type可能更好。因为在进一步使用中,您可能有10种类型的用户,并且只有其中一个用户是admin。为什么列必须是is_admin呢? – 2013-04-04 13:51:11

+0

我想我需要两张桌子?一个给学生,一个给管理员。我更喜欢有两个独立的表格,因为它们都有不同的列并存储不同的数据。当添加新学生时,他们被添加到学生表中,并给出类型'0',并且当添加新管理员时,他们被添加到管理表并且给出类型'1'。我只是想知道如何使用存储在指定表中的值作为学生或管理员登录。我需要一个连接子句的SQL查询还是有一个更简单的方法? – user2244961 2013-04-04 14:07:50

0

所有我可以建议,因为我不是一个php编码器,但已经做了一些过去是在你的数据库中添加另一个字段,你将设置用户权限级别(0为普通成员,1为管理员)。完成之后,只需通过php编码将其添加到您的用户脚本中,但我几乎不知道。希望有一点帮助。