gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:37:27 25 4
gpt4 key购买 nike

我有两个 php 文件来对事件目录用户进行身份验证,我想从中获取属性 url 并从 authenticate 传递此变量 $data .phplogin.php 如果函数返回 true 位于 header("Location: *URL*"); 的位置,这怎么办完成了吗?

验证.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;
}
}
?>

登录.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";
?>

最佳答案

login.php

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

这本质上就像将 authenticate.php 的内容粘贴到 login.php 中,所以虽然从技术上讲它是 2 个文件,但它的作用就好像它只是一个 - 然而 $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 是松散类型的,因此该函数返回的任何(非空)字符串都可以被评估为“truthy” - 从函数中获得的唯一其他返回是 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 />";
}

关于javascript - 如何在2个php文件之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43231717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com