当URL传递变量时,php $ _g是空的

发布于 2025-01-18 03:41:00 字数 991 浏览 3 评论 0原文

这是我的第一个堆栈溢出问题。 我有一个主页,您可以在其中登录,如果您登录它会将您发送到登录php,其中有一些代码检查登录是否不正确。如果是这种情况,则此代码将您重定向到主页:

标题(“位置:” .Server_Address。“ Home?error = Invalid_login”);

现在您回到主页,但是url url未设置变量。它没有在 var_dump(parse_url($ _ server ['http_referer']))); or var_dump($ _ get);

这是我的index.php文件:

<?php

session_start();
require_once("config.php");

if(!(isset($_GET['page']))) { // redirect to main page 
    
    header("Location: ".SERVER_ADDRESS."home");
    
} else {
    
    $mp = new MainPage($_GET['page']); // redirect to given page
    
}

?>

如果您尝试使用错误的凭据再次登录,并且执行相同的重定向过程,则现在将显示变量。 如果我要在几秒钟后刷新标头,也会发生同样的事情。

我似乎不明白为什么主页不会立即显示变量,我只想将变量用于IF语句,该语句会打印出额外的无效登录消息。

我正在使用wampserver64进行局部主机。

我的.htaccess看起来像这样:

Options FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z\-\#]+)/?$ index.php?page=$1 [L]

编辑: cbroe的答案对我有帮助,我的htaccess没有QSA标志

this is my first stack overflow question.
I have a home page, where you can login, if you login it sends you to a login php in which you have some code that checks if login is incorrect. If that is the case this code redirects you back to home page:

header("Location: ".SERVER_ADDRESS."home?error=invalid_login");

Now you are back on the home page but the url variable is not set. It does not show in a
var_dump(parse_url($_SERVER['HTTP_REFERER'])); or
var_dump($_GET);

This is my index.php file:

<?php

session_start();
require_once("config.php");

if(!(isset($_GET['page']))) { // redirect to main page 
    
    header("Location: ".SERVER_ADDRESS."home");
    
} else {
    
    $mp = new MainPage($_GET['page']); // redirect to given page
    
}

?>

If you go try login again with wrong credentials and do the same redirection process it will now show the variable.
Same thing happens if I were to refresh the header after few seconds.

I seem to not understand why the home page would not show the variable straight away, I just want to use the variable for an if statement that would print an extra invalid login message.

I am using wampserver64 for localhosting.

My .htaccess looks like this:

Options FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z\-\#]+)/?$ index.php?page=$1 [L]

EDIT:
CBroe's answer helped me, my htaccess did not have a QSA flag

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2025-01-25 03:41:00

您的规则与 home 匹配,并将请求重写为 /index.php?page=home

您需要添加 QSA 标志 - 查询字符串附加 - 以获得您在此处创建的新查询字符串,并将其与任何现有查询字符串合并。

Your rule matches on home, and rewrites the request to /index.php?page=home.

You need to add the QSA flag - query string append - to get the new query string you are creating there, merged with any existing one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文