如何在php中的同一页面上显示错误消息

时间:2016-04-01 12:34:25

标签: php html mysql pdo

我目前正在开发一个与朋友使用pdo的php许可密钥登录,但我们需要帮助在同一页面上显示错误消息,因为它们当前显示如下:http://prntscr.com/amrg8k 我们正在使用bootstrap

我的配置:

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'sprx');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('ERRMSG', 'ERROR: SOMETHING IS WRONG!');

try {
$odb = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD);
}
catch( PDOException $Exception ) {
    die(ERRMSG);
}

function error($string)
{
return '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>ERROR:</strong> '.$string.'</div>';
}

function success($string)
{
return '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>SUCCESS:</strong> '.$string.'</div>';
}

define('DIRECT', TRUE);
require 'func.php';
$user = new user;
?>

我的登录信息:

<?php
require 'inc/php/php.php';
if ($user -> LoggedIn())
{
header('Location: index');
die();
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="passion">
    <meta name="keyword" content="">

    <title>Passion | Key System</title>

    <!-- CSS -->
    <link href="inc/assets/css/bootstrap.css" rel="stylesheet">
    <link href="inc/assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <!-- Custom -->
    <link href="inc/assets/css/style.css" rel="stylesheet">
    <link href="inc/assets/css/style-responsive.css" rel="stylesheet">
    <!-- jQuery -->
    <script src="inc/assets/js/jquery.js" type="text/javascript"></script>
    <script src="inc/assets/js/jquery.maskedinput.js" type="text/javascript"></script>


</head>

  <body>

      <!-- Main -->

      <div id="login-page">
        <div class="container">
              <form class="form-login" action="" method="post">
            <?php
            if(isset($_POST['logBtn'])) {
            $licensekey = $_POST['licensekey'];
            //Is it empty?
            if (empty($licensekey) || strlen($licensekey) < 13) {
            die(error('Please fill in all fields.'));
            }

            //Check key
            $SQLCheckLogin = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `licensekey` = :licensekey");
            $SQLCheckLogin -> execute(array(':licensekey' => $licensekey));
            $countLogin = $SQLCheckLogin -> fetchColumn(0);
            if (!($countLogin == 1)) {
            die(error('License key is invalid.'));
            }

            //Check if the key is banned
            $SQL = $odb -> prepare("SELECT `status` FROM `users` WHERE `username` = :username");
            $SQL -> execute(array(':username' => $username));
            $status = $SQL -> fetchColumn(0);
            if ($status == 1) {
            die(error('You are banned'));
            }

            //Success, log in
            $SQL = $odb -> prepare("SELECT * FROM `users` WHERE `licensekey` = :licensekey");
            $SQL -> execute(array(':licensekey' => $licensekey));
            $keyInfo = $SQL -> fetch();
            $_SESSION['licensekey'] = $keyInfo['licensekey'];
            $_SESSION['ID'] = $keyInfo['ID'];
            header("Location: index");
            }

            //Start forgot license

            if (isset($_POST['doBtn'])) {
            $SQLGetInfo = $odb -> prepare("SELECT * FROM `users` WHERE `email` = :email LIMIT 1");
            $n = 0;
            $SQLGetInfo -> execute(array(':email' => $_POST['email']));
            while($row = $SQLGetInfo ->fetch())
            {
              $showLicense = $row['licensekey'];
              $showEmail = $row['email'];
              $currentIP = $_SERVER['REMOTE_ADDR'];
              $showID = $row['ID'];
              //Send Email
              $to      = $showEmail;
              $subject = 'License Key';
              $message = 'Hello, you have requested a license key reset.<br>Requested IP: '.htmlspecialchars($currentIP).'<br>Your License Key: '.htmlspecialchars($showLicense).'';
              $headers = 'From: admin@example.com' . "\r\n" .'Reply-To: admin@example.com' . "\r\n" .'X-Mailer: PHP/' . phpversion();
              mail($to, $subject, $message, $headers);
              echo '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>SUCCESS:</strong> Email has been sent!</div>';
              ++$n;
            }
            if(0 == $n)
            {
             echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>ERROR:</strong> Email is invalid</div>';
            }
          }
            ?>
                <h2 class="form-login-heading">PANEL LOGIN</h2>
            <form method="post">
                <div class="login-wrap">
                    <input id="key" class="form-control" name="licensekey" type="text" placeholder="XXXX-XXXX-XXXX" /><script>jQuery(function($){$("#key").mask("****-****-****",{placeholder:"XXXX-XXXX-XXXX"});});</script>
                    <label class="checkbox">
                        <span class="pull-right">
                            <a data-toggle="modal" href="#forgotKey"> Forgot your key?</a>

                        </span>
                    </label>
                    <button class="btn btn-theme btn-block" name="logBtn" type="submit"><i class="fa fa-lock">&nbsp;</i> LOGIN</button>
                    <hr>

                    <div class="registration">
                        Don't have a key yet?<br/>
                        <a class="" href="#">
                            Purchase a key
                        </a>
                    </div>

                </div> </form>
                  <div aria-hidden="true" aria-labelledby="forgotKey" role="dialog" tabindex="-1" id="forgotKey" class="modal fade">
                      <div class="modal-dialog">
                          <div class="modal-content">
                              <div class="modal-header">
                                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                  <h4 class="modal-title">Forgot your key ?</h4>
                              </div>
                          <form method="post">
                              <div class="modal-body">
                                  <p>Enter your e-mail address below to receive your key - This process is instant.</p>
                                  <input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">
                              </div>
                              <div class="modal-footer">
                                  <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                                  <button class="btn btn-theme" name="doBtn" type="submit">Submit</button>
                              </div>
                         </form>
                          </div>
                      </div>
                  </div>
              </form>

        </div>
      </div>

    <script src="inc/assets/js/jquery.js"></script>
    <script src="inc/assets/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="inc/assets/js/jquery.backstretch.min.js"></script>
    <script>
        $.backstretch("inc/assets/img/gta-background-blur.jpg", {speed: 500});
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您的陈述有die()。首先删除它。

die()导致仅显示错误消息,其余部分正在消亡,

替换

die(error('Please fill in all fields.'));

通过

echo error('Please fill in all fields.');