[英]Accessing $_REQUEST from an indirect call to a PHP script
I want to access $_REQUEST within a PHP script that generates a JavaScript file. 我想在生成JavaScript文件的PHP脚本中访问$ _REQUEST。 Here is my barebones index.php page: 这是我的准系统index.php页面:
<html>
<head>
<script src="phpGeneratedScript.js.php"></script>
</head>
<body>
<?php echo json_encode($_REQUEST); ?>
</body>
</html>
And here is my barebones .js.php file: 这是我的准系统.js.php文件:
<?php echo "alert('".json_encode($_REQUEST)."')"; ?>
When I visit index.php?key=value
, I see a query displayed in the page, but not in the alert. 当我访问index.php?key=value
,我看到一个查询显示在页面中,但没有显示在警报中。 Is there a way to access $_REQUEST (or $_GET or $_POST) from within a PHP script that is not directly include
d? 是否可以从不直接include
d的PHP脚本中访问$ _REQUEST(或$ _GET或$ _POST)?
EDIT: To partially answer my own question - this adds the query string to the call to the php file that returns the JavaScript file: 编辑:要部分回答我自己的问题-这会将查询字符串添加到对返回JavaScript文件的php文件的调用中:
<html>
<head>
<?php
$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
echo
"<script src='phpGeneratedScript.js.php?$query'></script>"
?>
</head>
<body>
<?php echo json_encode($_REQUEST); ?>
</body>
</html.
However, this does not handle an POST data which may be sent with the original request. 但是,这不处理可能与原始请求一起发送的POST数据。
Following drew010's suggestions, I tried this: 按照drew010的建议,我尝试了以下方法:
html
<html>
<head>
<?php
$query = http_build_query($_REQUEST);
echo
"<script src='phpGeneratedScript.js.php?$query'></script>"
?>
</head>
<body>
<?php
if (!$_POST) { ?>
<form action="test.php" method="post">
<!--input type="text" name="name" value="value"--><br>
<input type='submit' name="submit" value='Submit'>
</form>
<? } else {
echo json_encode($_REQUEST);
echo "<br />";
echo json_encode($_POST);
}
?>
</body>
js.php
<?php
echo "console.log('".json_encode($_REQUEST)."');";
echo "console.log('".json_encode($_POST)."')";
?>
Here is the output in the browser window, after I click on the Submit button: 单击“提交”按钮后,这是浏览器窗口中的输出:
{"submit":"Submit","PHPSESSID":"b6eb4d1d73fc75976a16031003f58fa0"}
{"submit":"Submit"}
Both $_REQUEST and $_POST are correctly populated. $ _REQUEST和$ _POST都已正确填充。
Here is the output in the console: 这是控制台中的输出:
{"submit":"Submit","PHPSESSID":"b6eb4d1d73fc75976a16031003f58fa0"}
[]
The $_POST data has been added to the $_REQUEST data, but it is not visible in $_POST. $ _POST数据已添加到$ _REQUEST数据中,但是在$ _POST中不可见。 Is this what you would expect, or is there a way to adjust my scripts to show $_POST data correctly? 这是您所期望的,还是可以调整脚本以正确显示$ _POST数据?
Instead of: 代替:
$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
echo "<script src='phpGeneratedScript.js.php?$query'></script>";
Try: 尝试:
$query = http_build_query($_REQUEST);
parse_url()
with PHP_URL_QUERY
will only return GET data from the query string. 使用PHP_URL_QUERY
parse_url()
将仅从查询字符串返回GET数据。
http_build_query()
will create a URL-encoded string from both GET and POST data. http_build_query()
将根据GET和POST数据创建一个URL编码的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.