沃梦达 / 编程问答 / php问题 / 正文

PHP 标头(位置:...):强制地址栏中的 URL 更改

PHP header(Location: ...): Force URL change in address bar(PHP 标头(位置:...):强制地址栏中的 URL 更改)

本文介绍了PHP 标头(位置:...):强制地址栏中的 URL 更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

新网页(index.php)加载正确;但是,当header重定向页面时,地址栏的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:

//其他一些未显示的东西$sql = "SELECT * FROM $user_table WHERE email = '$myemail' AND password = '$mypassword'";$login_result = mysql_query($sql, $connection);$count = mysql_num_rows($login_result);如果 ($count == 1) {//成功验证登录信息session_start();如果 (!isset($_SESSION['is_logged_in'])) {$_SESSION['is_logged_in'] = 1;}如果 (!isset($_SESSION['email'])) {$_SESSION['email'] = $myemail;}如果 (!isset($_SESSION['password'])) {$_SESSION['password'] = $mypassword;}//注册用户名和IDif ((!isset($_SESSION['name'])) && (!isset($_SESSION['user_id']))) {$row = mysql_fetch_assoc($login_result);$_SESSION['name'] = $row['name'];$_SESSION['user_id'] = $row['user_id'];}header("位置:http://localhost:8080/meet2eat/index.php");} 别的 {//未登录.重定向回登录页面header("位置:http://localhost:8080/meet2eat/php/login.php?err=1");}

解决方案

尝试改变:

header("位置:blabla")^|(空格)

header("位置:blabla")

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

}

解决方案

Try changing:

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

To

header("Location: blabla")

这篇关于PHP 标头(位置:...):强制地址栏中的 URL 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP 标头(位置:...):强制地址栏中的 URL 更改

基础教程推荐