使用会话在PHP中显示的不同登录错误

问题描述 投票:0回答:1

PHP的新手,我试图在登录页面上显示不同的错误消息。如果用户输入无效的密码,它将显示“无效的用户NamePassword"。现在的问题是我的系统已连接到与活动目录连接的Web api。现在我想做的是该用户是否存在在目录中,但用户无权登录系统,然后显示一条消息"You're not allowed to login"。如果服务器的链接已关闭,我也要显示"Server Down"

require('connection.php');
  session_start();
  $error = "Invalid User Name or Password";
  $URLerror = "Service down. Please try again later!";
  $noaccess = "You're not allowed to login";

  if (isset($_POST['submit'])) {
    $username = stripslashes($_REQUEST['username']);
    $password = stripslashes($_REQUEST['password']);
    // $domain = stripslashes($_REQUEST['domain']);

    $url = "http://localhost:51495/Api/Values";

    $data = array(
        'user' => $username,
        'pass' => $password,
        // 'domain' => $domain
    );

    $data_string = json_encode($data);
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt(
        $ch,
        CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        )
    );

    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result, true);
    print_r($result);
    if ($result == false) {
      $_SESSION["username"] = $noaccess;
      header("location: login.php");

    } else if ($result == true){
      $_SESSION['username'] = $username;
      header("Location: index.php");
    }
    else {
      $_SESSION["error"] = $error;
      header("location: login.php");
    }
  } else {
    ?>

<div class="container pt-4">

      <!-- Outer Row -->
      <div class="row justify-content-center pt-5">

        <div class="col-xl-8 col-lg-10 col-md-7 mt-5">

          <div class="card o-hidden border-0 shadow-lg my-5">
            <div class="card-body p-5">
              <!-- Nested Row within Card Body  -->
              <div class="row">
                <div class="col-lg-6 d-none d-lg-block logo"><img data-tilt class="img-fluid px-5 mt-3 " src="img/netsol.png"> </div>
                <div class="col-lg-6">
                  <div class="p-3 py-5 col-md-12 pr-5">
                    <div class="text-center">
                      <h1 class="h6 text-gray-900 mb-3 font-weight-bold">Welcome to Feedback System!</h1>
                    </div>
                    <form class="user" action="" method="post" name="login">
                      <div class="form-group">
                        <input type="text" class="form-control form-control-user" name="username" placeholder="Username" required />
                      </div>
                      <div class="form-group">
                        <input type="password" class="form-control form-control-user" name="password" placeholder="Password" required />
                      </div>
                      <div class="text-danger text-center mb-2">
                        <?php
                          if (isset($_SESSION["username"])) {
                            $noaccess = $_SESSION["username"];
                            echo "<span>$noaccess</span>";
                            unset($_SESSION["username"]);
                          }
                          else if (isset($_SESSION["error"])) {
                            $error = $_SESSION["error"];
                            echo "<span>$error</span>";
                            unset($_SESSION["error"]);
                          }
                          ?>
                      </div>
                      <input name="submit" class="btn btn-primary btn-user btn-block" type="submit" value="Login" />
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

我是PHP的新手,正在尝试在登录页面上显示不同的错误消息。如果用户输入无效的密码,则显示“无效的用户名或密码”。现在的问题是我的系统已连接...

php php-curl
1个回答
0
投票

有两件事:

© www.soinside.com 2019 - 2024. All rights reserved.