在 php 中嵌入 html 意外 <' 错误
Embedding html in php unexpected <' error
我是 php 的新手,但我不知道问题出在哪里。我正在尝试为字符串输入创建一个文本框。
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
echo $_GET['subject'];
<html> file_put_contents("/file/location", $_GET['subject'], FILE_APPEND); </html>
?>
第一部分运行良好,但当我到达 <html>
时,它开始吐出各种错误。
您将 html 与 php 混合使用,但未正确封闭。
这是编辑后的版本:
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
?>
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
<?php
echo $_GET['subject'] . file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
正如我在评论中所说,您将纯 HTML 注入 PHP。
您必须关闭 PHP 标签,然后重新打开。
此 <html> file_put_contents
和相关 </html>
中的 <html>
标记也需要删除。
如果给您带来问题,/file/location
可能应该有一个尾部斜杠,但可能不需要,这只是这里的旁注。
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
?>
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
<?php
echo $_GET['subject'];
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
但是,您需要在此处使用条件语句,因为您将收到回显 GET 数组的未定义索引通知,并且在 file_put_contents
中看到您的整个代码都在同一个文件中; action=""
建议。 Error reporting 会在这里帮助你。
即:if(!empty($var)){...}
and/or if(isset($var)){...}
.
还要确保该文件夹可以写入并且已为其设置了适当的权限。
您的文件夹声明可能还要求它是完整的服务器路径。
请查阅 fopen()
http://php.net/manual/en/function.fopen.php 上的手册。
即:fopen("/var/usr/public_html/file/location","r");
.
file_put_contents()
也可能发生同样的情况。如果您现在遇到问题,请尝试使用服务器路径。
脚注:
您可以将结尾 </html>
放在
之后
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
因为回显 HTML 标签之外的内容会在 console/HTML 源代码中抛出内容。
我是 php 的新手,但我不知道问题出在哪里。我正在尝试为字符串输入创建一个文本框。
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
echo $_GET['subject'];
<html> file_put_contents("/file/location", $_GET['subject'], FILE_APPEND); </html>
?>
第一部分运行良好,但当我到达 <html>
时,它开始吐出各种错误。
您将 html 与 php 混合使用,但未正确封闭。 这是编辑后的版本:
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
?>
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
<?php
echo $_GET['subject'] . file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
正如我在评论中所说,您将纯 HTML 注入 PHP。
您必须关闭 PHP 标签,然后重新打开。
此 <html> file_put_contents
和相关 </html>
中的 <html>
标记也需要删除。
如果给您带来问题,/file/location
可能应该有一个尾部斜杠,但可能不需要,这只是这里的旁注。
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
?>
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
<?php
echo $_GET['subject'];
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
但是,您需要在此处使用条件语句,因为您将收到回显 GET 数组的未定义索引通知,并且在 file_put_contents
中看到您的整个代码都在同一个文件中; action=""
建议。 Error reporting 会在这里帮助你。
即:if(!empty($var)){...}
and/or if(isset($var)){...}
.
还要确保该文件夹可以写入并且已为其设置了适当的权限。
您的文件夹声明可能还要求它是完整的服务器路径。
请查阅 fopen()
http://php.net/manual/en/function.fopen.php 上的手册。
即:fopen("/var/usr/public_html/file/location","r");
.
file_put_contents()
也可能发生同样的情况。如果您现在遇到问题,请尝试使用服务器路径。
脚注:
您可以将结尾 </html>
放在
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
因为回显 HTML 标签之外的内容会在 console/HTML 源代码中抛出内容。