1

I created a PHP site that has 3 pages, i.e., A, B, and C. Both A and B will call page C first by using "require_once". Is there a way for page C to know if this call came from A or B?

$_SERVER['HTTP_REFERER'] didn't work.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
Gary
  • 483
  • 2
  • 6
  • 17
  • Out of curiosity, why exactly do you need to know? And how much do you need to know? Do you only need to differentiate between the two or must you be able to pinpoint which one exactly? – Wug Jun 25 '12 at 18:06
  • put a tag in A and B, like $tag = "A.php"; – nullpotent Jun 25 '12 at 18:08

3 Answers3

2

A.php

$page = 'A';
require_once 'C.php';

B.php

$page = 'B';
require_once 'C.php';

C.php

echo 'I was required by'.$page;
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • This means `$page` variable needs to be available on all three pages. – Blaster Jun 25 '12 at 18:09
  • Yes, same for the require() call – Damien Pirsy Jun 25 '12 at 18:09
  • Your solution is only applicable if the number of files is small. Otherwise, it takes too long to include the initialization of $page in each file... – Jocelyn Jun 25 '12 at 18:22
  • It takes the same amount of time of using the require_once(); if the number of pages grows bigger OP could go for another approach, something like a "fron controller", which will handle the $page determination as well. Anyway, simple answer for simple question ('site that has 3 pages') – Damien Pirsy Jun 25 '12 at 18:25
2

You can use debug_backtrace function in c.php
You will get caller file and caller line nos as well.

$db =  debug_backtrace();

echo "Calling file: ". $db[0]['file'] . ' line  '. $db[0]['line'];
Mahesh Meniya
  • 2,627
  • 3
  • 18
  • 17
0

Try $_SERVER['REQUEST_URI']. Per php.net,

URI provides the entire request path (/directory/file.ext?query=string)

aynber
  • 22,380
  • 8
  • 50
  • 63
  • Thanks. This is the way i was looking for cuz i didn't want to put any extra variable in the calling pages. Sorry i don't have enough reputation to vote up. – Gary Jun 25 '12 at 19:59