duanmu5641 2017-04-05 12:49
浏览 282
已采纳

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

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";
?>
  • 写回答

2条回答 默认 最新

  • dongpan1308 2017-04-05 13:09
    关注

    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 />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值