PHP header(Location: ...): Force URL change in address bar
When using header("Location: ...") to redirect a user after a successful login, you might expect the browser's address bar to update immediately. However, there is a specific behavior in jQuery Mobile that often defies this expectation. If you are building a mobile site with authentication and find that the page loads correctly but the URL remains stuck on the original request (e.g., server_login.php), you are likely running into how jQuery Mobile handles form submissions rather than a PHP configuration error.
The core issue arises because jQuery Mobile intercepts standard HTML form submissions by default. When a form is submitted, jQuery Mobile prevents the browser from performing a full page reload. Instead, it performs an AJAX request to fetch the response and then injects the new content into the current DOM. While this provides a smooth, app-like experience, it leaves the URL in the address bar unchanged because the browser never actually navigated to a new resource; it simply updated the content of the current page.
This is why your logout functionality works fine. If logout.php performs a standard redirect without triggering a jQuery Mobile event that prevents navigation, or if the logout action is handled differently (perhaps via a server-side form submission that bypasses AJAX interception), the browser treats it as a genuine HTTP 302/303 redirect. The address bar updates because the browser follows the Location header to fetch a completely new URL.
To fix the relative pathing issue in your login flow, you have two primary options: forcing a full page reload or explicitly updating the address bar after the AJAX load.
Option 1: Force a Full Page Reload
The most robust solution is to bypass jQuery Mobile's default interception mechanism. You can do this by adding an attribute to your form tag that tells jQuery Mobile to treat the submission as a standard HTTP request.
Add data-ajax="false" to your login form tag in your HTML. When jQuery Mobile sees this attribute, it allows the browser to perform a normal navigation event upon form submission. Consequently, when PHP sends the header("Location: ..."), the browser will follow it, fetch the new URL (index.php), and update the address bar accordingly.
<form action="server_login.php" method="POST" data-ajax="false">
<!-- Your form fields here -->
</form>
This is often the preferred approach for authentication flows because login processes are sensitive to state changes, and a full page reload ensures that any session cookies or headers are handled cleanly by the browser engine.
Option 2: Manually Update the Address Bar
If modifying every form in your application is not feasible, you can handle the redirection entirely on the client side after receiving the PHP response. In server_login.php, instead of relying solely on the server-side header to change the URL, you can output a small script that forces the browser to update its history and location manually once the content loads.
You would need to ensure no whitespace or BOM precedes the output in your PHP file. Then, return a response that includes both the necessary session data (if needed) and a JavaScript snippet:
// server_login.php
session_start();
// ... database logic and session setting ...
header("Location: index.php"); // This triggers the redirect but jQuery Mobile may ignore it
exit;
However, because jQuery Mobile intercepts this, the exit happens after the AJAX request is processed. The reliable workaround in this scenario involves returning a JSON response or plain text that instructs the client to update the location. But a cleaner approach within the context of jQuery Mobile is often to let the redirect happen but then explicitly run JavaScript to force the window location to match the new URL:
// This would typically be inside your success callback or global event
$(document).on("pageinit", function() {
// Logic to detect if we just came from a redirect
});
Given the complexity of mixing server-side redirects with AJAX frameworks, Option 1 (using data-ajax="false") remains the industry standard for login pages. It aligns the client's behavior with the server's intent: "I am sending you to a new page."
A Note on Deprecated Functions
Your source material shows the use of mysql_query. Please note that this extension is deprecated as of PHP 5.5 and removed in PHP 7.0+. If you are targeting modern PHP versions, you should migrate to mysqli or PDO. Using deprecated functions can lead to security vulnerabilities and missing features in future updates. While switching database drivers won't fix the AJAX redirection issue directly, it is a critical step for maintaining long-term project health.
Conclusion on Relative Paths
The fundamental reason your resources fail to load is that the browser believes it is still at /php/server/. By forcing a full page reload via data-ajax="false", you align the URL in the address bar with the actual content being served. This allows all relative paths (CSS, JS, images) to resolve correctly against the new base URL (index.php), ensuring your application behaves consistently across different environments and devices.