PHP 登录会话错误.即使可信度正确,也不会输入目标年龄


PHP login sessions errors.not entering the destination age even if the crediantials are correct

即使登录成功,我也无法从登录页面移动,即使凭据正确,同一页面仍保持打开状态,但转到索引页面。

下面的代码中,如果凭据正确,它应该定向到索引页,但页面保留在登录页中。但是,如果我在 wamp 中托管相同的代码,它可以工作,但是当涉及到 linux 时,我面临着这个问题。

<head>
<meta charset="utf-8">
<title>login</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
</head>
<body>
<div class="container">
<a href="view.php">
<img src="logo.png" style="vertical-align:bottom; padding: 2px;">
</a>
<section id="content">
<?php
session_start();
if(isset($_SESSION['url'])) 
$url = $_SESSION['url']; // holds url for last page visited.
else 
$url = "index.php";  //home page 
// include mylib.php (contains Logging class)
include('mylib.php');
// Logging class initialization
$log = new Logging();
// set path and name of log file (optional)
$log->lfile('user_logins.log');
if(isset($_POST['username']) && isset($_POST['password'])){
$adServer = "ldap://ldap.com";
$ldap = ldap_connect($adServer);
$username = $_POST['username']; 
$password = $_POST['password'];
#$_SESSION['user'] = $username;
$ldaprdn = 'm' . "''" . $username;
#$ldaprdn = $username;
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = @ldap_bind($ldap, $ldaprdn, $password);
$real_name="";
$groupToAllow = "CN=***,OU=***DC=test,DC=com";
$baseDn="CN=***,OU=***DC=test,DC=com";
$_SESSION['admin']= false;
if ($bind) {
$log->lwrite("$username authenticated sucessfully..");
$filter="(sAMAccountName=$username)";
$result = ldap_search($ldap,$baseDn,$filter);
ldap_sort($ldap,$result,"sn");
$info = ldap_get_entries($ldap, $result);
$authorized= fale;
for ($i=0; $i<$info[0]["memberof"]["count"]; $i++) 
{                   
if ( strtoupper($info[0]["memberof"][$i]) == strtoupper($groupToAllow))
{   
$log->lwrite("$username belongs to the group $groupToAllow");               
$log->lwrite("$username is an authorised user");                
$_SESSION['user']=$info[0]["givenname"][0];
$_SESSION['email'] = $info[0]["userprincipalname"][0];
$_SESSION['admin'] = true;
$authorized= true;
break;
}
}
if ($authorized === true) {
$userDn = $info[0]["distinguishedname"][0];                         
// write message to the log file
$log->lwrite("$userDn logged in sucessfully..");
header("Location: $url");
} else {
$log->lwrite("Error: $username doesn't belongs to the group "); 
$log->lwrite("Error: $username is not authorised to login");
header("Location: Not_authorised.php");
}
@ldap_close($ldap);
} else {
$log->lwrite($_POST['username'] ." tried to login, but failed..");
header("Location: loginerror.php");
}
}else{
?>
<form action="login.php" method="POST">
<h1>Login Form</h1>
<div>
<input type="text" placeholder="Username" required="" id="username"  name="username"/>
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input type="submit" name="submit" value="Submit" />
</div>
</form><!-- form -->
<?php } ?> 
</section><!-- content -->
</div><!-- container -->
</body>
</html>

提前致谢

当你打印了一些东西时,你被阻止了下一个标题函数。

您应该在打印 html 之前执行所有检查。

请记住,PHP是一种脚本语言,从文档的顶部到底部运行。当您在html中打印某些内容然后打开php标签时,文档标题是不同的,您无法进行URL重定向。

<html>
<head>
  .... --> this not working .
</head>
<body>
<?php 
     if(!empty($_SESSION['login'])&&){
       //all your code implementation
       //now you can call your template file
       include 'account.php';
     }else{
       //return to index or the login page
       header('location:index.php');
     }
?>
</body>
</html>

使用PHP检查解决方案的所有步骤。

<?php 
     if(!empty($_SESSION['login'])&&){
       //all your code implementation
       //now you can call your template file
       //include 'account.php';
       //OR print html
     ?>
        <html>
           <head>
           </head>
           <body>
                Hi user : <?=$user?>
           </body>
        </html>
     <?php
     }else{
       //return to index or the login page
       header('location:index.php');
     }
?>

另外,我建议你应该分开你的PHP代码HTML代码。一个好的工具可以是 聪明 .

Whit Smarty您可以检查所有您想要的内容并为模板声明变量。

还要做控制模板的循环或结构。

我希望我有所帮助祝你好运!!