2015-08-21 65 views
1

我创建了一个HTML登录页面,它有一个用户登录表单和一个主持人登录表单,用户登录工作正常,它连接到数据库,检索电子邮件和密码,然后重定向用户回到主页,我使用的主持人登录基本相同的代码,但它不会工作。PHP主持人登录页面无效

这里是我的数据库的截图 http://i59.tinypic.com/91fcpj.png 这是在用户登录后,将其重定向到index.php http://i61.tinypic.com/21ahjt.png 这是管理员尝试登录后,它的意思是重定向到adminData.php但它不“T http://i58.tinypic.com/23m1r8p.png

的login.html代码

<!DOCTYPE html> 

<html> 

<head> 

<title>Sign In</title> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" type="text/css" href="css/style.css"/> 

</head> 
<body> 
<div id="container"> 

<header> 

<a href="index.html"><img src="images/Header.jpg" alt="logo" /></a> 
<a href="login.html"><img src="images/login.jpg" alt="login" /></a> 
<a href="https://www.facebook.com/ArcticMonkeys"><img src="images/Facebook.jpg" alt="FB" /></a> 
<a href="https://twitter.com/arcticmonkeys"><img src="images/Twitter.jpg"  alt="Twitter" /></a> 

</header> 

<div class="menu"> 
<div align="center"> 
<ul class="list"> 
    <li class="item"><a href="index.html">Home</a> 
    <li class="item"><a href="gallery.html">Gallery</a> 
    <li class="item"><a href="videos.html">Videos</a> 
    <li class="item"><a href="discography.html">Discography</a> 
    <li class="item"><a href="register.php"#">Register</a> 

    <li class="item"><a href="#">About</a> 
     <ul class="list"> 
      <li><a href="alex.html">Alex Turner</a></li> 
      <li class="list"> 
       <a href="matt.html">Matt Helders</a> 
       <ul class="list"> 
        <a href="jamie.html">Jamie Cook</a> 
        <ul class="list"> 
         <a href="nick.html">Nick O'Malley</a> 
         <ul class="list"> 
          <a href="andy.html">Andy Nicholson</a> 
          <ul class="list"> 
         </ul> 
      </li> 


</div> 
</div> 


<div align="center"><BR><BR><BR><BR> 
<body id="body-color"> 

<div id="Sign-In"> 




</head> 

<form action="login.php" method="post"> 

<table width="500" align="center"> 

<tr align="center"> 

<td colspan="3"><h2>User Login</h2></td> 

</tr> 

<tr> 

<td align="right"><b>Email</b></td> 

<td><input type="text" name="email" required="required"/></td> 

</tr> 

<tr> 

<td align="right"><b>Password:</b></td> 

<td><input type="password" name="pass" required="required"></td> 

</tr> 

<tr align="center"> 

<td colspan="3"> 

<input type="submit" name="login" value="Login"/> 


</td> 

</tr> 

</table> 

</form> 

<br><br> 

<form action="moderatorLogin.php" method="post"> 

<table width="500" align="center"> 

<tr align="center"> 

<td colspan="3"><h2>Moderator Login</h2></td> 

</tr> 

<tr> 

<td align="right"><b>Email</b></td> 

<td><input type="text" name="email" required="required"/></td> 

</tr> 

<tr> 

<td align="right"><b>Password:</b></td> 

<td><input type="password" name="pass" required="required"></td> 

</tr> 

<tr align="center"> 

<td colspan="3"> 

<input type="submit" name="Admin" value="Login"/> 


</td> 

</tr> 

</table> 
</form> 






<H3>If you do not have an account please register <a href="register.html">HERE</a><br>otherwise access is restricted to member pages<h3> 

</div> 

</body> 

</html> 

moderatorLogin.php代码

<?php session_start(); 
// establishing the MySQLi connection 



$con = mysqli_connect("localhost","root","","admin"); 

if (mysqli_connect_errno()) 

{ 

echo "MySQLi Connection was not established: " . mysqli_connect_error(); 

} 

// checking the user 

if(isset($_POST['login'])){ 

$email = mysqli_real_escape_string($con,$_POST['email']); 

$pass = mysqli_real_escape_string($con,$_POST['pass']); 

$sel_user = ("select * from moderators where email='$email' AND password='$pass'"); 
$sel_user = ("select * from moderators where email='$email' AND password='$pass'"); 

$run_user = mysqli_query($con, $sel_user); 

$check_user = mysqli_num_rows($run_user); 

if($check_user>0){ 

$_SESSION['email']=$email; 

echo "<script>window.open('adminData.php','_self')</script>"; 

} 

else { 

echo "<script>alert('Email or password is not correct, try again!')</script> 
      <script>window.open('register.php','_self')</script>"; 

} 



} 

?> 

注意:这个网站不会在网上托管,我会在未来的安全性方面让它变得更加复杂,什么不是,但是现在我只想在开始之前得到我的工作。学习安全等...

任何帮助都非常感谢。

Ryan。

+0

那么究竟是什么问题呢?你看过HTML的来源吗?你看看PHP错误日志? – rlanvin

+0

@rlanvin当我输入管理员电子邮件和密码,并尝试登录我被定向到一个空白页面,它应该去moderatorLogin.php确定电子邮件和传递是否正确,如果他们应该重定向到adminData.php,如果不是它应该弹出和错误消息。我没有收到任何错误,这是其中一个问题,我也有完全相同的PHP代码工作的另一种形式只是不是这一个。 –

回答

1

好像你的重定向有问题。

尝试

// similar behavior as an HTTP redirect 
    window.location.replace("http://stackoverflow.com"); 

// similar behavior as clicking on a link 
    window.location.href = "http://stackoverflow.com"; 

window.location.replace(...)将最好的模拟HTTP重定向。

它比使用window.location.href =好,因为replace()不会将原始页面放入会话历史记录中,这意味着用户不会陷入永无止境的后退按钮失败。如果你想模拟某人点击链接,请使用location.href。如果你想模拟一个HTTP重定向,使用location.replace

我不能赞扬这个答案。你可以检查出原来的SO帖子here

+0

感谢您的回复,但在更改我的代码后,它似乎仍然卡住,完全丢失,因为代码工作正常,我有另一个登录表单。 –

+0

@Ryan你有没有保证'如果($ check_user> 0){ $ _SESSION [ '电子邮件'] = $电子邮件; 回声 “”; }'你的代码的一部分实际上得到执行或没有?注释掉JS重定向并尝试在那里随机打印一些东西,这样可以指出实际的问题。 –

+0

它没有得到那么远,错误似乎是在这之前,甚至评论该脚本并尝试打印的东西随机它只是导致 –

0

这不是一个答案,只是我的2美分。

如果有人想知道为什么,有没有真正的理由,只是看到可以没有太多的精力来改善结构,并在数据库中看到文本密码。

但是我确实注意到如何将用户保存到数据库中。 您在此处有两个不同的组,其中表中的 db中的一个组是常规用户,而admin db中的moderator表中的其他组是db中的主持人。

大多数CMS如wordpress,joomla,typo3等..他们使用一个数据库。 你已经有两个只包含关于用户的信息,导致我认为你是过度分类。

我根本不是数据库专家,但不是为两个不同的表使用两个相同(可能稍有不同)数据的独立数据库。

我在这种情况下会做的是在名为user_typeinfo db内的users表中创建一列。

然后在我的应用我会设置一些常数(define('ADMIN', 1)define('USER', 0))等等

在新的user_type列的类型也将是一个数,例如

---------------------------------------------------- 
| name | email | password | user_type | 
---------------------------------------------------- 
| John | [email protected] | sha1(pass) | 0   | 
| Lisa | [email protected] | sha1(pass) | 1   | 
---------------------------------------------------- 

现在您可以使用同一张表获取您的用户和管理员。 你也有用户阵列内的区别与MySQL取它时,那么你永远知道,如果用户是管理员或不,这看起来是这样的:

$_SESSION['user'] = [ 
    'name' => 'John', 
    'email' => '[email protected]', 
    'password' => 'sha1', 
    'user_type', 0 
]; 

现在,当您要检查,如果用户为admin,在你的代码的任何地方(只要你已经设置这个$_SESSION['user']

if ($_SESSION['user']['user_type'] == ADMIN) { //remember the define? :D 
    //do cool admin stuff 
} else { 
    //do regular user stuff 
} 

我注意到另一个次要的事情是,你在你的数据库中使用任何不散列技术。密码以纯文本形式存储。

你可能实际上仍然需要这样做,但在实现用户系统时实现这一点将是理智的,否则你可能会忘记或者将来会懒得做这件事(没有任何意图,只是如何开发一些开发者是 ;))。

PHP有一些内置函数可以处理以单向加密的方式存储密码。

这意味着没有解密密码的方法,因此它是安全的。但是,这也使得比较密码变得更加复杂。

使用的典型方法是PHP's SHA1函数。 你会只是做sha1($_POST['password'])和获得的十六进制字符(0-9A-F)40个字符长的字符串

您的密码应该像下面的东西:

a89f88e9d9ce9d8f9e98d09c90e9f5431a21b12a

当您尝试比较任何密码在纯文本反对上面的散列,它会返回false - 这是正常的。字符串是完全不同的。

但是,当您再次加密相同的字符串时,它会产生相同的输出。

所以这只是一个额外的步骤,但如果有人决定窃取您的数据库,他们也不会窃取您的所有用户密码。

您当前密码校验程序看起来像这样的时刻:

User registers -> save info

Validate login -> (user->password == input->password)

现在你做什么,看起来是这样的:

User registers -> sha1 password -> save info

Validate login -> (user->password == sha1(input->password))

现在我可以去这个很长的时间,但是这将是对你的用户绝对最低的必要性和责任,如果你不保护自己的密码,他们有一个很好的机会,有人闯入你的数据库和抢断该密码。

如果你认为没有任何伤害,你必须认识到,大多数用户都simple和使用一个密码的一切,因为记住多个会比较困难。

这导致了相同的密码登录了很多(也安全)服务,黑客可以利用再发挥到淋漓尽致,因为他得到了一个密码,基本上是免费的。