我的重定向课程将我重定向到mysite.com/URLerror

时间:2015-02-27 01:15:33

标签: php

我创建了一个功能来重定向我的用户,但它使用插件URLerror重定向到了错误的页面

这是我的代码:

class Redirect
{
    /**
     * To the homepage
     */
    public static function home()
    {
        header("location: " . Config::get('URL'));
    }

    /**
     * To the defined page
     *
     * @param $path
     */
    public static function to($path)
    {
        header("location: " . Config::get('url') . $path);
    }
}

我正在使用的是这样的:

Redirect::to('dashboard/following?success=5');

但它正在重定向到:

http://localhost/user/dashboard/URLerror

我的配置网址是:http://localhost/

为什么它应该这样做是没有意义的,因为代码看起来很好我认为

我在仪表板前也尝试了正斜杠,但结果相同

1 个答案:

答案 0 :(得分:0)

首先关闭:

public static function home()
{
    header("location: " . Config::get('URL'));
}

public static function to($path)
{
    header("location: " . Config::get('url') . $path);
}

您在get()字符串中使用了两种不同的样式。除非您在get()方法中对参数使用strtolower,否则您应该只使用一个。网址或网址。

此外,您应该考虑清理URL路径。试试这个:

header("Location: " . Config::get('URL') . urlencode($path));

看看它是如何运作的。

另外一件事,urlencode也会清理/,所以你应该添加另一个参数。

public function to($path, $data = array())
{
    $urldata = "";
    if(!empty($data))
    {
        $count = 0;
        $urldata = "?";
        foreach($data as $item => $value)
        {
            $count++;
            if($count < count($data))
            {
                $urldata .= urlencode($item . "=" . $value . "&");
            }
            else
            {
                $urldata .= urlencode($item . "=" . $value);
            }
        }
    }

    header("Location: " . Config::get('url') . $path . $urldata);
}

请记住,功能未经测试,因此您可能需要做一些工作才能使其正常工作。但是,这样可以更轻松地将数据传递到网址,尤其是在您使用GET请求的情况下。