我是网络开发的新手。尝试创建一个登录页面,该页面处理用户信息并在同一页面上抛出错误,而不是重定向到error.php。页面刷新但未提交任何消息。这是代码。
<?php
//check if form has been submitted
if(isset($_POST['submit'])) {
//check if the variables are null
if (empty($user) || empty($password) || empty($domain)) {
$msg = "<p>Username, Password and Domain cannot be blank</p>";
} else {
//sanitize the data
$user = htmlspecialchars (stripslashes (trim ($_POST['user'])));
$password = htmlspecialchars (stripslashes (trim ($_POST['password'])));
$domain = htmlspecialchars (stripslashes (trim ($_POST['domain'])));
//access group variables
$sausers = "user accounts not member of below groups which need admin access";
$sagroup = "user group which needs admin access";
$agroup = "user group which needs user access";
//$accesslevel = 0;
//assign LDAP variables based on user domain
if ($domain == "domain"){
$ldap_host = "childdomain.domain.com";
$ldap_user = $user."@childdomain.domain.com";
$ldap_dn = "DC=childdomain,DC=domain,DC=com";
}
//Connect to LDAP Directory
$ldap = ldap_connect($ldap_host);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);
If ($bind = @ldap_bind($ldap,$ldap_user,$password)) {
//authentication against AD successful
$filter = "(sAMAccountName=".$user.")";
$attr = array("memberof");
if ($result = ldap_search($ldap, $ldap_dn, $filter, $attr)) {
$groups = ldap_get_entries($ldap, $result);
ldap_unbind($ldap);
//check if user is listed in $sausers
If(strops($sausers, $user) != false) {
//access granted - ideally redirect to admin page
$msg = "<p>Welcome Super Admin</p>";
}
//check if user is member of admin team
foreach($groups[0]['memberof'] as $group) {
// check if user is a member of admin team
if(strpos($group, $sagroup) != false) {
//access granted - ideally redirect to admin page
$msg = "<p>Welcome Super Admin</p>";
} elseif (strpos($group, $agroup) != false) {
//access granted - ideally redirect to admin page
$msg = "<p>Welcome user</p>";
}
}
} else {
//Unable to search LDAP server
ldap_unbind($ldap);
$msg = "<p>We are facing issues with LDAP search at this time. Please report this issue to us by emailing emailaddress</p>";
}
} else {
//AD authentication failed
$msg = "<p>Domain Authentication Failed!!! Please try again.</p>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Admin Login</title>
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="logincontainer">
<div class="login">
<h1>RCP Ops Admin Console Login</h1>
<form action="<?php htmlentities(urldecode($_SERVER['PHP_SELF']));?>" method="post">
<p><b></b><input type="text" name="user" value="" placeholder="user" required autocomplete="off"></p>
<p><b> </b><input type="password" name="password" value="" placeholder="Password" required autocomplete="off"></p>
<p><select name="domain" placeholder="Domain">
<option value="Select DOMAIN">DOMAIN</option>
<option value="NANET">childdomain1</option>
<option value="EUNET">childdomain2</option>
<option value="APNET">childdomain3</option>
<option value="JPNET">childdomain4</option>
</select>
</p>
<p class="submit"><input type="submit" name="submit" value="Login"></p>
</form>
</div>
</div>
<div class="error">
<?print "<p>.$msg.</p>";?>
</div>
页面将刷新并重置表单,但不会显示任何消息。
答案 0 :(得分:0)
你忘了在这里重新打开php标签:
<div class="error">
<?print "<p>.$msg.</p>";?>
</div>
应该是:
<div class="error">
<?php print "<p>.$msg.</p>";?>
</div>