2011-11-17 73 views
0

我有以下字段的MySQL表:PHP登录不工作

ID 用户名 密码 作用

管理有一定的作用1号和普通用户有一定的作用2号

<?php 
// we must never forget to start the session 
session_start(); 

$errorMessage = ''; 
if (isset($_POST['username']) && isset($_POST['password'])) { 
    include 'library/connect.php'; 

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

    // check if the user id and password combination exist in database 
    $sql = "SELECT role FROM user WHERE username = '$username' AND password = '$password'"; 

    $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
    $row = mysql_fetch_array($result); 

    if (mysql_num_rows($result) == 1 AND $row['role']==2) { 
     // the user id and password match, 
     // set the session 
     $_SESSION['userame'] = true; 
     // after login we move to the main page 
     header('Location: login_success.php'); 
     exit; 
    } 
    elseif (mysql_num_rows($result) == 1 AND $row['role']== 1) { 
     // the user id and password match, 
     // set the session 
     $_SESSION['admin'] = true; 
     $_SESSION['username'] = true; 
     // after login we move to the main page 
     header('Location: admin.php'); 
     exit; 
    } 
else { 
     $errorMessage = 'Sorry, wrong user id/password <a href="login.html">Go Back</a>'; 
    } 

    include 'library/closedb.php'; 
} 
?> 



<?php 
if ($errorMessage != '') { 
?> 
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p> 
<?php 
} 
?> 

当管理员日志,它工作正常并且被重定向到admin.php的,而当在正常用户登录,什么都不会发生在所有的页面只是得到刷新。我究竟做错了什么?

+3

**警告**此代码易受** [SQL注入](http://en.wikipedia.org/wiki/SQL_injection)**影响。 – mellamokb

+2

我知道,不专注于secuirty atm – Jahed

+0

这是一个错字在这里或在您的代码? '$ _SESSION ['userame'] = true;'不应该是$ _SESSION ['username']? –

回答

6

您在第一if块拼写错误的用户名role=2

$_SESSION['userame'] = true;

+0

噢耶....silly我......谢谢:) – Jahed

4

看起来像一个错字是原因:

$_SESSION['userame'] = true; 
0
$_SESSION['userame'] = true; 
to 
$_SESSION['username'] = true; 

如果不修复问题,您可以给login_success .php

if (mysql_num_rows($result) == 1 AND $row['role']==2) { 
     // the user id and password match, 
     // set the session 
     $_SESSION['username'] = true; 
     // after login we move to the main page 
     header('Location: login_success.php'); 
     exit; 
    } 
+0

你有点迟了:) –

+0

是的,它的第一天和第一反应几乎不可能 –