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

PHP和Javascript:使用Javascript变量获取文件内容,javascript,php,html,variables,Javascript,Php,Html,Variables,有人能帮我理解为什么这不起作用吗 var uname = "<?php echo strtolower($_GET['un']) ?>"; var source = "<?php echo file_get_contents('accounts/"+uname+"') ?>"; console.log(source); var uname=”“; var source=“试试这个: <?php $uname = strtolower($_GET['un']); ?

有人能帮我理解为什么这不起作用吗

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

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

var source=“”;
试试这个:

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

var source=“”;
试试这个:

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

var source=“”;
试试这个:

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

var source=“”;

将uname设置为javascript变量,然后尝试使用php读取它

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

我必须说,无论如何,这对于公共使用来说是不安全的。

您将uname设置为javascript变量,然后尝试使用php读取它

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

我必须说,无论如何,这对于公共使用来说是不安全的。

您将uname设置为javascript变量,然后尝试使用php读取它

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

我必须说,无论如何,这对于公共使用来说是不安全的。

您将uname设置为javascript变量,然后尝试使用php读取它

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

我必须说,无论如何,这对于公共使用来说是不安全的。

您混合了客户端和服务器端技术…+uname+看起来像是PHP命令中的JavaScript变量…JavaScript变量在呈现页面之前不可用

试试看

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

您正在混合客户端和服务器端技术…+uname+看起来像是PHP命令中的JavaScript变量…在呈现页面之前,JavaScript变量不可用

试试看

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

您正在混合客户端和服务器端技术…+uname+看起来像是PHP命令中的JavaScript变量…在呈现页面之前,JavaScript变量不可用

试试看

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

您正在混合客户端和服务器端技术…+uname+看起来像是PHP命令中的JavaScript变量…在呈现页面之前,JavaScript变量不可用

试试看

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

我只想指出,您不需要PHP来访问查询字符串中的变量,正如一些人已经提到的,您在这里混合了各种技术。还要注意,您的问题示例存在安全问题,我非常确定这些问题将持续存在,因为您需要清理您调用的脚本中的输入

您的完整示例可以用javascript完成。我将使用jQuery进行ajax调用(您可以省略它ofc,然后自己处理ajax调用-我只是在偷懒;-))和查询字符串检索

// 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();
//使用查询字符串(window.location.search)填充变量-由MDN提供
var oGetVars=new(函数(sSearch){
var rNull=/^\s*$/,rBool=/^(真|假)$/i;
函数构建值(sValue){
if(rNull.test(sValue)){return null;}
if(rBool.test(sValue)){返回sValue.toLowerCase()==“true”;}
if(isFinite(sValue)){return parseFloat(sValue);}
if(isFinite(Date.parse(sValue)){返回新日期(sValue);}
返回值;
}
如果(搜索长度>1){
对于(var aItKey,nKeyId=0,acooples=sSearch.substr(1)。拆分(“&”);nKeyId1?构建值(unescape(aItKey[1]):null;
}
}
})(窗口、位置、搜索);
//使用jQuery
$.ajax({
url:“accounts/”+oGetVars.un,
成功:函数(数据、文本状态、jqXHR){
控制台日志(数据);
}
});
//纯javascript
var xhr=new XMLHttpRequest();
xhr.open(“GET”、“accounts/”+oGetVars.un,true);
xhr.onreadystatechange=函数(){
如果(http.readyState==4&&http.status==200){
console.log(this.responseText);
}
};
xhr.send();

[编辑]用一个简单的javascript XMLHttpRequest调用更新示例。

我只想指出,您不需要PHP来访问查询字符串中的变量,正如一些人已经提到的,您在这里混合了各种技术。还要注意,您的问题示例存在安全问题,我非常确定这些问题将在您需要清理时持续存在您调用的脚本中的输入

您的完整示例可以用javascript完成。我将使用jQuery进行ajax调用(您可以省略它ofc,然后自己处理ajax调用-我只是在偷懒;-))和查询字符串检索

// 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();
//使用查询字符串(window.location.search)填充变量-由MDN提供
var oGetVars=new(函数(sSearch){
var rNull=/^\s*$/,rBool=/^(真|假)$/i;
函数构建值(sValue){
if(rNull.test(sValue)){return null;}
if(rBool.test(sValue)){返回sValue.toLowerCase()==“true”;}
if(isFinite(sValue)){return parseFloat(sValue);}
if(isFinite(Date.parse(sValue)){返回新日期(sValue);}
返回值;
}
如果(搜索长度>1){
对于(var aItKey,nKeyId=0,acooples=sSearch.substr(1)。拆分(“&”);nKeyId1?构建值(unescape(aItKey[1]):null;
}
}
})(窗口、位置、搜索);
//使用jQuery
$.ajax({
url:“accounts/”+oGetVars.un,
成功:函数(数据、文本状态、jqXHR){
控制台日志(数据);
}
});
//纯javascript
var xhr=new XMLHttpRequest();
xhr.open(“GET”、“accounts/”+oGetVars.un,true);
xhr.onreadystatechange=函数(){
如果(http.readyState==4&&http.status==200){
console.log(this.responseText);
}
};
xhr.send();

[编辑]使用一个简单的javascript XMLHttpRequest调用更新示例。

我只想指出,您不需要PHP来访问查询字符串中的变量,正如前面提到的,您在这里混合了各种技术。另外请注意,您的问题示例具有安全性