我昨天开始学习 PHP,我在我的 Ubuntu 系统上设置了一个小的 Apache 服务器。 现在,我的代码有点乱,但我会放在这里:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>My PHP</title>
</head>
<body>
<h1>Welcome to leave a message</h1>
<p>Welcome message: <?php echo readfile("prev_message.txt"); ?>
<form action="form_handle.php" method="post">
<p>Your message: <input type="text" name="message" /></p>
<p><input type="submit" /></p>
</form>
</body>
form_handle.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
</head>
<body>
<p>You've submitted your message for the next person</p>
<?php
$messagefile = fopen("prev_message.txt","w") or die("Unable to save message!");
$message = $_POST["name"];
fwrite($messagefile,$message);
fclose($myfile);
?>
</body>
</html>
当我使用 localhost 连接到服务器时,一切都按预期工作。您在框中输入的文本会保存到名为 prev_message.txt
的文本文件中,然后当您重新加载 index.php
时,输入的文本会正常显示。
然而,由于某种原因,当我在同一网络上的笔记本电脑上通过我的 IPV4 地址连接到同一页面时,提交文本不起作用。 index.php
显示文本文件中写入的文本很好,但是当我提交我输入的文本时,服务器端会吐出一堆错误,并且文本文件被擦除:
[Sun Aug 01 12:02:38.241279 2021] [core:notice] [pid 1218] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 01 12:03:50.927608 2021] [php7:notice] [pid 1221] [client 192.168.1.85:57616] PHP Notice: Undefined index: name in /var/www/html/form_handle.php on line 11,referer: http://192.168.1.157/
[Sun Aug 01 12:03:50.927654 2021] [php7:notice] [pid 1221] [client 192.168.1.85:57616] PHP Notice: Undefined variable: myfile in /var/www/html/form_handle.php on line 13,referer: http://192.168.1.157/
[Sun Aug 01 12:03:50.927665 2021] [php7:warn] [pid 1221] [client 192.168.1.85:57616] PHP Warning: fclose() expects parameter 1 to be resource,null given in /var/www/html/form_handle.php on line 13,referer: http://192.168.1.157/
[Sun Aug 01 12:15:59.872866 2021] [php7:notice] [pid 1224] [client 192.168.1.85:57634] PHP Notice: Undefined index: name in /var/www/html/form_handle.php on line 11,referer: http://192.168.1.157/
[Sun Aug 01 12:15:59.872917 2021] [php7:notice] [pid 1224] [client 192.168.1.85:57634] PHP Notice: Undefined variable: myfile in /var/www/html/form_handle.php on line 13,referer: http://192.168.1.157/
[Sun Aug 01 12:15:59.872930 2021] [php7:warn] [pid 1224] [client 192.168.1.85:57634] PHP Warning: fclose() expects parameter 1 to be resource,referer: http://192.168.1.157/
[Sun Aug 01 12:17:11.594541 2021] [php7:notice] [pid 3569] [client 192.168.1.85:57636] PHP Notice: Undefined index: name in /var/www/html/form_handle.php on line 11,referer: http://192.168.1.157/
[Sun Aug 01 12:17:11.594573 2021] [php7:notice] [pid 3569] [client 192.168.1.85:57636] PHP Notice: Undefined variable: myfile in /var/www/html/form_handle.php on line 13,referer: http://192.168.1.157/
[Sun Aug 01 12:17:11.594581 2021] [php7:warn] [pid 3569] [client 192.168.1.85:57636] PHP Warning: fclose() expects parameter 1 to be resource,null given in /var/www/html
如果需要更多细节,请告诉我。