tags:

views:

46

answers:

3

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>
+4  A: 

$_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.

Matchu
+2  A: 

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.

lonesomeday
I vote for the hidden field solution since the user can have multiple tabs open with the same website.
Emil Vikström
i am new to php...i will try it...and get back to u ....thnx a lot...
Vinod K
Emil, that's a very good point. The only problem with it is that it can be spoofed, but that probably isn't a problem here.
lonesomeday
If the user wants to spoof which of the public pages they're coming from, then they're just messing themselves up, anyway.
Matchu
@Matchu, exactly right.
lonesomeday
+1  A: 

Just add a hidden input with the current absolute path to the page:

<input type="hidden" name="path" id="id_path" value="<?php echo $_SERVER['SCRIPT_NAME'] ?>

Or use the server variable HTTP_REFERER on the next page:

$path = $_SERVER['HTTP_REFERER']

Both the above can be tampered with so if it is important that it is correct save the path on the page in a session variable or in your database:

$_SESSION['PATH'] = $_SERVER['SCRIPT_NAME']

and retrieve it on the next page:

$path = $_SESSION['PATH']
sunn0
@sunn0 $_SERVER['SCRIPT_NAME']..do i need to give the full path inplace of 'SCRIPT_NAME'
Vinod K
No. The examples are just copy paste. $_SERVER['SCRIPT_NAME'] in PHP will give you the current path to your page like /hello/my-world.php
sunn0