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

后端 未结 14 1564
失恋的感觉
失恋的感觉 2020-11-28 08:10

I\'m currently working on a mobile site with authentication using PHP sessions with a database. I have a login page with a form that goes to server_login.php

相关标签:
14条回答
  • 2020-11-28 08:14

    Are you sure the page you are redirecting too doesn't have a redirection within that if no session data is found? That could be your problem

    Also yes always add whitespace like @Peter O suggested.

    0 讨论(0)
  • 2020-11-28 08:15

    Just change home to your liking

    $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/home';
    
    header('Location: ' . $home_url);
    
    0 讨论(0)
  • 2020-11-28 08:21

    Well, if the server sends a correct redirection header, the browser redirects and therefore "changes the url". It might be a browser issue, then. I don't know if it has anything to do with it, but you should not send a relative url in the location header ("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), and "location" must be capitalized, like:

    header('Location: http://myhost.com/mypage.php');
    
    0 讨论(0)
  • 2020-11-28 08:21

    As "cfphpflex" suggested you can add break; after setting the header. You can also echo something, such as echo 'test';.

    0 讨论(0)
  • 2020-11-28 08:24

    // 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");
    

    change to

    // 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");
    }
    
    0 讨论(0)
  • 2020-11-28 08:25

    Try changing:

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

    To

    header("Location: blabla")
    
    0 讨论(0)
提交回复
热议问题