Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To obtain the previous page's URL in php

Tags:

php

i have got 5 php pages which are question papers (MCQ's).

the user is provided with 1 of the papers...which he answers and submits...it then goes to AnsCheck.php ...in AnsCheck.php i need to understand from which page i.e from which of the 5 papers the request was received so that i can proceed with the checking ...how do i obtain the page from where i received the request?

----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>
like image 387
Vinod K Avatar asked Nov 29 '22 04:11

Vinod K


2 Answers

$_SERVER['HTTP_REFERER'] contains the referring page. (And, yes, it is spelled wrong in PHP because it is spelled wrong in the actual HTTP spec. Go figure.)

However, whether or not that header is sent is sometimes an option in the browser which some users disable, and the really old browsers don't even support it at all, so depending on it can be problematic.

Your code will be more likely to work for more users if you simply add a hidden field to each of these 5 forms indicating which form it is.

like image 137
Matchu Avatar answered Dec 06 '22 06:12

Matchu


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

$_SESSION['last_question'] = 1;

You can then check this in AnsCheck. Alternatively, you could put a hidden field into your form:

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

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

Both of these are more reliable than HTTP_REFERER, which is not supplied by all browsers.

like image 31
lonesomeday Avatar answered Dec 06 '22 05:12

lonesomeday