PDA

View Full Version : need help debugging php, please.


brianellisrules
Oct 22, 2003, 09:57 AM
<?php
$full_path = getenv("REQUEST_URI");
$file = basename ($full_path,".php");
include('head.inc');
include($file.inc);
?>

OK, here's what I'm trying to do. Let's say I have a file named "pics.php". There's a corresponding "pics.inc" file that would have all the html formatting. "head.inc" contains my header/banner/links/etc.

I'd like for the php to grab the name of the file "pics.php" and automatically know to use "pics.inc". I'm thinking this make my life easier because I'd have files like "ohio.php", "towelshirt.php", "complaint.php". Rather than going into each one of those files and changing the corresdponding *.inc name, I'd like to have it update automatically.

Is this making any sense?

To test this:
www.brianellisrules.com/test/ -> click on Pictorials. It's the only like I have setup to work like this. And it doesn't even work right.

sonofslim
Oct 22, 2003, 10:47 AM
first off, basename might not be working the way you want it to--this is no fault of your own.

basename(dir/whatever.ext) will return the filename, with extension, as expected; but to return a filename without extension using the suffix parameter, as you have with basename($full_path,".php") your server needs to be running php version 4.1.0. my server is running 4.0.6 and this wouldn't work for me either. so you need to strip off the extension in some other manner.

try this:
$filename = current(explode('.', basename($SCRIPT_NAME)));

here's what happens, from the inside out: basename($SCRIPT_NAME) does what it looks like, taking the basename (with extension, remember) from whatever page the command is on.

next, we use the explode command to break that result into an array, using the '.' as our break point.

finally, we use the current command to retrieve the current element of this array. since we haven't done anything to this array yet, the pointer is still on the first element -- just what we want, the first bit of that filename.

so: you've arrived at a good basename. now you need to append the correct .inc suffix. your command, include($file.inc) won't work because of that period. in php, the period acts as a concatenator; meaning that it appends one string to another. print('hello'.'world') prints: "hello world".

likewise, if your basename is "thisfile," include($basename.inc) asks the php engine to include "thisfileinc" because it just runs the two together. and without quotes, that inc isn't going to do any good, either, because php won't know it's a string. instead, use the concatenator like this: include($file.'.inc')

this will result in an include of filename.inc. see the difference?

so your complete code looks like this:
<?php
$filename = current(explode('.', basename($SCRIPT_NAME)));
include($filename.'.inc');
?>

placing this in anyfile.php will result in an include of anyfile.inc

hope this helps!!

sonofslim
Oct 22, 2003, 10:50 AM
one more thing i should definitely mention: a great, GREAT resource for php is php.net. to get more information on all things php, just go to php.net/[whatever] and it'll bring up a full reference for you.

try it out: www.php.net/basename (http://www.php.net/basename)

brianellisrules
Oct 22, 2003, 11:02 AM
Awesome!! Thanks for the help!!

I should've mentioned that at some point during my debugging process I added the command "print $file" as an intermediate step just to make sure itwas extracting the file name correctly (which it was).... so I guess that means my server has 4.1.0, eh? And since php is all server-side, I should be good to go, regardless of who visits the site, right?

And now that you mention it, I do remember reading yesterday in one of the tutorials that the "." is used to concactenate two strings... I should've remembered. Oh well. I had a sneaking suspicion it would be a *really* simple fix.

Thanks again!

sonofslim
Oct 22, 2003, 11:19 AM
phpinfo() will list the version as well as every other parameter--useful at times, but too verbose if you just want the version. echo phpversion(); will do the trick.

and yes, php is server-side; so if your code works, it'll work for everyone.

good luck!

brianellisrules
Oct 22, 2003, 04:11 PM
I've got another problem. I want to include a file that's not in the same directory... according to www.php.net/include/ this should work.

<?php
include('http://www.brianellisrules.com/test/head.inc');
include('stories.inc');
?>
Unfortunately, I get this error:

Warning: main(): stream does not support seeking in /home2/brian/public_html/test/stories/index.php on line 2

Does the error mean that the php isn't configured to allow that function? Or that it doesn't allow including of files from different directories?


EDIT: I forgot to mention that this php file would be stored in: http://www.brianellisrules.com/test/stories/

sonofslim
Oct 22, 2003, 04:38 PM
i don't know if you can include an absolute URL like that. (sorry, end of day, synapses clogged with cold coffee.) try pointing to the include file from the parent directory or your root. ie: include('../whatever/whatever/file.inc')

this is always one of those areas where i trip up, and have to spend some time just trying out various dots and slashes to see what points where. don't ask me why i can never keep it straight...

brianellisrules
Oct 22, 2003, 04:49 PM
another simple fix. thanks again.

Rower_CPU
Oct 22, 2003, 05:01 PM
Looks like I missed the fun earlier. ;)

Good stuff, sonofslim and glad you're moving along Mr. ber.

brianellisrules
Oct 22, 2003, 05:06 PM
Originally posted by Rower_CPU
Looks like I missed the fun earlier. ;)

Good stuff, sonofslim and glad you're moving along Mr. ber.
Haha...

I'm actually getting started with the big 'switch'.... so, I'm sure there will be plenty more 'fun' in the future.... :)

-brian

jelloshotsrule
Oct 22, 2003, 05:39 PM
Originally posted by ber.com
Haha...

I'm actually getting started with the big 'switch'.... so, I'm sure there will be plenty more 'fun' in the future.... :)

-brian

so the question that comes up, given where we are.... when will you make the ultimate "switch"??????

Rower_CPU
Oct 22, 2003, 05:40 PM
Aha...so Mr. Brian's a Windows user?

tsk, tsk, tsk...;)

brianellisrules
Oct 22, 2003, 05:45 PM
Uh.... well.... you see....

OH MY GOD, LOOK OUT BEHIND YOU!!!!




*runs away*

jelloshotsrule
Oct 22, 2003, 05:57 PM
just answer the question

you can lick my g5 next time you're here if that will help sell it... hah

or better yet... when we go to night of the panther in nyc friday.... hot!

brianellisrules
Oct 22, 2003, 06:59 PM
speaking of a website switch, this is getting UGLY.... a mix of php and straight up, all-html pages (because of things like the guestbook, etc). If I want to change my header at all, it's going to be a pain in the butt.

Rower_CPU
Oct 22, 2003, 08:38 PM
Yeah, sometimes it's forcing it to do everything when you don't have complete control over all the elements.

You just have to decide if it's worth the saved time and effort to have some PHP and then keep the few HTML-only pages, or if the mix will throw you off too much.