dongzhong1891 2018-08-10 15:06
浏览 118

为什么$ GLOBALS ['name']在其他php文件中有所不同

So what I am doing is I set a variable $GLOBALS['me] in File1.php, and in File2.php I try to get and set a new variable with that data, but it doesn't work, when I pass it it then becomes null,... but if you check if it is set, it says it's set. I am calling File2.php with an ajax XMLHttpRequest which I feel might be the cause of this.

---File1.php---

$raceid = 2;
$wpm = 1;
$char = 3;
$me = 4;
...
$GLOBALS['me'] = $me;
$content = "char: " . $char . "
" . "wpm: " . $wpm . "
" . "me: " . $GLOBALS['me'] . "
" . "raceId: " . $raceid . "
";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
//End of php File1.php

As you can see, it print the results to a file, so I can see what $GLOBALS['me'] is. So here is myText.txt after this get's run.

---myText.txt---

char: 3
wpm: 1
me: 4
raceId: 2

So this is great $GLOBALS['me'] equals 4 (Only in this file it equals 4) So here's PHP File2.php(it get's run when javascript function is executed)

---File2.php---

//wpm char and race id are defined above in this file.
if (isset($_POST['char']) && isset($_POST['wpm'])){
    $char = $_POST['char'];
    $wpm = $_POST['wpm'];
    if(isset($GLOBALS['raceid'])){
        $raceid = $GLOBALS['raceid'];
    }
    if (isset($GLOBALS['me'])){
        $me = $GLOBALS['me'];
        $content = "char: ".$char."
"."wpm: ".$wpm."
"."me: ".$GLOBALS['me']."
"."raceId: ".$raceid."
";
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
        fwrite($fp,$content);
        fclose($fp);
    }
//Other stuff that only executes if $me is a value
}

As you can see in this file if $GLOBALS['me'] is set then I set this files $me equal to it. Then I again set the data in the file, but this time the file looks like this.

---myText.txt---

char: 3
wpm: 1
me: 
raceId: 2

now me is blank(null) and I'm still using $GLOBALS???
now here is my javascript to call File2.php, via the post method, and executed via an html button click.

---Javascript---

function ClickMe(){
    $.post("update.php", {
        char: 72,
        wpm: 123
    }, function(data, status){
        alert("done!");
    });
}

Any help on why $GLOBALS['me'] is not keeping it's value, between these php files, would be helpful, and how you would be able to get the $me variable from php File1 to php File2. Their is nothing else called between these php files except the script that posts to File2, thanks for the help. Sorry for the long question wanted to be as descriptive as possible and show that in File1 what $GLOBALS['me'] is set and in File2 it is empty or null. Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿