You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache 2执行PHP登录脚本时302重定向失效问题求助

KLiK-SocialMediaWebsite 登录重定向问题排查与解决

问题背景

运行一个4年前的KLiK-SocialMediaWebsite项目,已按操作步骤完成配置并添加.htaccess文件,但点击登录按钮后,Apache日志显示302重定向状态码,URL返回login=success但页面并未完成跳转。修改重定向路径后问题依然存在。

相关Apache日志记录:

::1 - - [21/Apr/2023:15:55:30 -0500] "POST /socialmedia/includes/login.inc.php HTTP/1.1" 302 354 "https://sourcecodedepot.com/socialmedia/login.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0"

涉及的login.inc.php代码:

<?php

if (isset($_POST['login-submit']))
{

require 'dbh.inc.php';

$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];

if (empty($mailuid) || empty($password))
{
    header("Location: ../login.php?error=emptyfields");
    exit();
}
else
{
    $sql = "SELECT * FROM users WHERE uidUsers=?;";
    $stmt = mysqli_stmt_init($conn);
    
    if (!mysqli_stmt_prepare($stmt, $sql))
    {
        header("Location: ../login.php?error=sqlerror");
        exit();
    }
    else
    {
        mysqli_stmt_bind_param($stmt, "s", $mailuid);
        mysqli_stmt_execute($stmt);
        
        $result = mysqli_stmt_get_result($stmt);
        
        if($row = mysqli_fetch_assoc($result))
        {  
            
            $pwdCheck = password_verify($password, $row['pwdUsers']);
            if ($pwdCheck == false)
            {
                header("Location: ../login.php?error=wrongpwd");
                exit();
            }
            else if($pwdCheck == true)
            {
                session_start();
                $_SESSION['userId'] = $row['idUsers'];
                $_SESSION['userUid'] = $row['uidUsers'];
                $_SESSION['userLevel'] = $row['userLevel'];
                $_SESSION['emailUsers'] = $row['emailUsers'];
                $_SESSION['f_name'] = $row['f_name'];
                $_SESSION['l_name'] = $row['l_name'];
                $_SESSION['gender'] = $row['gender'];
                $_SESSION['headline'] = $row['headline'];
                $_SESSION['bio'] = $row['bio'];
                $_SESSION['userImg'] = $row['userImg'];
                //$_SESSION['coverImg'] = $row['coverImg'];
                
                
                header("Location: ../index.php?login=success");
      //header("Location: ../sourcecodedepot.com/socialmedia/index.php");
                exit();
            }
            else
            {
                header("Location: ../login.php?error=wrongpwd");
                exit();
            }
        }
        else
        {
            header("Location: ../login.php?error=nouser");
            exit();
        }
    }
}

}
     else 
{
    header("Location: ../login.php");
    exit();
}

问题解答

1. ../路径的含义

是的,../是相对路径写法,代表向上一级目录。比如当前脚本login.inc.php位于socialmedia/includes/目录下,../login.php就指向socialmedia/login.php,也就是跳出当前的includes目录,定位到上一级目录中的login.php文件。

2. 实现登录成功后正常重定向到默认页面

确定默认页面

从项目结构和代码逻辑来看,index.php是该社交网站的首页(默认页面),代码中原有的重定向目标../index.php也是指向这个文件。

排查并修复重定向失效问题

你遇到的302状态码返回但页面不跳转的问题,可从以下几个方向排查:

  • 路径匹配问题:
    你的项目部署在https://sourcecodedepot.com/socialmedia/下,login.inc.php在socialmedia/includes/目录中,原代码的../index.php会定位到网站根目录的index.php(即https://sourcecodedepot.com/index.php),而不是socialmedia目录下的index.php。这是路径错误导致的重定向失效。

    修复方案:使用网站根目录绝对路径来编写重定向地址,避免相对路径的歧义:

    header("Location: /socialmedia/index.php?login=success");
    

    这种写法直接从网站根目录开始定位,不会因当前脚本位置出错。

  • 输出缓冲冲突:
    如果header()函数执行前有任何输出(包括文件开头的空格、HTML代码、echo内容等),会导致header()失效。检查login.inc.php及其引用的dbh.inc.php文件开头是否有多余空格或提前输出的内容。

  • 会话初始化问题:
    代码中session_start()放在登录成功后逻辑上可行,但可尝试将session_start()移到脚本最顶部(<?php之后立即执行),确保会话正常初始化,避免会话写入失败导致的页面跳转异常。

  • .htaccess规则干扰:
    你添加的.htaccess文件可能包含重写规则,干扰了重定向路径。可暂时注释掉.htaccess中的规则,测试登录是否正常,再逐步排查规则问题。


内容的提问来源于stack exchange,提问作者user2180706

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:23:11