Home Page
Archive > Posts > 2009 > February
Search:

PHP file inclusion weirdness
PHP has its own quirks too
In PHP, you cannot include files in parent directories “../” from a file that has already been included from another file in a different directory. This has been a nuisance for a long time.

Here is a test case: (Files followed by their code)

/test1.php (This is the file that is called directly by the browser/apache)
<?
//This all works fine
print 'test1'_start;
require('foo/bar/test2.php');
print 'test1_end';
?>

/foo/bar/test2.php
<?
print 'test2_start';
require('blah/test3.php'); //This works fine because the include file it is in a subdirectory, not a parent directory of test2.php
require('../test4.php'); //This does not call fine (an error is thrown by php) because it is in a parent directory relative to test2.php, which was already included from the parent file (test1.php) in another directory (/). To fix this, use 'foo/test4.php'
print 'test2_end';
?>

/foo/bar/blah/test3.php
<? print 'test3'; ?>

/foo/test4.php (This file is not reached by this example without the fixes mentioned in either the comment in test2.php, or below)
<? print 'test4'; ?>

The obvious method to fix this whole conundrum is to always set all includes relative to one root path, and then make sure that path is always used with the set_include_path function if your parent file is not in the root directory. For example:
set_include_path(get_include_path().':/PATH_TO_ROOT_DIRECTORY');

Another method would be to write a require/include wrapper that calculates paths from the current directory whenever a parent path “../” is used. Here is an example of this:
function _require($IncludeFile, $CurrentFile)
{
	$CurrentPath=preg_replace('/[^\/]+$/', '', $CurrentFile); //Remove the filename to get the current path
	require(realpath("$CurrentPath$IncludeFile"));
}

This method is called with 2 parameters: the relative path from the current include file to the file you want to include, and __FILE__
For example, line 4 of “/foo/bar/test2.php” above would now be:
_require('../test4.php', __FILE__);

The first line of the _require function could also be removed by using the __DIR__ constant (instead of __FILE__) which was added in PHP 5.3.0.
Google Search Removed Functionality
Has Google been removing search options to make things run faster?

I’ve been meaning to get searching working on my site for what seems like forever, and I decided to finally get around to getting some manner of search working via the temporary “use Google” solution. Unfortunately, it seems Google no longer does boolean searches completely properly as advertised. I am sure Google Search still supports boolean logic (as opposed to the assumed “and” between each word) because the Advanced Search, linked to from their front page, still has it, and it returns a few of the results it should.


As an example:
If I wanted to search the Projects and Updates sections of my sites for either the keywords fractal or font I would use the following search:
(site:www.castledragmire.com/Projects OR site:www.castledragmire.com/Updates) AND (Fractal OR Font)

This currently only returns 3 results, when it should return 11 different results, enumerated by using the 4 separate searches (with return results):
  1. site:www.castledragmire.com/Projects Fractal
  2. site:www.castledragmire.com/Projects Font
  3. site:www.castledragmire.com/Updates Fractal
  4. site:www.castledragmire.com/Updates Font

Actual Google returned results: A simple example of this through the Google Advanced Search Page is as follows:

Fractal OR Font site:www.castledragmire.com/Projects [Advanced Search]

Which only returns 3 results (following) instead of the 6 (see above) that it should:

Because of this, I need to go ahead and get real searching up via MySQL (or possibly another solution), as originally planned, sooner than later, since Google will not work as a temporary solution for what I want.


I wrote up a paper on what could be done through Google Search over 5 years ago as a job request [to be posted soon], which I believe is very informative. I’m sure it’s a little outdated, but it shows how much can Google can [could] do for you.

No Phone Signal at the Grand Canyon
“Can you hear me now?” ... Thor, did I just do that clichéd line? x.x;

Hmm, I’m currently stuck at an outlet mall between Las Vegas and Hollywood for 3 or so hours while the highway is closed down. They are apparently blowing up a chunk of a mountain so they can expand the highway (I-15 South). My vacation has been very event filled so far, with barely enough time to get stuff done, so I figure while I’m stuck here, I might as well get one of the posts I’ve been meaning to do out of the way :-).

The highlights of my vacation so far have included:
  • 2 days at Disney Land, which was lots of fun :-)
  • 3 days in Las Vegas which included:
    • A little gambling
    • Seeing lots of casinos and free shows
    • My travel companion’s wedding
    • Destroying my ankles from walking to each casino to catch all the shows in time. Each Casino is 3-6 blocks long and has so much in them, it feels overwhelming! There’s just too much to see!
  • The Grand Canyon, which was magnificent, majestic, and breathtaking. We also crossed over the Hoover Dam on the way there.
  • We are now heading to Hollywood for the rest of the vacation ^_^.
I am still kind of anxious to get back home, see my kitty, and resume normal life though. This is a lot of activity for an extended period of time for me.

Anyways, Verizon has failed me for the first time ever in the US :-(. I got no signal at the Grand Canyon, doh. That’s all I had to report though, heh. And I’m not really complaining, because Verizon still gets far better signals in far more places than the other phone services I’ve tried.