从另一个外部php脚本运行外部php脚本
问题描述:
我有我的主要索引页,它使用ajax请求来处理用户登录数据的外部php文件。在外部PHP文件我还包括其它外部PHP文件处理所有的功能,但外部登录文件无法使用任何功能从另一个外部php脚本运行外部php脚本
这里是从的index.php
$('#ind_login_submit').on('click', function (e) {
var vars = $("#ind_login_form").serialize(); // the script where you handle the form input.
//alert("gu");
var hr = new XMLHttpRequest();
hr.open("POST", "scripts/index/ind_login_submit.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
for(var obj in data){
if(obj == "error"){
alert(data[obj]);
}else if(obj == "success"){
alert(data[obj]);
window.location.replace("http://localhost/site/dashboard.php");
}
}
//alert(hr.responseText);
//location.load();
}
};
hr.send(vars);
//results.innerHTML = "requesting...";
event.preventDefault();
});
Ajax调用
这里是外部ind_login_submit.php
header("Content-Type: application/json");
session_start();
include '../../connect.php';
include '../functions.php';
$secret_key = '';
globalSecret($secret_key);
$error_array = array('error' => $secret_key);
$jsonData = json_encode($error_array);
echo $jsonData;
exit;
if(isset($_POST['ind_login_remember'])){
$ind_login_remember=1;
}else{
$ind_login_remember=0;
}
$ind_login_email = $_POST['ind_login_email'];
$ind_login_password = $_POST['ind_login_password'];
这里是functions.php的
function globalSecret(){
$secret = "This is the secret";
$secret_key = sha1($secret);
}
当我运行代码,我只是得到了一个空白警报显示,它应该显示$ SECRET_KEY变量
答
我觉得行
globalSecret($secret_key);
应该
$secret_key = globalSecret();
和功能globalSecret()应如下所示:
function globalSecret(){
$secret = "This is the secret";
$secret_key = sha1($secret);
return $secret_key;
}
跟踪您的ne中的请求twork日志。 PHP的回应是什么?暂时忘记警报 - 这是您的第一个调试端口。 – Utkanos
你不用在函数中使用$ secret_key做任何事情,你是否忘记了'return'或者'echo' – 2014-03-05 22:37:29
就像@Dagon建议你需要返回'$ secret_key'。该功能似乎是一个“空白”功能。 –