重定向用户变化的位置路径

时间:2018-03-26 17:55:07

标签: php

如果我有一个位于"Project/index.php"的文件,我希望我的用户无论文件结构在何处都可以重定向。

我有两个文件路径不同的文件:

"Project/file1.php"

"Project/sub/file2.php"

在这两个文件上,用户可以单击注销,将其带到注销页面。 注销页面位于:"Project/sub/logout.php" 其中包含代码:

header('Location: ../index.php');
die();

如果用户在file2上,则此脚本可以正常工作,但是如果用户在file1上,那么脚本将无效。

有没有办法让用户无需在文件结构中的位置注销而无需创建多个注销页面?

3 个答案:

答案 0 :(得分:2)

您可以使用绝对网址:

import java.util.ArrayList;

public class Bishop extends Piece{

    public Bishop(int x, int y, boolean color) {
        super(x, y, color);
    }

    public ArrayList<Point> moves(){
        ArrayList<Point> possibleMoves = new ArrayList<>();
        int x = 1;
        while(getPoint().x + x <= 7 && getPoint().y + x <= 7){
            possibleMoves.add(new Point(getPoint().x + x, getPoint().y + x));
            x++;
        }
        x = 1;
        while(getPoint().x - x >= 0 && getPoint().y + x <= 7){
            possibleMoves.add(new Point(getPoint().x - x, getPoint().y + x));
            x++;
        }
        x = 1;
        while(getPoint().x - x >= 0 && getPoint().y - x >= 0){
            possibleMoves.add(new Point(getPoint().x - x, getPoint().y - x));
            x++;
        }
        x = 1;
        while(getPoint().x + x <= 7 && getPoint().y - x >= 0){
            possibleMoves.add(new Point(getPoint().x + x, getPoint().y - x));
            x++;
        }
        return possibleMoves;
    }

    @Override
    public String toString(){ return "B"; }

    public int getValue(){ return 3;}
}

或者您可以使用相对于您网站的网址:

header('Location: http://my.site/logout');

两个代码示例都将重定向到同一页面。

答案 1 :(得分:2)

您需要使用相对于项目的DOCUMENT_ROOT的路径。

header('Location: /Project/index.php');
exit;

或者,如果您的服务器设置为自动提供index.php,请执行以下操作:

header('Location: /');
exit;

答案 2 :(得分:0)

为什么不使用绝对路径?

$path = $_SERVER['DOCUMENT_ROOT'] . "/yourpath/yourfile.php";
header("Location:" . $path);