我有两个php文件对活动目录用户进行身份验证,我希望从中获取属性url
并将此变量$data
从authenticate.php
传递给login.php
如果函数返回true在header("Location: *URL*");
的位置,怎么办呢?
authenticate.php:
<?php
// Initialize session
session_start();
function authenticate($user, $password) {
if(empty($user) || empty($password)) return false;
// Active Directory server
$ldap_host = "CRAMSDCR01V.cloud4rain.local";
// connect to active directory
$ldap = ldap_connect($ldap_host);
$ldap_dn="OU=by-style,DC=cloud4rain,DC=local";
// verify user and password
if($bind = @ldap_bind($ldap, $user, $password))
{
$result = ldap_search($ldap,$ldap_dn, "(cn=*)") or die ("Error in search query: ".ldap_error($ldap));
$data = ldap_get_entries($ldap, $result);
echo $data["url"];
return true;
}
else
{
// invalid name or password
return false;
}
}
?>
的login.php:
<?php
include("authenticate.php");
// check to see if user is logging out
if(isset($_GET['out'])) {
// destroy session
session_unset();
$_SESSION = array();
unset($_SESSION['user'],$_SESSION['access']);
session_destroy();
}
// check to see if login form has been submitted
if(isset($_POST['btn-login'])){
// run information through authenticator
if(authenticate($_POST['userLogin'],$_POST['userPassword']))
{
// authentication passed
header("Location: authenticate.php?$data");
die();
} else {
// authentication failed
$error = "Login failed: Incorrect user name, password, or rights<br /-->";
}
}
// output logout success
if(isset($_GET['out'])) echo "Logout successful";
?>
答案 0 :(得分:1)
<强>的login.php 强>
<?php
include("authenticate.php");
这实际上就像粘贴 login.php 中的 authenticate.php 的内容一样,所以虽然它在技术上是2个文件,但它就好像它是&#39 ;只是一个 - 但$data
在authenticate()
函数中定义,因此只有scoped within that function。
在 authenticate.php 中 - 从函数
// verify user and password
if($bind = @ldap_bind($ldap, $user, $password))
{
$result = ldap_search($ldap,$ldap_dn, "(cn=*)") or die ("Error in search query: ".ldap_error($ldap));
$data = ldap_get_entries($ldap, $result);
// echo $data["url"]; // I assume this is just for debugging...
// return $data from the function which should be "truthy"
return $data;
}
else
{
// invalid name or password
return false;
}
在 login.php 中 - 评估authenticate()
函数的返回值 - 因为PHP是松散类型的,函数返回的任何(非空)字符串都可以被评估为&# 34; truthy&#34; - 你从函数中获得的唯一其他回报是false
所以......
// run information through authenticator
if($authData = authenticate($_POST['userLogin'],$_POST['userPassword']))
{
// authentication passed
// renamed the variable $authData just for clarity
header("Location: authenticate.php?$authData");
die();
}
else {
// authentication failed
$error = "Login failed: Incorrect user name, password, or rights<br />";
}
答案 1 :(得分:0)
不确定为什么你在login.php中有$_SESSION = array();
但如果你想将$ data从一个php传递到另一个php,那么只需将它设置为session
$_SESSION['data'] = $data;
在另一个文件中使用
获取它$data = $_SESSION['data'];