首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使PHP登录脚本能够正确地登录用户?

如何使PHP登录脚本能够正确地登录用户?
EN

Stack Overflow用户
提问于 2016-04-21 00:48:30
回答 3查看 177关注 0票数 0

我是PHP新手,我正在尝试开发一个简单的登录系统,它会回显成功消息并重定向到安全页面,当细节错误时,它会回显错误消息并重新加载登录表单。

我已经尝试了很长一段时间了,虽然我有一些指向正确页面的功能,但我还是想不出这一点。

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

PHP代码:

代码语言:javascript
复制
<?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码

代码语言:javascript
复制
<!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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-21 06:10:16

代码语言:javascript
复制
...
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将需要检查是否发布了特定的输入:

代码语言:javascript
复制
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if($_POST['username'] && $_POST['password'])
    {
        //do work
    }
} 

下面是一个基本的php表单教程来开始您的工作:php教程

票数 1
EN

Stack Overflow用户

发布于 2016-04-21 00:58:50

我想这是因为你没有发帖子就把它转到了同一个页面。我没有看完所有的代码,这只是我第一次尝试

票数 0
EN

Stack Overflow用户

发布于 2016-04-21 01:01:13

这将不会出现在loginform.html上:

代码语言:javascript
复制
echo "Wrong details";

使用这样的方法:

代码语言:javascript
复制
$_SESSION['errorMessage'] = "Wrong details";
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
exit();

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

代码语言:javascript
复制
if(isset( $_SESSION['errorMessage'])) echo $_SESSION['errorMessage'];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36757696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档