2016-04-21 53 views
0

我是PHP新手,我试图开发一个简单的登录系统,它可以回应成功消息并重定向到安全页面,当细节错误时,它会回应错误消息并重新载入登录表单。我如何获得PHP登录脚本以正确登录用户?

我一直在尝试一段时间,但无法弄清楚,即使我有一些功能,它指向正确的页面。

PhpMyAdmin上的我的数据库已正确配置。此外,任何会议的帮助将不胜感激。

PHP代码:

<?php 

$servername = "localhost"; 
$username = "root"; 
$password = "cornwall"; 

$con=mysqli_connect('localhost','root','cornwall','ibill'); 
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill': 

$username = $_POST['username']; 
$password = $_POST['password']; 
//These are the different PHP variables that store my posted data. 

$login="SELECT * FROM users WHERE username='$username' AND password='$password'"; 
$result=mysqli_query($con, $login); 
$count=mysqli_num_rows($result); 
//This is the query that will be sent to the MySQL server. 

if($count==1) 
{ 
    header('Location: http://localhost/projects/ibill_v3/html/main.html#home'); 
    exit(); 
} 
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page. 
else { 
    header('Location: http://localhost/projects/ibill_v3/html/loginform.html'); 
    echo "Wrong details"; 
    exit(); 
} 
//If login details are incorrect 

/** Error reporting */ 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
?> 

HMTL代码

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <meta name="viewport" content="width=device-width, initial-scale=1; minimum-scale=1;"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> 
    <link href="/projects/ibill_v3/css/mainstyles.css" rel="StyleSheet"/> 
    <link href="/projects/ibill_v3/css/loginform.css" rel="StyleSheet"/> 
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
    <script src="script.js"></script> 
    <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
    <script type='text/javascript'> 
      $(document).on('pageinit', function(){ 
       $('.loginform').validate({ // initialize the plugin 
        // rules & options 
       }); 
      }); 
    </script> 
</head> 

<body> 
<!--********************************LOGIN FORM PAGE**********************************************--> 
<!--****************************************************************************************--> 

<!--********************************HEADER**********************************************--> 
<div data-role="page" id="loginform"> 
    <div data-role="header" data-id="foo1" data-position="fixed"> 
    <h1>Register</h1> 
    </div> 
<!--********************************HEADER**********************************************--> 

<!--********************************MAIN**********************************************--> 
    <div data-role="main" class="ui-content"> 
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190"> 
    <h2>Sign in</h2> 
    <section class="loginform"> 
     <form data-ajax="false" method="POST" action="loginform.php" > 
     <ul> 
      <li> 
      <label for="username">Username</label> 
      <input type="text" name="username" id="username" class="required" minlength="5" placeholder="enter username (min-5 characters)"> 
      </li> 
      <li> 
      <label for="password">Password</label> 
      <input type="password" name="password" placeholder="enter password" minlength="6"> 
      </li> 

      <div id="loginformbutton"> 
      <button class='active' type='submit' value='submit'>Sign in</button> 
      </div> 
      <p>Don't have an account? Sign up!</p> 
      <div id="registerbutton"> 
      <a href="/projects/ibill_v3/html/register.html" data-role="button">Register</a> 
      </div> 
     </ul> 
     </form> 
    </section> 

    </div> 
<!--********************************MAIN**********************************************--> 

<!--********************************FOOTER**********************************************--> 
    <div data-role="footer"> 
    <footer class="footer"> 
     <p>awilliams&copy;</p> 
    </footer> 
    </div> 
</div> 
<!--********************************END OF LOGIN FORM PAGE**********************************************--> 
<!--****************************************************************************************--> 
</body> 
+1

** WARNING **:当使用'mysqli'你应该使用参数化查询和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)将用户数据添加到您的查询中。 **不要**使用字符串插值或连接来完成此操作,因为您创建了严重的[SQL注入漏洞](http://bobby-tables.com/)。 **绝不**将'$ _POST'或'$ _GET'数据直接放入查询中。 – tadman

+0

**警告**:编写自己的访问控制层并不容易,并且有很多机会使其严重错误。请不要在[Laravel](http://laravel.com/)等任何现代开发框架(http://codegeekz.com/best-php-frameworks-for-developers/)上编写自己的认证系统,内置了强大的[认证系统](https://laravel.com/docs/5.2/authentication)。绝对不要遵循[推荐的安全最佳实践](http://www.phptherightway.com/#security),也绝不要将密码存储为纯文本格式。 – tadman

+0

@PedroLobito该教程使用纯文本密码并且不使用预准备语句。这是什么不该做的一个很好的例子。这是任何框架中解决的问题。只要用他们提供的任何东西没有必要重新发明轮子,并做得不好。 – tadman

回答

1
... 
else 
{ 
    header('Location: http://localhost/projects/ibill_v3/html/loginform.html'); 
    echo "Wrong details"; 
    exit(); 
} 

以上将在您的echo声明到达之前重定向,因此不会显示任何内容。

其次,下面一行:
<form data-ajax="false" method="POST" action="loginform.php" >不会将任何数据发送回包含窗体如果您正在使用echo声明文件。它将重定向到loginform.php,并且如果您没有显式地将该页面重定向到您的表单,它将停留在那里。

改为使用:
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>作为表单的动作。然后将您的loginform.php包含在HTML表单之前的某个位置。
这将发送数据到表单的文件并将特殊字符替换为HTML实体(为了安全),它还允许您使用echo或变量将消息返回给用户。

loginform.php需要检查是否存在特定的输入被贴:

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    if($_POST['username'] && $_POST['password']) 
    { 
     //do work 
    } 
} 

这是一个基本的PHP形式的教程,您开始使用:php tutorial

0

我想这是因为你的转向与无后相同的页面。我没有通过所有的代码看,那只是我在它

0

首次尝试这不会对loginform.html出现:

echo "Wrong details"; 

使用类似:

$_SESSION['errorMessage'] = "Wrong details"; 
header('Location: http://localhost/projects/ibill_v3/html/loginform.html'); 
exit(); 

而且然后在loginform.html上添加此代码以显示错误消息:

if(isset($_SESSION['errorMessage'])) echo $_SESSION['errorMessage']; 
+0

谢谢,只是尝试过,无济于事。我把最后一段代码放到了html文件中的form标签中,并将其封装在php标签中?这是错的吗? – asharoo85

+0

最后一个代码片段应该包含在php标签中。 – fislerdata

+0

我这样做了,但它没有工作 – asharoo85