0

I have several folders on my domain, within each folder contains an index.php file that checks to see if the database connection passes or fails, if it fails, the page is redirected to a top level file (outside of all folders) called offline.php. This part works great. The basic format I'm using to redirect if the db is offline is:

if ( !$dbconnect ) {
header("Location: https://www.test.com/offline.php");
}

Then, within the offline.php page, I need to check to see which folder brought the user to the offline.php page, and display a unique message to the user - based on the folder that brought them to the offline.php page.

For example:

test.com/test1/index.php redirects to offline.php, the message would say 'test1 brought you to this page'

test.com/test2/index.php redirects to offline.php, the message would say 'test2 brought you to this page'.

In multiple browsers I've tried the following code, which always results in 'unknown uri':

$url = 'https://' . $_SERVER['HTTP_REFERER'] ;
if ( strpos($url,'test') !== false ) {
echo 'test';
} elseif ( strpos($url,'test1') !== false ) {
echo 'test1';
} elseif ( strpos($url,'test2') !== false ) {
echo 'test2';
} else {
echo 'unknown uri';
}

Suggestions?

EDIT Due to the unreliable nature of HTTP_REFERER I've decided to put all of the conditions within the index.php page and forget about the offline.php page. A HUGE thank you to everyone who offered suggestions!

gandjyar
  • 1,145
  • 2
  • 10
  • 15
  • I see you've added some stuff since I answered earlier. What happens when you just echo $_SERVER['HTTP_REFERER'], does anything show? – OrganizedChaos Feb 17 '14 at 21:52
  • nothing shows, just a blank page. – gandjyar Feb 17 '14 at 22:03
  • $_SERVER['HTTP_REFERER'] is unreliable, use with extreme caution –  Feb 17 '14 at 22:05
  • I would just start session in that error-generating page, put script name in some variable and redirect. Then this redirected page could start session and see if this variable exists by `if(isset($_SESSION['somevariable']))` – Kitet Feb 17 '14 at 22:11
  • I like Kitet's better than mine. I didn't think of storing it in a session variable, but it would be much cleaner IMO. – OrganizedChaos Feb 18 '14 at 01:13

2 Answers2

1

Why would you use redirects at all? They are heavy on the server, slow and just plain old unnecessary. Use a switch statement and have 1 controlling page instead of multiple folders and pages.

Adam
  • 11
  • 1
0

If you use the following code on your offline.php page, you can see all of the $_SERVER variables available (referring URL is in there)

echo '<pre>',print_r($_SERVER),'</pre>';

From there, you can take $_SERVER['HTTP_REFERER'] use a select case, or if then statement and accomplish your goal.

Based on some of your questions in the comments and people pointing out the use of $_SERVER['HTTP_REFERER'] being unreliable, you could do something like this instead.

On your index.php page with the dbconnect check, you could modify it to be something like this. header("Location: https://www.test.com/offline.php?org=".urlencode($_SERVER['REQUEST_URI']));

Then, on the offline.php,

$page = urldecode($_GET['org']);  
$org = explode('/',$page);

echo $org[1] to get the first value after the slash, $org[2] would get the next value etc..

OrganizedChaos
  • 431
  • 2
  • 11
  • Just tried this but the referring url was not included in the list, should it be listed? – gandjyar Feb 17 '14 at 21:37
  • It depends on the browser. In some browsers, the referer is the page that performed the redirect, in others it's the page that linked to the page with the redirect. – Barmar Feb 17 '14 at 21:39
  • See http://stackoverflow.com/questions/2158283/will-a-302-redirect-maintain-the-referer-string – Barmar Feb 17 '14 at 21:39
  • Ohh yeah, good call Baramar. user1172854, is it possible to adjust your script that redirects them to include the originating page in the url? That way it would be definitive where it's coming from and is cross-browser dependent. – OrganizedChaos Feb 17 '14 at 21:44
  • How best to include the originating page in the url? – gandjyar Feb 17 '14 at 21:51
  • I modified my answer to show one way to include the originating url. – OrganizedChaos Feb 17 '14 at 22:08