Forum Moderators: coopster

Message Too Old, No Replies

php redirect

         

JohnnyEng

7:38 pm on Jul 24, 2006 (gmt 0)

10+ Year Member



Hello there,

I was wondering if anyone had any ideas on how to accomplish this bit of php programming.

What we need is to essentially protect a directory with an interim page. The interim page would have a form asking a yes/no question.

For example:

What we have now..
/ --> /protected_dir/

turns into this:
/ --> interim page (with form) --> /protected_dir/

Optimally, when a person requests a file in the protected directory they would be redirected/prompted by the page with the form on it. Once they answer the question and hit "Submit" they would then be redirected to the page they intially requested.

I've tried .htaccess and $HTTP_REFERER but have had no luck. The $HTTP_REFERER is always the page with the form.

Thanks much. Any advice is GREATLY appreciated.

johnnyeng

barns101

9:13 pm on Jul 24, 2006 (gmt 0)

10+ Year Member



If I understand correctly, you could use PHP sessions. Stick something like this at the top of each page in the protected directory:


<?php
session_start();

if(!$_SESSION['answer'])
{
// Display form asking the question
}
else
{
// Show the protected page
}
?>

JohnnyEng

2:46 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



Thanks Barns101, I think the little snippet you passed along is going to help me solve the puzzle for the HTML/PHP files in the protected directory.

Alas, there are PDF's in the directory as well. Do you have any ideas on how to accomplish the goal when PDF's enter the equation? Perhaps an Apache Redirect or .htaccess? I'm stumped!

Thanks much. :)

johnnyeng

JohnnyEng

6:09 pm on Jul 26, 2006 (gmt 0)

10+ Year Member



I think I almost have it!

By using a combination of .htaccess and $_SERVER['PHP_SELF']; I've managed to echo out the path. I've made a few changes though.
Instead of a form, I now have an image map that counts clicks.

When a user requests a file in the /protected_dir/ they are redirected to a page called 'answer.php', which displays an image map.
Once they click YES/NO, the choice is recorded in MySQL and they "should" go to the page they initially requested....except for one thing. I can't get the 'href' correct.

Here is the code I've been working with:

<?
$referer=$_SERVER['PHP_SELF'];
?>

<map name="Map" id="Yes">
<area shape="poly" coords="96,29,101,66" href="<?php echo $referer;?>" />
<area shape="poly" coords="107,81,126,86,158,86,161,86,180,89,184,42,97,28" href="<?php echo $referer;?>" />

When $referer is echoed out, it looks like this:
/answer.php/protectedfile.php

So I ask this:
Is there any way to replace /answer.php/protectedfile.php
with
/protected_dir/protectedfile.php
when it's echoed out?

Thanks much. I hope that makes sense. :)

johnnyeng