PHP header(Location: ...):强制地址栏中的URL更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7467330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP header(Location: ...): Force URL change in address bar
提问by vemoxy
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.phpon submit. The php file then creates some session data (store in $_SESSION), and redirects the user back to the index page:
我目前正在使用带有数据库的 PHP 会话进行身份验证的移动站点。我有一个登录页面,其中包含一个在提交时转到server_login.php的表单。php 文件然后创建一些会话数据(存储在 $_SESSION 中),并将用户重定向回索引页面:
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.phpand 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 /.
新网页 (index.php) 正确加载;但是,当header重定向页面时,地址栏的URL并没有改变;它停留在 *http://localhost/php/server/server_login.php* 而不是http://localhost/index.php,因此我所有其他使用相对路径的资源都无法加载。就好像网页仍然认为它驻留在 /php/server 而不是 /。
Strangely, my other use of header("location: ...") at logout.php works and redirects the page successfully with a URL change.
奇怪的是,我在 logout.php 中使用 header("location: ...") 的其他用法可以通过更改 URL 成功地重定向页面。
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.
我已经确保在头重定向之前我的 *server_login.php* 中没有输出(上面只是要检查的 mysql 调用),并且我也使用了 ob_start() 和 ob_end_flush()。
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?
是否有任何方法可以强制更改地址栏上的 URL(从而有望解决相对路径问题)?还是我做错了什么?
P/S: I am using jQuery Mobile.
P/S:我正在使用 jQuery Mobile。
EDIT: Here's my code for the redirection that doesn't change the URL:
编辑:这是我的重定向代码,不会更改 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");
}
回答by BuraCULa
Try changing:
尝试改变:
header("Location : blabla")
^
|
(whitespace)
To
到
header("Location: blabla")
回答by schneck
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:
好吧,如果服务器发送正确的重定向标头,浏览器会重定向并因此“更改 url”。那么可能是浏览器的问题。我不知道它是否与它有什么关系,但是您不应该在位置标头中发送相对 url(“HTTP/1.1 需要一个绝对 URI 作为参数?位置:包括方案、主机名和绝对路径,但有些客户端接受相对 URI。", http://php.net/manual/en/function.header.php),并且 "location" 必须大写,例如:
header('Location: http://myhost.com/mypage.php');
回答by kackleyjm
In your form element add data-ajax="false"
. I had the same problem using jquery mobile.
在您的表单元素中添加data-ajax="false"
. 我在使用 jquery mobile 时遇到了同样的问题。
回答by Hasib Omi
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.
然后它起作用了。
回答by Hadi
I had the same problem with posting a form. What I did was that turning off the data-ajax.
我在发布表单时遇到了同样的问题。我所做的是关闭数据ajax。
回答by cfphpflex
you may want to put a break; after your location:
你可能想休息一下;在您的位置之后:
header("HTTP/1.1 301 Moved Permanently");
header('Location: '. $YourArrayName["YourURL"] );
break;
回答by shlgug
As "cfphpflex" suggested you can add break;
after setting the header. You can also echo something, such as echo 'test';
.
正如“cfphpflex”所建议的,您可以break;
在设置标题后添加。您还可以回显某些内容,例如echo 'test';
.
回答by GULIM SHAH
// Register user's name and ID
// 注册用户名和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
// 注册用户名和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");
}
回答by Joe Barbour
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.
另外是的,总是像@Peter O 建议的那样添加空格。
回答by Jomar Delfin
I got a solution for you, Why dont you rather use Explode if your url is something like
我为你找到了一个解决方案,如果你的 url 是这样的,你为什么不宁愿使用 Explode
Url-> website.com/test/blog.php
网址-> website.com/test/blog.php
$StringExplo=explode("/",$_SERVER['REQUEST_URI']);
$HeadTo=$StringExplo[0]."/Index.php";
Header("Location: ".$HeadTo);