Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 26-06-2008, 11:38 PM   #1 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default checking page referer in PHP


I have a page say result.php, which must be displayed only after a visitor fills in a form (reg.php). Even if the user visits result.php by typing the url, he must be directed to the registration page (reg.php). I am using page referer ($referer = $_SERVER['HTTP_REFERER']; )to see if the user came via the registration page or not.

This code works fine, but there is a problem. In the result.php page there is form which uses <?php echo $_SERVER['PHP_SELF']; ?>" method="post" to reload the page. At this stage the page checks once again if it has come from the refrerer and sends the page back to the registration page.

To check this I am using this script in the head of the html PHP Code:
PHP Code:
<?php 
if (!isset($ok)){
 
$referer $_SERVER['HTTP_REFERER']; 
 if (
$referer != 'reg.php'){ 
  
header('Location: reg.php'); 
 }else{
 
$ok=1;
 }

?>

If the user visits result.php for the first time, script would check if $ok is set, if it is not, he is directed to reg.php. When the user fills in reg.php and is directed to result.php, the script checks $ok (which is not set) but is directed from the correct location, so $ok is set to 1 and the page loads normally.

when the page reloads via a form, the page checks if $ok is set (actually it has been set to 1) and the page should load normally. But this is not happening, it is getting directed back to reg.php.
What is wrong?

It is still not working
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-06-2008, 12:03 AM   #2 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: checking page referer in PHP

Possible reason for the error is because referrer is not 'reg.php' but its 'http://www.domian.com/ref.php'.

Just a couple of suggestions:
Don't rely on the referrer.
1. The browser may not always send the referrer.
2. The referrer may be spoofed or be purposely set to send the wrong information.

This may lead to a genuine submission being redirected to the form filling page and the form may never be submitted successfully.

Instead use sessions. Set a session variable. Set a session variable on the page that contains the form. On the form processing page, check if that session variable is set and let the process proceed accordingly.

Its very easy to integrate sessions into your script.
At the top of your form page, simply add the below snippet. The rest of the code on the page should remain unchanged.
PHP Code:
<?php
session_start
();
$_SESSION['allow_form_to_be_submitted']=1;
?>
Now add the below snippet to the form processing page. Even here, the rest of the content remains unchanged.
PHP Code:
<?php
session_start
();
if(
$_SESSION['allow_form_to_be_submitted']==1)
{
//process the inputs of the form
}
else
{
//redirect the user to the form filling page.
}
?>
Again, remember to add them to the 'top' of the page. Adding them anywhere in between may result in an ugly error.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 27-06-2008, 06:02 AM   #3 (permalink)
MMO Addict
 
amitava82's Avatar
 
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
Default Re: checking page referer in PHP

Here is how you can achieve this. Say, in your reg.php page you have form1 and with a button name 'submit1' and in result.php page you have 2nd form with button name 'submit2'.
PHP Code:
<?php
//Check if form1 was submitted
if (isset($_POST['submit1'])){
// If yes, do validation and process form data..
    
}
else {
// If form 2 is not submitted i.e., user directly landed on this page
if (!isset($_POST['submit2'])){
//redirect to reg.php page
    
header('Location: reg.php');
    exit;
     }
//Process form 2
}
?>
So, when a user directly lands on result.php page, since the form on this page was not submitted, he/she gets redirected to reg.php page. Now here are couple of things you should do:

1. Process any data you receive from forms and validate them. It's very important!
2. If you want to process the data received from both the forms together, its necessary to store the values as session data or variable.
3. If you are using session variable, DO destroy them [session_destroy();] whenever necessary. If you try Rohan's example, you will see that at the first try user will be redirected to reg.php page but in subsequent tries users will NOT be redirected to reg.php page because session variable is still set to 1
__________________
Steam Profile || Personal Page
Warp drive active. Approaching stargate.
amitava82 is offline  
Old 27-06-2008, 08:55 AM   #4 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: checking page referer in PHP

Thank you guys for the reply. I think I had left out important info in my thread, please go through this and let me know if your solution is still the same.

The first page where the user fills in registration detail is not a php file as I mentioned, it is reg.htm file with no php script running in it. My server is windows based and does not handle php for sending forms to email, hence I am using vbscript to handle this.

Code:
<form name="test" method="post" action="scripts/cdosys.asp">
<input name="_redirect" type="hidden" value="http://www.xxx.com/result.php" />
This script sends the user input to my email and redirects to result.php.
Will the submit button in reg.htm be recognized in the result.php as $_POST['submit'], can your suggestions work in this scenario?
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Old 27-06-2008, 06:59 PM   #5 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: checking page referer in PHP

I don't know anything about ASP, but as far as your .htm extension is concerned, you can change that to .htm.php
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to open preview page within html page? Sridhar_Rao QnA (read only) 1 08-04-2008 06:49 PM
Checking the WGA version Devrathnd Software Q&A 3 19-04-2007 07:37 PM
Checking SMPS moxy123 Hardware Q&A 8 27-05-2006 09:52 PM
Checking the RAM devilhead_satish QnA (read only) 2 15-04-2005 01:55 AM
MS WORD Page Numbering (ommiting 1,2,3 page) Any body Butterfly Software Q&A 1 24-03-2005 12:48 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:04 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2