Wern Ancheta
Wern Ancheta

Reputation: 23317

how to know what files called for a php file

Here's what I want to do. I have 3 php files, one is a.php, b.php, and c.php. If a.php has a link to b.php, by using an href tag or by is using b.php as its form action. How would b.php know that it is a.php who is calling it? c.php is also linked to b.php. But I want to redirect the page to something else if it is not a.php who is using b.php.

I'm thinking of something like this for b.php,i'm just not sure how to do it in actual php code:

<?php

if(called_by('a.php')){
echo "something";
}
else{
header('location:a.php');
}

?>

Upvotes: 1

Views: 220

Answers (3)

Ditto
Ditto

Reputation: 88

What about $_SERVER['HTTP_REFERER']?

Edit: Guess someone beat me to it

Upvotes: 1

Reese Moore
Reese Moore

Reputation: 11650

If I am understanding you correctly, it sounds like you are trying to get the http referrer, for which you should look at the $_SERVER['HTTP_REFERER'] variable.

Upvotes: 1

RageZ
RageZ

Reputation: 27323

The $_SERVER['HTTP_REFERER'] contain the url where the user came from.

You have to note this is sent by the user browser so it's easy to be forged, so it's not secure. Also some browsers might not send that header so it might be empty time to time.

One other possibility would be to use the session and set a flag on the page like:

$_SESSION['come_from'] = 'a';

The session solution would be more secure.

Upvotes: 3

Related Questions