To get previous page URL in php

I have 5 php pages which are questionnaires (MCQ).

the user is provided with 1 of the documents ... which he answers and sends ... then he goes to AnsCheck.php ... in AnsCheck.php, I need to understand from which page, from which of 5 documents the request was received, so that I could continue checking ... how can I get the page where I received the request from?

---- 1.php ----

<?php (E_ALL & ~E_NOTICE); session_start(); // is the one accessing this page logged in or not? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) { // not logged in, move to login page header('Location: login.php'); exit; } ?> <html> <head> <title>My Page</title> </head> <body> <form name="1" action="/NewDir/AnsCheck.php" method="POST"> 1.Name the owl of harry potter. <div align="left"><br> <input type="radio" name="paper1" value="op1">Mr Barnesr<br> <input type="radio" name="paper1" value="op2" checked> Wighed<br> <input type="radio" name="paper1" value="op3"> Hedwig<br> <input type="radio" name="paper1" value="op4"> Muggles<br> <input type="submit" name="submit" value="Go"> </div> </form> </body> </html> 
+4
source share
2 answers

$_SERVER['HTTP_REFERER'] contains the link page. (And, yes, it is spelled incorrectly in PHP, because the HTTP specification is spelled incorrectly. See the picture.)

However, regardless of whether this header is sent, sometimes this is an option in the browser that some users have disabled, and indeed older browsers do not even support it, so depending on this it can be problematic.

Your code will have a better chance of working for more users if you simply add a hidden field to each of these 5 forms indicating which form.

+22
source

You are already using sessions. I would use them again here:

 $_SESSION['last_question'] = 1; 

Then you can check it out in AnsCheck. Alternatively, you can put a hidden field in your form:

 <input type="hidden" name="question" value="1"> 

And then check the value of this in AnsCheck with $_POST['question'] .

Both are more reliable than HTTP_REFERER , which is not provided by all browsers.

+3
source

All Articles