【问题标题】:Php Header command working in one file but not working on otherPhp Header 命令在一个文件中工作,但在另一个文件中不工作
【发布时间】:2023-04-05 14:08:01
【问题描述】:

嘿, 我当前的目录结构是这样的

  1. 管理员(index.php、country.php)
  2. 类(connection.php,login.php,country.php)
  3. header.php
  4. footer.php
  5. index.php
  6. 包括(header.php,footer.php)

我的问题是,当我在 /admin/country.php 并使用表单发布方法和操作设置为 /classes 添加国家/地区时,在 webserver 上/country.php 我的标题语句“Header("Location: ../Admin/country.php")" 工作正常,但是当我在根目录的索引页上时尝试使用表单操作 "classes/login.php" 登录,成功登录后我使用 header("Location: ../Admin/index.php") 它永远不会重定向,但我的本地服务器一切正常,我不知道这里有什么问题,任何帮助将不胜感激, 我已经搜索了这个论坛和其他人,并尝试使用他们告诉过的技术,但没有任何效果

我的索引页index.php

我的管理部门Admin/Country.php

我的 login.php 脚本在下面

<?php 
        ob_start();
        include_once("classes/connection.php");
?>



<?php

    class login
    {
        public static function validateLogin($userName,$password)
        {
            if(isset($userName) && isset($password))
            {
                $connection  = dbconnection::getConnection();
                $query = "Select * from tbllogin Where loginID ='" . $userName .
                "' and password = '" . $password . "'";

                $result = mysql_query($query);
                $rowsAffected =  mysql_affected_rows();

                if($rowsAffected==0)
                {

                        //header("Location: ../index.php/");
                        //exit();
                        return false;
                }
                else
                {

                    while($row = mysql_fetch_array($result))
                    {
                        //working

                        $role = $row["role"];
                        if($role == "Admin")
                        {

                            //header('Location: ../Admin/index.php');
                            //exit();
                            return true;
                        }
                        else
                        {
                            //echo "hello";
                            //header("Location: ../index.php/");
                            //exit();
                            return false;
                        }

                        //return $result;
                        //header("Location: ../index.php");

                    }
                }
            }
            else
            {
                //header("Location: ../index.php/");
                return false;
            }
        }
    }


?>


<?php

    if(isset($_POST["btnSumbit"]))
    {
        $isValid = login::validateLogin($_POST["userID"],$_POST["password"]);
        if(isset($isValid))
        {
            if($isValid ==true)
            {
                $host  = $_SERVER['HTTP_HOST'];
                $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
                $extra = 'Admin/index.php';
                header("Location: http://$host$uri/$extra");
                exit();
            }
        }
    }

    ob_end_flush();

?>

【问题讨论】:

  • 请显示一些您的实际代码。它将帮助社区能够帮助您。
  • 不重定向时会出现什么错误?您是否打开了错误和警告,以便查看问题所在?
  • @anigel -> 我没有收到任何错误,当我使用表单操作 classes/login.php 从 index.php 登录然后在 classes/login.php 中我调用 header( “位置:../Admin/index.php”)并且它不起作用,但是当我在“/Admin/country.php”并调用表单操作../classes/country.php然后使用标题时(“位置:../Admin/country.php”)它按预期重定向!!e

标签: php redirect header


【解决方案1】:

不要将标头重定向与相对路径一起使用。您应该重定向到前端 URL 路径或绝对路径。

您的“classes/login.php”可能是“index.php”中包含的文件——因此您实际上是在尝试跳出 Web 服务器目录——这就是它在本地工作但不能在本地工作的原因服务器。

【讨论】:

猜你喜欢
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2020-06-29
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多