0

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";
?>
Bnabil
  • 71
  • 3
  • 13
  • set as session and pass it. – dehood Apr 05 '17 at 12:53
  • for example `$data = ldap_get_entries($ldap, $result); $_SESSION['url'] = $data["url"]; return true;` – dehood Apr 05 '17 at 12:54
  • You're including `authenticate.php` inside `login.php` so the data is already there - your issue is with variable scoping, `$data` is defined inside a function. Just have that function `return $data;` and use something like `if($authData = authenticate( ... ) { ... }` in `login.php` : http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – CD001 Apr 05 '17 at 12:55

2 Answers2

1

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 />";
}
Community
  • 1
  • 1
CD001
  • 8,332
  • 3
  • 24
  • 28
  • @CD001..Thanks it worked, can you tell me how to get specific attribute from active directory? – Bnabil Apr 05 '17 at 13:34
  • I don't really do Active Directory sorry - but `ldap_search()` has a 4th parameter you can use to filter just to the attributes you want: http://php.net/manual/en/function.ldap-search.php - might be what you're after... – CD001 Apr 05 '17 at 13:48
0

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'];
BiJ
  • 1,639
  • 5
  • 24
  • 55