从另一个外部PHP脚本运行外部PHP脚本

时间:2014-03-05 22:32:19

标签: javascript php ajax json

我的主索引页面使用ajax请求来处理用户登录数据的外部php文件。在那个外部php文件中我还包含另一个处理所有功能的外部php文件,但外部登录文件无法使用任何功能

这是来自index.php的ajax调用

$('#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();
});

这是外部的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变量

1 个答案:

答案 0 :(得分:0)

我认为该行

globalSecret($secret_key);

应该是

$secret_key = globalSecret();

并且函数globalSecret()应如下所示:

function globalSecret(){
    $secret = "This is the secret";
    $secret_key = sha1($secret);
    return $secret_key;
}
相关问题