Trying a require in a custom 404 page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tarview
Forum Newbie
Posts: 4
Joined: Mon Oct 29, 2007 7:44 am

Trying a require in a custom 404 page

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trying a require in a custom 404 page

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
tarview
Forum Newbie
Posts: 4
Joined: Mon Oct 29, 2007 7:44 am

Re: Trying a require in a custom 404 page

Post 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!
dayyanb
Forum Commoner
Posts: 46
Joined: Wed Jan 23, 2008 12:34 am

Re: Trying a require in a custom 404 page

Post 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?
Last edited by dayyanb on Sat Jan 26, 2008 3:46 pm, edited 2 times in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trying a require in a custom 404 page

Post by VladSun »

Code: Select all

<?php
$r = $_SERVER['DOCUMENT_ROOT'] . "/path/to/settings.php";
require( $r );
?>
Obviously - absolute paths.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Trying a require in a custom 404 page

Post 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.';
}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Trying a require in a custom 404 page

Post by JAM »

Could also play around with getcwd().
Post Reply