tags:

views:

34

answers:

4

I have two page: rooms.php and reservation.php User can only access to reservation.php via rooms.php I have used define('NOT',1); and if (!defined('NOT')) exit('no direct!'); But when user goes from rooms.php to reservation.php there is error message? Why define function not working? Thanks in advance

+1  A: 

Assuming you are calling rooms.php first and go to reservation.php via a link:

The constant will be lost after you access the other page. You should store the value in a session:

session_start();
$_SESSION['NOT'] = 1;

A PHP file a script that gets executed every time you call it. But that also means that all variables, constants are lost after the script was executed. But sessions are for preserving data through multiple page calls.

Felix Kling
The square brackets are broken Markdown links. I think this is what he means.
Pekka
@Pekka: Oh ok thanks.
Felix Kling
A: 

in your rooms.php add the following code before the inclusion of reservation.php

define('FROM_ROOM',true);

Within Reservation.php at the very top add

if(!defined('FROM_ROOM')){ exit("Please go away");}

So it looks like this

rooms.php: define('FROM_ROOM',true); include 'reservation.php';

reservation.php:

if(!defined('FROM_ROOM')){ exit("Please go away");}
echo "Im reservation.php, i only can be include by rooms.php";
RobertPitt
Im not include reservation.php, Im going from rooms to reservation
phpExe
Then you need to use Sessions, DEFINE is used to secure files from being directly accessed, not to allow certain html depending on the referer.
RobertPitt
Yes you are right, As pekka said, have a misunderstanding about this issue. Thanks for help.
phpExe
+2  A: 

You may have a misunderstanding here: This method works only when rooms.php includes reservation.php using include() or require(). It does not work for, say, referers.

Is that what you are doing? In that case, your code looks correct. rooms.php needs to contain

define('NOT', 1);

before the line where reservation.php is included; but that should work.

Pekka
A: 

you don't need constants you need session variables or to check the HTTP referrer; constants will only work if you are including reservation.php and you're not, you're simply stipulating that the user should have seen rooms.php before seeing reservation.php

nathan