isc*_*red 1 php variables get header hyperlink
我有两个文件,一个名为test3.php,另一个名为test4.php.我试图在文件test4.php的链接中回显变量,但它回应了意想不到的结果.请看一下.
在名为test3.php的文件中:
<?php
$text = "Good morning.";
header('Location:test4.php?text=$text');
?>
Run Code Online (Sandbox Code Playgroud)
在名为test4.php的文件中:
<?php
$text = $_GET['text'];
echo "$text";
?>
Run Code Online (Sandbox Code Playgroud)
预期的回声结果:
"Good morning."
Run Code Online (Sandbox Code Playgroud)
实际回声结果:
$text
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它会回复$ text,而不是"早上好".我想到的一件事是,当你使用标题时,你实际上无法设置变量,所以如果是这种情况请告诉我.谢谢.
变量不会在单引号中解析
header('Location:test4.php?text=$text');
Run Code Online (Sandbox Code Playgroud)
因此,您需要使用双引号
header("Location:test4.php?text=$text");
Run Code Online (Sandbox Code Playgroud)
参考文献:
另外,最好exit;在标题后添加,以便停止进一步执行,如果你有更多的代码(或将来决定).
并http://按照手册使用完整的通话
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Run Code Online (Sandbox Code Playgroud)
脚注,关于标题,并根据手册:
请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header().使用include或require,函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用header()之前输出空格或空行.使用单个PHP/HTML文件时存在同样的问题.
预期的回声结果:
"Good morning."
Run Code Online (Sandbox Code Playgroud)
如果您想回显只有"Good morning."双引号文本,那么您需要在test4.php文件中更改以下内容:
echo "$text";
Run Code Online (Sandbox Code Playgroud)
到,并逃避"使用\
echo "\"$text\"";
Run Code Online (Sandbox Code Playgroud)