PHP header(Location: ..):强制在地址栏更改URL


PHP header(Location: ...): Force URL change in address bar

我目前正在使用PHP会话与数据库进行身份验证的移动站点上工作。我有一个登录页面的形式,去server_login.php提交。然后,php文件创建一些会话数据(存储在$_SESSION中),并将用户重定向回索引页:

header("location:../../index.php");

新网页(index.php)正确加载;但是,当标题重定向页面时,地址栏上的URL不会改变;它停留在*http://localhost/php/server/server_login.php*而不是http://localhost/index.php,因此所有使用相对路径的其他资源都无法加载。这就好像网页仍然认为它驻留在/php/server而不是/.

奇怪的是,我在logout.php中使用的另一个header("location:…")可以工作,并且通过更改URL成功地重定向了页面。

我已经确保在头重定向之前没有在我的*server_login.php*中输出(上面只是mysql调用来检查),我也使用了ob_start()和ob_end_flush()。

是否有任何方法可以强制地址栏上的URL改变(从而有希望修复相对路径问题)?还是我做错了什么?

p/S:我使用jQuery Mobile.

编辑:这是我的重定向代码,不改变URL:

// some other stuff not shown

$sql = "SELECT * FROM $user_table WHERE email = '$myemail' AND password = '$mypassword'";
$login_result = mysql_query($sql, $connection);
$count = mysql_num_rows($login_result);
if ($count == 1) {
    // Successfully verified login information
    session_start();
    if (!isset($_SESSION['is_logged_in'])) {
        $_SESSION['is_logged_in'] = 1;
    }
    if (!isset($_SESSION['email'])) {
        $_SESSION['email'] = $myemail;
    }
    if (!isset($_SESSION['password'])) {
        $_SESSION['password'] = $mypassword;
    }
    // Register user's name and ID
    if ((!isset($_SESSION['name'])) && (!isset($_SESSION['user_id'])))  {
        $row = mysql_fetch_assoc($login_result);
        $_SESSION['name'] = $row['name'];
        $_SESSION['user_id'] = $row['user_id'];
    }
    header("Location: http://localhost:8080/meet2eat/index.php");
} else {
    // Not logged in. Redirect back to login page
    header("Location: http://localhost:8080/meet2eat/php/login.php?err=1");
}

尝试更改:

header("Location : blabla")
                ^
                |
           (whitespace)

header("Location: blabla")

好吧,如果服务器发送正确的重定向头,浏览器重定向,因此"更改url"。那么,这可能是浏览器的问题。我不知道它是否与它有任何关系,但你不应该在位置标头中发送相对url("HTTP/1.1需要一个绝对URI作为»location的参数:包括方案,主机名和绝对路径,但一些客户端接受相对URI。", http://php.net/manual/en/function.header.php)和"location"必须大写,如:

header('Location: http://myhost.com/mypage.php');

在表单元素中添加data-ajax="false"。我在使用移动版jquery时也遇到了同样的问题。

请勿使用空白。我也有同样的问题。然后删除空白,如:

header("location:index.php"); or header('location:index.php');

你可能想要放一个break;在您的位置后面:

header("HTTP/1.1 301 Moved Permanently");
header('Location:  '.  $YourArrayName["YourURL"]  );
break;

我在发布表单时遇到了同样的问题。我所做的是关闭data-ajax。

如果没有找到会话数据,您确定您要重定向的页面也没有重定向吗?这可能是你的问题

是的,总是像@Peter O建议的那样添加空白

我有一个解决方案,你为什么不宁愿使用爆炸,如果你的url是类似的东西

Url -> website.com/test/blog.php

$StringExplo=explode("/",$_SERVER['REQUEST_URI']);
$HeadTo=$StringExplo[0]."/Index.php";
Header("Location: ".$HeadTo);

把home改成你喜欢的

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/home';
header('Location: ' . $home_url);

//注册用户名和ID

if ((!isset($_SESSION['name'])) && (!isset($_SESSION['user_id'])))  {
    $row = mysql_fetch_assoc($login_result);
    $_SESSION['name'] = $row['name'];
    $_SESSION['user_id'] = $row['user_id'];
}
header("Location: http://localhost:8080/meet2eat/index.php");

改为

//注册用户名和ID

if ((!isset($_SESSION['name'])) && (!isset($_SESSION['user_id'])))  {
    $row = mysql_fetch_assoc($login_result);
    $_SESSION['name'] = $row['name'];
    $_SESSION['user_id'] = $row['user_id'];
header("Location: http://localhost:8080/meet2eat/index.php");
}

正如"cfphpflex"建议的那样,您可以在设置标题后添加break;。也可以回显一些内容,如echo 'test';

在header的末尾添加exit,这样就可以了

header("location:index.php"); or header('location:index.php'); exit;

你应该使用它像header(Location:../index.php)如果它在另一个文件夹

use

头(地点:index . php);//这个工作在我的网站

在PHP文档中阅读更多关于header()的信息

为什么所有这些位置url?

http://localhost:8080/meet2eat/index.php

你可以直接用

index.php

,如果PHP文件在同一文件夹中,这是更好的,因为如果您想要托管这些文件