首页 文章

PHP Headers (位置:...):在地址栏中强制更改URL

提问于
浏览
48

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

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

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

奇怪的是,我在logout.php上使用 Headers (“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");

}

14 回答

  • -3

    why all of this location url?

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

    你可以使用

    index.php
    

    如果php文件在同一个文件夹中,这样更好,因为如果你想托管文件或更改端口,你可以毫无问题地到达这个URL .

  • -3

    你可能想休息一下;在你的位置后:

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

    使用

    header(“Location:index.php”); //这个工作在我的网站上

    阅读更多关于header()的php文档 .

  • 0

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

    也是,总是添加像@Peter O建议的空格 .

  • 17

    //注册用户的名称和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");
    }
    
  • 0

    如"cfphpflex"建议你可以在设置 Headers 后添加 break; . 你也可以回应一些东西,比如 echo 'test'; .

  • -7

    尝试改变:

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

    header("Location: blabla")
    
  • 3

    只需根据自己的喜好换回家

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

    在表单元素中添加 data-ajax="false" . 我使用jquery mobile遇到了同样的问题 .

  • 0

    我为你找到了一个解决方案,如果你的网址类似,为什么不使用Explode

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

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

    发布表单时遇到了同样的问题 . 我做的是关闭数据-ajax .

  • 1

    不要使用任何空白区域 . 我遇到过同样的问题 . 然后我删除了白色空间,如:

    header(location:index.php);
    

    然后它奏效了 .

  • 5

    如果它在另一个文件夹中,您可以像 header(Location:../index.php) 一样使用它

  • 58

    好吧,如果服务器发送了正确的重定向标头,浏览器会重定向,因此"changes the url" . 那么它可能是一个浏览器问题 . 我不知道它是否与它有任何关系,但你不应该在位置 Headers 中发送相对url("HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. ",http://php.net/manual/en/function.header.php),并且"location"必须大写,如:

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

相关问题