如何在2个php文件之间传递变量

我有两个php文件对活动目录用户进行身份验证,我想获取属性 url < / code>来自它并将此变量 $ data </ code>从 authenticate.php </ code>传递给 login.php </ code>,如果函数返回true则位于该位置 header(“Location:* URL *”); </ code>,如何做到这一点?</ p>

authenticate.php:</ p>

< pre> &lt;?php
//初始化会话
session_start();

函数验证($ user,$ password){
if(empty($ user)|| empty($ password) ))返回false;

// Active Directory服务器
$ ldap_host =“CRAMSDCR01V.cloud4rain.local”;

//连接到活动目录
$ ldap = ldap_connect($ ldap_host);

$ ldap_dn =“OU = by-style,DC = cloud4rain,DC = local”;

//验证用户和密码
if($ bind = @ldap_bind($ ldap,$ user,$ password) )
{
$ result = ldap_search($ ldap,$ ldap_dn,“(cn = *)”)或die(“搜索查询中的错误:”。oldap_error($ ldap));
$ data = ldap_get_entries($ ldap,$ result);
echo $ data [“url”];
返回true;

}
其他
{
//名称或密码无效
返回false;
}
}
?&gt;
</ code> </ pre>

login.php:</ p>

 &lt;?php 
include(“authenticate.php”);

//检查用户是否正在退出
if(isset) ($ _GET ['out'])){
// destroy session
session_unset();
$ _SESSION = array();
unset($ _ SESSION ['user'],$ _ SESSION ['access']) ;
session_destroy();
}

//检查登录表单是否已提交
if(isset($ _ POST ['btn-login'])){
//通过身份验证器运行信息\ nif(authenticate($ _ POST ['userLogin'],$ _ POST ['userPassword']))
{
//身份验证传递
标题(“位置:authenticate.php?$ data”);
die( );
} else {
//身份验证失败
$ error =“登录失败:用户名,密码或权限不正确&lt; br / - &gt;”;
}
}

/ n / output logout success
if(isset($ _ GET ['out']))echo“Logout successful”;
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

I have two php files to make authentication to active directory users, i want to get the attribute url from it and pass this variable $data from authenticate.php to login.php if the function returned true to be in the location of header("Location: *URL*");,how can this be done?

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";
?>

dseomy1964
dseomy1964 您在login.php中包含authenticate.php,因此数据已经存在-您的问题是变量作用域,$data是在函数内定义的。只需要该函数返回$data;并在login.php中使用if($authData=authenticate(...){...}之类的东西:stackoverflow.com/questions/16959576/...
3 年多之前 回复
douchan6512
douchan6512 例如$data=ldap_get_entries($ldap,$result);$_SESSION['url']=$data[“url”];返回true;
3 年多之前 回复
douba3943
douba3943 设置为会话并传递它。
3 年多之前 回复

2个回答



login.php </ strong> </ p>

 &lt;?php

include(“authenticate.php”);
</ code> </ pre>

这实际上就像粘贴登录中 authenticate.php </ em>的内容一样 .php </ em>所以虽然它在技术上是2个文件,但它就好像它只是一个 - 但 $ data </ code>是在 authenticate()</ code>函数中定义的,所以是 只有在该职能范围内</ a >。</ p>



authenticate.php </ strong> - 从函数中返回数据</ p>

  /  /验证用户和密码
if($ bind = @ldap_bind($ ldap,$ user,$ password))
{
$ result = ldap_search($ ldap,$ ldap_dn,“(cn = *)”)或死 (“搜索查询中的错误:”。oldap_error($ ldap));
$ data = ldap_get_entries($ ldap,$ result);
// echo $ data [“url”]; //我假设这只是用于调试...

//从函数返回$ data,该函数应该是“truthy”
返回$ data;
}
else
{
//名称无效 或密码
返回false;
}
</ code> </ pre>



login.php </ strong> - 评估<代码的返回 > authenticate()</ code>函数 - 因为PHP是松散类型的,函数返回的任何(非空)字符串都可以被评估为“truthy”</ em> - 你从中获得的唯一其他返回 函数是 false </ code>所以...... </ p>

  //通过authenticator 
运行信息
if($ authData = authenticate($ _ POST ['userLogin'], $ _POST ['userPassword']))
{
//身份验证传递
//为了清晰起见重命名变量$ authData
header(“Location:authenticate.php?$ authData”);

die();
}

其他{
//身份验证失败
$ error =“登录失败:用户名,密码或权限不正确&lt; br /&gt;”;
}
</ code> </ pre>
</ div>

展开原文

原文

login.php

<?php
include("authenticate.php");

That essentially acts like pasting the contents of authenticate.php inside login.php so although it's technically 2 files, it acts as if it's just the one - however $data is defined within the authenticate() function and so is only scoped within that function.


In authenticate.php - return the data from the function

// 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;
}


In login.php - evaluate the return from the authenticate() function - since PHP is loosely typed any (non-empty) string returned by the function can be evaluated as being "truthy" - the only other returns you have from the function are false so...

// 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 />";
}

doujiyong7604
doujiyong7604 我真的没有做Active Directory抱歉 - 但ldap_search()有一个第四个参数可以用来过滤你想要的属性:php.net/manual/en/function.ldap-search.php - 可能是你的 '之后......
3 年多之前 回复
douzhechi2435
douzhechi2435 它有用,你能告诉我如何从活动目录中获取特定属性吗?
3 年多之前 回复



不确定为什么在login.php中有 $ _ SESSION = array(); </ code>但是如果你想 将$ data从一个php传递到另一个php,然后将其设置为会话</ p>

  $ _ SESSION ['data'] = $ data; 
</ code> </ pre>

在另一个文件中使用</ p>

  $ data = $ _SESSION ['data']; 
</ code> </ pre >
</ div>

展开原文

原文

Not sure why you have $_SESSION = array(); in login.php but if you want to pass $data from one php to another then just set it in session as

$_SESSION['data'] = $data;

ang to get it in the other file use

$data = $_SESSION['data'];

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐