PHP - 无法从登录页面重定向到主页

PHP - Unable to redirect from login page to home page

我的登录系统遇到了问题。无法从 登录页面 (index.php) 重定向 主页 (index2.php)。尽管我输入了正确的 ID 和密码,但单击 "Login" 按钮时登录页面没有重定向到主页。

这是我的代码:

index.php

<?php
    session_start();
?>

<form action="checklogin.php" method="POST">
<div align="center">
    <table style="background-color:#9FF781" width="285" border="1">
        <tr>
            <td width="130"><div align="right"><strong>ID :</strong></div></td>
            <td width="159"><label for="admid"></label>
                <input type="text" name="admid" id="admid"></td>
        </tr>
        <tr>
            <td><div align="right"><strong>PASSWORD :</strong></div></td>
            <td><label for="admpwd"></label>
                <input type="password" name="admpwd" id="admpwd"></td>
        </tr>
        <tr>
            <td colspan="2"><div align="center">
                <button type="submit" name="login">Login</button>
            </div></td>
        </tr>
    </table>
</div>
</form>

checklogin.php

<?php 
    include('../../../Connections/fyp.php');
    session_start();
    $admid = $_POST["admid"];
    $admpwd = $_POST["admpwd"];
    $result = mysql_query("SELECT * FROM acc_login WHERE id = '$admid' AND pwd = '$admpwd'");
    $row = mysql_num_rows($result);

    if($admid != "" && $admpwd != "")
    {
        if ($row != "")
        {
            $_SESSION['loggedin'] = $admid;
            header("Location: index2.php");
        }
        else
        {
            echo "<script type='text/javascript'>alert('Invalid ID or Password! Please try again!')</script>";
            exit('<a href="index.php"><strong>Go Back</strong></a> to Login Page');
        }
    }
    else
    {
        echo "<script type='text/javascript'>alert('ID or Password field cannot be blank! Please try again!')</script>";
        exit('<a href="index.php"><strong>Go Back</strong></a> to Login Page');
    }
?>

index2.php

<?php
    include("session.php"); //check whether user was logged in.
?>

session.php

<?php
    include('../../../Connections/fyp.php');
    session_start();
    $verify = $_SESSION['loggedin'];
    $session = mysql_query("SELECT * FROM acc_login WHERE id = '$verify'");
    $row = mysql_fetch_array($session);
    $login_session = $row['id'];

    if(!isset($login_session))
    {
        header("Location: index.php");
    }
?>

如果 header 已发送,则 header 功能无法正常工作。所以碰巧用这个。

<?php
include('../../../Connections/fyp.php');
session_start();
$verify = $_SESSION['loggedin'];
$session = mysql_query("SELECT * FROM acc_login WHERE id = '$verify'");
$row = mysql_fetch_array($session);
$login_session = $row['id'];

if(!isset($login_session))
{
    echo "<script>location.href='index.php'</script>";
}
?>

这就是我在发送 header 甚至 ob_end_flush() 后需要重定向时所做的;已调用。

你试过绝对 url 到你的主页吗?

checklogin.php

header("Location: http://www.example.com/index2.php");

修改你的文件目录如下:

1- 在你的 checklogin.php

include('fyp.php');

2- 在你的 session.php

include('fyp.php');

并删除 PHP 文件中的最后一个 ?> PHP 结束标记(这是一个好习惯),以避免 headers' 问题,然后将所有你的 PHP 个文件在同一个文件夹中,如果你的问题解决了,这意味着你需要根据你的项目文件夹结构仔细检查你传递的 URL。

您是否尝试调试您的代码,请在 header("Location: index2.php"); 之前放置 die('debug');(或 die() 函数中的任何内容)并检查输出,这将帮助您解决实际问题. 或者也把 exit(1); 放在 header();

之后