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:
// 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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
尝试更改:
至
Try changing:
To
那么,如果服务器发送正确的重定向标头,浏览器就会重定向并因此“更改 url”。那么可能是浏览器问题。
我不知道它是否与此有关,但您不应该在位置标头中发送相对网址(“HTTP/1.1 需要绝对 URI 作为»位置的参数:包括方案、主机名和绝对路径,但有些客户端接受相对 URI。 ", http://php.net/manual/en/function.header.php),并且“location”必须大写,例如:
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:
在表单元素中添加
data-ajax="false"
。我在使用 jquery mobile 时遇到了同样的问题。In your form element add
data-ajax="false"
. I had the same problem using jquery mobile.不要使用任何空白。我有同样的问题。然后我删除了空格,例如:
然后它起作用了。
Do not use any white space. I had the same issue. Then I removed white space like:
Then it worked.
你可能想休息一下;在您的位置之后:
you may want to put a break; after your location:
我在发布表格时遇到了同样的问题。我所做的就是关闭数据ajax。
I had the same problem with posting a form. What I did was that turning off the data-ajax.
如果未找到会话数据,您确定您正在重定向的页面也没有重定向吗?这可能是你的问题
另外,是的,总是像@Peter O建议的那样添加空格。
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.
把家改成自己喜欢的就好
Just change home to your liking
在标题末尾添加 exit 即可工作
Add exit at the end of header then it will work
我为你找到了一个解决方案,如果你的 url 类似于
Url-> ,为什么不使用 Explode? website.com/test/blog.php
I got a solution for you, Why dont you rather use Explode if your url is something like
Url-> website.com/test/blog.php
// 注册用户名和ID
更改为
// 注册用户名和ID
// Register user's name and ID
change to
// Register user's name and ID
正如“cfphpflex”建议的那样,您可以在设置标头后添加
break;
。您还可以回显某些内容,例如echo 'test';
。As "cfphpflex" suggested you can add
break;
after setting the header. You can also echo something, such asecho 'test';
.如果它在另一个文件夹中,您应该像
header(Location:../index.php)
一样使用它You are suppose to use it like
header(Location:../index.php)
if it in another folder使用
header("位置:index.php"); //我网站上的这项工作
请在 php 文档中阅读有关 header() 的更多信息。
use
header("Location: index.php"); //this work in my site
read more on header() at php documentation.
为什么所有这些位置 url?
如果 php 文件位于同一文件夹中,则可以使用
,这更好,因为如果您想托管这些文件
或者更改端口,您将可以毫无问题地访问此 URL。
why all of this location url?
you can just use
if the php files are in the same folder and this is better because if you want to host the files
or change the port you will have no problem reaching this URL.