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

891,773

Solution 1

Try changing:

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

To

header("Location: blabla")

Solution 2

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');

Solution 3

In your form element add data-ajax="false". I had the same problem using jquery mobile.

Solution 4

Do not use any white space. I had the same issue. Then I removed white space like:

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

Then it worked.

Solution 5

you may want to put a break; after your location:

header("HTTP/1.1 301 Moved Permanently");
header('Location:  '.  $YourArrayName["YourURL"]  );
break;
Share:
891,773
vemoxy
Author by

vemoxy

Updated on July 05, 2022

Comments

  • vemoxy
    vemoxy almost 2 years

    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 on submit. The php file then creates some session data (store in $_SESSION), and redirects the user back to the index page:

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

    The new web page (index.php) loads correctly; however, when the header redirects the page, the URL at the address bar is not changed; it stays at *http://localhost/php/server/server_login.php* instead of http://localhost/index.php and thus all my other resources that makes use of relative pathing could not be loaded. It's as if the web page still thinks that it resides at /php/server instead of /.

    Strangely, my other use of header("location: ...") at logout.php works and redirects the page successfully with a URL change.

    I've made sure that there are no outputs in my *server_login.php* before the header redirect (above it are just mysql calls to check) and I've used ob_start() and ob_end_flush() too.

    Are there any methods of forcing the URL on the address bar to change (and thus hopefully fix the relative path problem)? Or am I doing something wrong?

    P/S: I am using jQuery Mobile.

    EDIT: Here's my code for the redirection that doesn't change the 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");
    
    }
    
    • xdazz
      xdazz over 12 years
      can you show the code how you post the data?
  • vemoxy
    vemoxy over 12 years
    I see. I've fixed the code (as shown above), but I'm still getting the same URL error. :/ I've changed to HTTP Authentication as I realised that's part of my assignment requirement, but I still am intrigued by this weird 'bug'.
  • Frank Nocke
    Frank Nocke over 11 years
    Note the kind of 'catch-all' that is usual in WP's root .htacces: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] Check (i.e. by means of logging) if you get TWO passes after the redirect, your intentional redirect (re-caught by index.php) and another subsequent one that then gets it wrong... implementing an add_filter('[pre_]option_siteurl|home'); do quadruple enforce the new place to be might help you...
  • kackleyjm
    kackleyjm about 11 years
    Not sure why this was down voted, he is right. Must have to do with jquery mobile. stackoverflow.com/questions/8403242/…
  • dotancohen
    dotancohen almost 11 years
    As per RFC 2616, HTTP headers are case-insensitive. The Location header does not have to be capitalized.
  • icedwater
    icedwater almost 11 years
    The thing is, the index.php is not in the same folder.
  • Andy
    Andy over 10 years
    Had the same issue. You just need to add data-ajax on the element that doesn't require ajax (e.g., an <a> link) so that jQuery Mobile doesn't interfere with is... even if it's not in a jQuery Mobile block/form.
  • Bugdr0id
    Bugdr0id over 9 years
    I always forget this when using jquery mobile! Worked for me ;)
  • eQ19
    eQ19 about 9 years
    I got the same issue. This data-ajax=false worked in my case.
  • kurdtpage
    kurdtpage over 7 years
    You can combine the first 2 lines into one: header('Location:'.$YourArrayName["YourURL"], true, 301);