【问题标题】:Running external php script from another external php script从另一个外部 php 脚本运行外部 php 脚本
【发布时间】:2014-03-05 22:32:19
【问题描述】:

我有我的主索引页面,它使用对处理用户登录数据的外部 php 文件的 ajax 请求。在那个外部 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 变量

【问题讨论】:

  • 在您的网络日志中跟踪请求。 PHP 的响应是什么?暂时忘记警报 - 这是您的第一个调试端口。
  • 你没有在函数中使用 $secret_key 做任何事情,你是否忘记了return 或者echo
  • 像@Dagon 建议你需要返回你的$secret_key。该函数似乎是void 函数。
  • 你也将一个变量解析为你不使用的函数,在开始之前先了解一下基础知识。
  • 您还使用错误数量的参数调用函数。它不需要任何参数,但您传递的是$secret_key

标签: javascript php ajax json


【解决方案1】:

我觉得行

globalSecret($secret_key);

应该是

$secret_key = globalSecret();

函数globalSecret()应该如下:

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

【讨论】:

  • 他需要从函数中返回一些东西。
  • 不会做任何事情,因为globalSecret 什么都不返回
  • 好的,现在完美,我完全忘记了返回它,只是认为它会神奇地将变量发送回来
  • 我相信 perl 会自动返回最后计算的值,但在 php 中你必须明确地这样做
猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 2015-10-01
  • 2018-08-14
  • 2021-06-21
相关资源
最近更新 更多