Page 1 of 1

Trying a require in a custom 404 page

Posted: Fri Jan 25, 2008 2:05 pm
by tarview
HI all,

I am attempting to create a custom 404 page that redirects the user to a search result based on what the script thinks they were looking for. I have everything set up and it all works great, except for one thing.

I would like to have all the configuration performed externally, including the path to the search script. So, for instance, if my search script is in the "search" subfolder on my hosting account, the configuration (settings.php) would say something like:

Code: Select all

<?php
define( "_DOMAIN_", "www.example.com" );
define( "_SEARCHPATH_", "/search/" );
?>
And in the custom 404 page (called 404.php), I would like to require this settings file so I can refer to these constants

Code: Select all

<?php
$r = $_SERVER['DOCUMENT_ROOT'] . "/path/to/settings.php";
require( $r );
?>
But this isn't working. If I set the constant within 404.php, it works (obviously), so I know it's got something to do with putting require in there, but I don't know how to solve it.

Oh, and if I echo $r, it gives me the correct full path (/home/tarview/public_html), so I know that part should be fine. Or is it? Could I be missing something there?

Re: Trying a require in a custom 404 page

Posted: Fri Jan 25, 2008 2:57 pm
by VladSun
tarview wrote:But this isn't working.
What exactly do you mean by that?

Error messages, logs ?

I couldn't reproduce your problem.

Re: Trying a require in a custom 404 page

Posted: Fri Jan 25, 2008 3:08 pm
by tarview
Hmm...apparently neither can I. :?

It was telling me that the path (to settings.php) was incorrect. Now it isn't. :banghead:

I think I need to unplug for a while. Thanks though!

Re: Trying a require in a custom 404 page

Posted: Fri Jan 25, 2008 7:57 pm
by dayyanb
tarview wrote:Hmm...apparently neither can I. :?

It was telling me that the path (to settings.php) was incorrect. Now it isn't. :banghead:

I think I need to unplug for a while. Thanks though!
Are you using relative links instead of absolute?

Re: Trying a require in a custom 404 page

Posted: Sat Jan 26, 2008 10:48 am
by VladSun

Code: Select all

<?php
$r = $_SERVER['DOCUMENT_ROOT'] . "/path/to/settings.php";
require( $r );
?>
Obviously - absolute paths.

Re: Trying a require in a custom 404 page

Posted: Sat Jan 26, 2008 6:38 pm
by Jonah Bron
You might try

Code: Select all

if (file_exists($_SERVER['DOCUMENT_ROOT'] .'/path/to/settings.php')){
 echo 'Yes, file exists';
}else{
 echo 'Nope.  Bad address.';
}
if (file_exists($_SERVER['DOCUMENT_ROOT'] .'path/to/settings.php')){
 echo 'Yes!  This other one works!';
}else{
 echo 'No. Neither of them work.';
}

Re: Trying a require in a custom 404 page

Posted: Sat Jan 26, 2008 7:14 pm
by JAM
Could also play around with getcwd().