PHP项目中点击登录按钮跳转空白页,如何关联主Header实现正常跳转?
PHP登录注册页面关联Header及空白页问题修复
问题概述
我正在开发PHP项目的登录与注册页面,已用HTML完成表单编写,但需要关联PHP主Header文件让其在所有页面生效。目前点击登录按钮后会跳转到空白页面,相关代码如下:
头部代码
<div id="top-header"> <img src="/phpmotors/images/site/logo.png" alt="PHP Motors logo" id="logo"> <a href="/phpmotors/accounts/index.php?action=login.php" title="Login or Register with PHP Motors" id="acc">My Account</a>
控制器Index.PHP代码
// Get the database connection file require_once '../library/connections.php'; // Get the PHP Motors model for use as needed require_once '../model/main-model.php'; //Get the array of classifications from the database using the model. $classifications = getClassifications(); // Build a navigation bar using the $classifications array $navList = '<ul>'; $navList .= "<li><a href='/phpmotors/index.php' title='View the PHP Motors home page'>Home</a></li>"; foreach ($classifications as $classification) { $navList .= "<li><a href='/phpmotors/index.php?action=".urlencode($classification['classificationName'])."' title='View our $classification[classificationName] product line'>$classification[classificationName]</a></li>"; } $navList .= '</ul>'; $action = filter_input(INPUT_GET, 'action'); if ($action == NULL) { $action = filter_input(INPUT_POST, 'action'); } //Get the database of login information $clientEmail = filter_input(INPUT_POST, 'clientEmail'); $clientPassword = filter_input(INPUT_POST, 'clientPassword'); //Get the database of registration information $clientFirst = filter_input(INPUT_POST, 'clientFirstname'); $clientLast = filter_input(INPUT_POST, 'clientLast'); $clientEmail = filter_input(INPUT_POST, 'clientEmail'); $clientPassword = filter_input(INPUT_POST, 'clientPassword'); switch ($action) { case 'login': include 'view\login.php'; break; case 'registration': include 'view\registration.php'; default: include 'view/home.php'; } ?> </div>
表单HTML代码
<h1>Sign in</h1> <form action="/accounts/" method="POST"> <label>Email</label> <br> <input name="clientEmail" id="clientEmail" type="email" <?php if(isset($clientEmail)){echo "value='$clientEmail'";} ?> required> <br> <br> <label>Password</label> <br> <input name="clientPassword" id="clientPassword" type="password"> <br> <input type="submit" name="submit" id="regbtn" value="Login"> <!-- Add the action name - value pair --> <input type="hidden" name="action" value="Login"> </form> <p> <a href = "/accounts/index.php/?action=registration">Not a member yet?</a> </p>
问题修复方案
1. 修正表单提交路径
表单当前action="/accounts/"无法精准指向控制器,改为直接指向控制器文件:
<form action="/phpmotors/accounts/index.php" method="POST">
2. 统一action参数大小写
PHP对字符串大小写敏感,表单隐藏域的action值为Login,但控制器switch判断的是login,修改表单隐藏域:
<input type="hidden" name="action" value="login">
3. 全局引入Header文件
创建独立的header.php文件存放头部代码,然后在所有视图文件(login.php、registration.php、home.php)顶部添加引入代码:
<?php include '../includes/header.php'; ?>
同时修正头部的登录链接,去掉多余的.php后缀:
<a href="/phpmotors/accounts/index.php?action=login" title="Login or Register with PHP Motors" id="acc">My Account</a>
4. 修复控制器代码问题
- 移除重复的
$clientEmail和$clientPassword赋值,避免覆盖; - 将Windows风格的路径分隔符
\改为跨平台的/:
case 'login': include 'view/login.php'; break; case 'registration': include 'view/registration.php';
5. 排查空白页问题
若修复后仍显示空白,在控制器开头开启错误提示,定位具体问题:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
内容的提问来源于stack exchange,提问作者KwameDickson
相关产品推荐
相关产品推荐

