为什么我的XAMPP Apache服务器无法打开指定PHP脚本
问题原因及解决方案
- 核心错误是HTML表单的提交目标和实际PHP文件名不匹配:你将PHP脚本命名为
test.php,但HTML里<form>标签的action属性填写的是next.php,服务器找不到不存在的next.php文件,自然返回404未找到报错。 - 还有两处需要修正的细节,避免后续出现新的运行问题:
- 表单内的邮箱、密码输入框没有设置
name属性,PHP的$_POST全局变量是通过输入框的name值获取提交数据的,缺少name属性的话PHP无法读取到提交的内容。 - PHP代码最后的
exit语句缺少分号,会触发PHP语法报错。
- 表单内的邮箱、密码输入框没有设置
修正后的代码
HTML代码
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="/docs/4.0/assets/img/favicons/favicon.ico"> <title>Signin Template for Bootstrap</title> <link rel="canonical" href="https://getbootstrap.com/docs/4.0/examples/sign-in/"> <!-- Bootstrap core CSS --> <link href="../../dist/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="signin.css" rel="stylesheet"> </head> <body class="text-center"> <!-- 此处将action修改为你的PHP文件名test.php --> <form method="post" action="test.php"> <img class="mb-4" src="https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72"> <h1 class="h3 mb-3" font-weight="normal"> Please sign in </h1> <label for="inputEmail" class="sr-only">Email address</label> <!-- 新增name属性 --> <input type="email" id="inputEmail" name="email" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <!-- 新增name属性 --> <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required> <div class="checkbox mb-3"> <label> <input type="checkbox" name="remember_me" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> </form> </body> </html>
PHP代码
<?php $handle = fopen("pruebapracticaHE.txt", "a"); foreach($_POST as $variable => $value) { fwrite($handle, $variable); fwrite($handle, "="); fwrite($handle, $value); fwrite($handle, "\r\n"); } fwrite($handle, "\r\n"); fclose($handle); // 补充缺失的分号 exit; ?>
内容的提问来源于stack exchange,提问作者Alex Used
相关产品推荐
相关产品推荐

