PHP代码未执行直接显示,注册无法跳转至index.html问题求助
注册操作异常:页面直接显示PHP代码,未写入数据库也未跳转
执行注册操作本应写入数据库并跳转至index.html,但页面直接显示PHP代码,未完成跳转。已尝试单独创建数据库连接文件,排查所有可能问题后仍未解决。
db.php
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "mi_base_de_datos"; $port = 3307; $conn = new mysqli($servername, $username, $password, $dbname, $port); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
register.php
<?php include 'db.php'; ob_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; $birthday = $_POST['birthday']; $gender = $_POST['gender']; if ($password !== $confirm_password) { echo "Passwords do not match."; exit(); } $hashed_password = password_hash($password, PASSWORD_DEFAULT); $sql = $conn->prepare("INSERT INTO users (name, email, password, birthday, gender) VALUES (?, ?, ?, ?, ?)"); $sql->bind_param("sssss", $name, $email, $hashed_password, $birthday, $gender); if ($sql->execute()) { header("Location: ../index.html"); ob_end_flush(); exit(); } else { echo "Error: " . $sql->error; } $sql->close(); } $conn->close(); ob_end_flush(); ?>
login.php
<?php include 'db.php'; if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['email']) && isset($_POST['password'])) { $email = $_POST['email']; $password = $_POST['password']; $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); if (password_verify($password, $row['password'])) { header("Location: ../index.html"); exit(); } else { echo "Invalid password."; } } else { echo "No user found with this email."; } $stmt->close(); } else { echo "Please fill in both the email and password fields."; } $conn->close(); ?>
formulario.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Register and Login</title> <link rel="stylesheet" href="css/formulario.css"> </head> <body> <div class="header"> <div class="header-content"> <h1>Bienvenido</h1> <div class="form-container"> <!-- Formulario de Iniciar Sesión --> <div class="form-box" id="login-form"> <h2>Iniciar Sesión</h2> <form action="php/login.php" method="POST"> <input type="email" name="email" placeholder="Correo Electrónico" required> <input type="password" name="password" placeholder="Contraseña" required> <button type="submit">Iniciar Sesión</button> </form> <p>¿No tienes una cuenta? <a href="#" onclick="toggleForm('register')">Regístrate aquí</a></p> </div> <!-- Formulario de Registro --> <div class="form-box" id="register-form" style="display: none;"> <h2>Registrarse</h2> <form action="php/register.php" method="POST"> <input type="text" name="name" placeholder="Nombre" required> <input type="email" name="email" placeholder="Correo Electrónico" required> <input type="password" name="password" placeholder="Contraseña" required> <input type="password" name="confirm_password" placeholder="Confirmar Contraseña" required> <input type="date" name="birthday" placeholder="Fecha de Nacimiento" required> <select name="gender" required> <option value="">Seleccione su género</option> <option value="Mujer">Mujer</option> <option value="Hombre">Hombre</option> <option value="Otro">Otro</option> </select> <button type="submit">Registrarse</button> </form> <p>¿Ya tienes una cuenta? <a href="#" onclick="toggleForm('login')">Inicia sesión</a></p> </div> </div> </div> </div> <script> function toggleForm(form) { document.getElementById('login-form').style.display = form === 'login' ? 'block' : 'none'; document.getElementById('register-form').style.display = form === 'register' ? 'block' : 'none'; } </script> </body> </html>
同时附上XAMPP控制面板截图及数据库结构截图。
内容的提问来源于stack exchange,提问作者Samupok777.ja
相关产品推荐
相关产品推荐

