0

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

4
  • 2
    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. Commented Mar 30, 2022 at 6:08
  • 1
    That alone would not explain how it works the second time or after a refresh though. Maybe you have an additional caching issue. And # in your rule pattern character class is pretty superfluous by the way, you will never encounter that character in a URL path. (RewriteRule only matches against the path component of the URL.) Commented Mar 30, 2022 at 6:10
  • It does rewrite to /home but the second time that is also the case, which could mean that it could potentially be a caching issue? This is a var_dump(parse_url($_SERVER['HTTP_REFERER'] after the initial login: 'scheme' => string 'http' (length=4) 'host' => string 'localhost' (length=9) 'path' => string '/gra/home' (length=9) after second time: 'scheme' => string 'http' (length=4) 'host' => string 'localhost' (length=9) 'path' => string '/gra/home' (length=9) 'query' => string 'error=invalid_login' (length=19) Commented Mar 30, 2022 at 12:00
  • 1
    This page must not be resolved by a comment and an edit of the question. SHNXG or @CBroe should post an answer so that this page is resolved officially/properly. (Also, all comments containing relevant question details should be removed. All irrelevant comments (including mine) should be removed.) Commented Mar 30, 2022 at 23:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.