2014-03-05 195 views
0

我有我的主要索引页,它使用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变量

+1

跟踪您的ne中的请求twork日志。 PHP的回应是什么?暂时忘记警报 - 这是您的第一个调试端口。 – Utkanos

+1

你不用在函数中使用$ secret_key做任何事情,你是否忘记了'return'或者'echo' – 2014-03-05 22:37:29

+0

就像@Dagon建议你需要返回'$ secret_key'。该功能似乎是一个“空白”功能。 –

回答

0

我觉得行

globalSecret($secret_key); 

应该

$secret_key = globalSecret(); 

和功能globalSecret()应如下所示:

function globalSecret(){ 
    $secret = "This is the secret"; 
    $secret_key = sha1($secret); 
    return $secret_key; 
} 
+0

他需要从函数中返回一些东西。 – Barmar

+0

不会做任何事情,因为'globalSecret'不会返回任何东西 – 2014-03-05 22:41:06

+0

好吧,现在完美的工作,我完全忘了返回它,只是认为它会奇迹般地发回变量 – Arken