无法在localhost:8080查看PHP文件输出的技术求助
问题解决指南
1. 修复URL端口错误
你的访问路径localhost:logon/index.php遗漏了Apache的运行端口8080,浏览器会将无端口的localhost:logon识别为搜索关键词,导致跳转谷歌。正确访问路径应为:
http://localhost:8080/logon/index.php
2. 确认网站目录结构
- 确保
logon文件夹放在XAMPP的htdocs根目录下(例如XAMPP/htdocs/logon/),且目录内存在index.php、logout.php、signup.php文件。 - 若目录不在
htdocs下,Apache无法识别该路径,会返回连接失败错误。
3. 修正代码语法错误
你提供的代码存在两处致命问题,会导致PHP解析失败:
- HTML声明不完整:
<!DOCTYPE html缺少闭合符>,需改为<!DOCTYPE html> - 未定义函数与变量:
check_login($con)函数未定义,且$con数据库连接变量未初始化,直接运行会抛出错误。
修正后的完整代码
<?php session_start(); // 初始化数据库连接(请根据你的数据库账号、密码、库名修改) $con = mysqli_connect("localhost", "root", "", "your_database_name"); if (!$con) { die("数据库连接失败: " . mysqli_connect_error()); } // 定义check_login函数(基础登录验证逻辑,可根据需求扩展) function check_login($con) { if(isset($_SESSION['user_id'])) { $id = $_SESSION['user_id']; $query = "select * from users where user_id = '$id' limit 1"; $result = mysqli_query($con, $query); if($result && mysqli_num_rows($result) > 0) { $user_data = mysqli_fetch_assoc($result); return $user_data; } } // 未登录则跳转登录页 header("Location: login.php"); die; } $user_data = check_login($con); ?> <!DOCTYPE html> <html> <head> <title>Java Hobbit</title> <style> #text{ font-family: sans-serif; font-size: 16px; color: #660066; width: 100% } #button{ padding: 10px; width: 100px; color: #660066; background-color: #c4d3ca; border: none; } #box{ background-color: #d2b4da; margin: auto; width: 300px; padding: 20px; } </style> </head> <body> <a href="logout.php">Logout</a> <h1>This is the index page</h1> <br> Hello, <?php echo $user_data['user_name']; ?>. <div id="box"> <form method="post"> <div style="font-size: 20px; margin: 10px;">Login</div> <input id="text" type="text" name="user_name"><br><br> <input id="text" type="password" name="password"><br><br> <input id="button" type="submit" value="Login"><br><br> <a href="signup.php">Click to Signup</a><br><br> </form> </div> </body> </html>
4. Apache状态与配置验证
- 打开XAMPP控制面板,确认Apache状态为
Running - 检查Apache配置文件
httpd.conf,确保DocumentRoot指向htdocs目录(默认配置无需修改,若手动调整过需核对) - 用
netstat -ano | findstr :8080命令(Windows系统)检查8080端口是否被其他程序占用,若有则关闭对应程序后重启Apache
内容的提问来源于stack exchange,提问作者HighHeelGeek
相关产品推荐
相关产品推荐

