简体   繁体   English

PHP和Javascript:使用Javascript变量获取文件内容

[英]PHP and Javascript: Getting file contents with Javascript variable

Could someone help me to understand why this isn't working? 有人可以帮我了解为什么这不起作用吗?

var uname = "<?php echo strtolower($_GET['un']) ?>";
var source = "<?php echo file_get_contents('accounts/"+uname+"') ?>";
console.log(source);

I've been trying for a while to get this working and it just doesn't seem to. 我已经尝试了一段时间以使其正常工作,但似乎没有。 Before I added in the source variable, it was working fine and displayed the un variable on the page. 在添加源变量之前,它工作正常,并在页面上显示了un变量。

Thanks. 谢谢。

You are mixing client side and server side technologies... +uname+ looks like a JavaScript variable within a PHP command... The JavaScript variable isn't available until the page is rendered. 您正在混合使用客户端和服务器端技术... + uname +看起来像PHP命令中的JavaScript变量...在呈现页面之前,JavaScript变量不可用。

Try... 尝试...

var source = "<?php echo file_get_contents('accounts/'. $_GET['un']) ?>";

You set uname as a javascript variable and then you try to read it with php. 您将uname设置为javascript变量,然后尝试使用php读取它。

var source = "<?php echo file_get_contents('accounts/'.strtolower($_GET['un'])) ?>";
console.log(source);

I must say that this is not safe for a public usage in anyway. 我必须说,无论如何这对于公众使用都是不安全的。

I just want to point out that you don't need PHP to access variables in your query string, as some already mentioned, you are mixing technologies here. 我只想指出,您不需要PHP来访问查询字符串中的变量,就像已经提到的那样,您正在这里混合使用各种技术。 Also note that your questions example has security issues and I'm pretty sure that these will persist as you need to sanitize the input in the script you call! 还要注意,您的问题示例存在安全问题,我很确定这些问题将持续存在,因为您需要清理调用脚本中的输入!

Your complete example can be done in javascript. 您的完整示例可以使用javascript完成。 I'll use jQuery for the ajax call (you can omit it ofc and handle the ajax call yourself obviously - I'm just being lazy ;-) ) and an MDN snippte for query string retrieval. 我将使用jQuery进行ajax调用(您可以省略它的ofc并明显地自己处理ajax调用-我只是懒惰;-))和一个用于查询字符串检索的MDN代码段

// populate a variable with your query string (window.location.search) - courtesy of MDN
var oGetVars = new (function (sSearch) {
  var rNull = /^\s*$/, rBool = /^(true|false)$/i;
  function buildValue(sValue) {
    if (rNull.test(sValue)) { return null; }
    if (rBool.test(sValue)) { return sValue.toLowerCase() === "true"; }
    if (isFinite(sValue)) { return parseFloat(sValue); }
    if (isFinite(Date.parse(sValue))) { return new Date(sValue); }
    return sValue;
  }
  if (sSearch.length > 1) {
    for (var aItKey, nKeyId = 0, aCouples = sSearch.substr(1).split("&"); nKeyId < aCouples.length; nKeyId++) {
      aItKey = aCouples[nKeyId].split("=");
      this[unescape(aItKey[0])] = aItKey.length > 1 ? buildValue(unescape(aItKey[1])) : null;
    }
  }
})(window.location.search);

// with jQuery
$.ajax({
    url: "accounts/" + oGetVars.un,
    success: function(data, textStatus, jqXHR) {
        console.log(data);
    }
});

// plain javascript
var xhr = new XMLHttpRequest();
xhr.open("GET", "accounts/" + oGetVars.un, true);
xhr.onreadystatechange = function () {
    if(http.readyState == 4 && http.status == 200) {
        console.log(this.responseText);
    }
};
xhr.send();

[edit] Update the example with a plain javascript XMLHttpRequest call. [edit]使用普通的javascript XMLHttpRequest调用更新示例。

Try this: 尝试这个:

<?php $uname = strtolower($_GET['un']); ?>
var source = "<?php echo file_get_contents('accounts/'.$uname) ?>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM