Forum Moderators: coopster

Message Too Old, No Replies

php include if page exist

         

gearupandplay

11:41 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Im having trouble with this script.

<? $incl = "/home/example/public_html/phpfiles/{$subCat}.php";
if (is_file("$incl")) { include($incl);
} ?>

It works on part of my site but another part that has diffirent template and URL structure it dont work.

example: it works here:
<snip>

but not here:
<snip>

I would just like a script that checks if the last part of the URL is Clothing.html or whatever it is and include a file of the same name if it does.

Basically I just want to include files only on certain pages and not every page. Thanks, I'm pretty new to all this

[edited by: dreamcatcher at 8:20 am (utc) on Mar. 18, 2009]
[edit reason] No personal urls, thanks. [/edit]

penders

11:49 am on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<? $incl = "/home/example/public_html/phpfiles/{$subCat}.php";
if (is_file("$incl")) { include($incl);
} ?>

This is OK. But have you defined $subCat?

You are using PHP short tags (which won't work on all servers) - it might be better to use regular PHP tags: <?php ... ?>

Also, you don't need to include the "..." (double quotes) on your 2nd line:

if (is_file($incl)) {....}

is sufficient.

whoisgregg

3:29 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, gearupandplay!

How are you defining $subCat? The snippet of code which defines that may be helpful in diagnosing the issue.

BTW, you might also want to use the superglobal $_SERVER['DOCUMENT_ROOT'] instead of hardcoding the absolute path.

Edited Fixed my typo, thx penders!

[edited by: whoisgregg at 3:53 pm (utc) on Mar. 18, 2009]

penders

3:48 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



BTW, you might also want to use the superglobal $_SERVER['REMOTE_ADDR'] instead of hardcoding the absolute path.

I think you mean $_SERVER['DOCUMENT_ROOT'] ?

whoisgregg

3:52 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I did. Thanks for catching that, penders. :)

gearupandplay

6:05 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



How are you defining $subCat?

I'm really new to this so I hope I'm answering this correct.
The {$subCat} changes based on whats at the end of the URL.
I have a folder called phpfiles where I put a bunch of php files. For instance if the URl was

ttp://www.sitename.com/store/gear-store

I would have a php file in the phpfiles folder called gear-store.php that would be called when on this URL

It works on part of my site but I'm trying to make it work on another part that has diffirent URL structure and template.
the URL is longer like this.

ttp://www.sitename.com/mf/shop.php/S-731/Kids_Pants.php

Ive tried making a phpfiles folder in the mf folder and make a call to a file called Kids_Pants.php but it don't work. Maybe I will try adding the full tags and see if it works. Also, what would the mainCat be here? mf?

thanks guys

penders

10:43 am on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The {$subCat} changes based on whats at the end of the URL.

But how exactly are you doing this? ...presumably this is being set prior to your code above?

One way you might do this would be:

$subCat = basename($_SERVER['PHP_SELF']);

Followed by the rest of your code (and using whoisgregg's suggestion to use DOCUMENT_ROOT):

$incl = $_SERVER['DOCUMENT_ROOT'].'/'.$subCat;  
if (is_file($incl)) {
include($incl);
}

This is assuming your original URL is of the form:
http://www.sitename.com/store/gear-store.php
(presumably it does end in ".php"? Was that a typo?)

$_SERVER['DOCUMENT_ROOT'] returns the server path to the root of your website - the path to your public_html folder. (ie. probably '/home/example/public_html' in your case.) It saves you having to hardcode this path in your pages - which would be a big headache if you later move your site to another server!

Ive tried making a phpfiles folder in the mf folder and make a call to a file called Kids_Pants.php but it don't work. Maybe I will try adding the full tags and see if it works. Also, what would the mainCat be here? mf?

Having a phpfiles folder in the mf folder should work in principle - you'll need to adjust your include accordingly (to include the mf folder). Or just leave it as before and have your phpfiles folder off the web root (in your public_html folder).

What do you mean by 'mainCat'? The folder path?

Using the full tags (<?php...?>) won't actually make a difference in this instance, if short tags (<?...?>) are currently allowed. It's just that short tags are not allowed on all servers.

[edited by: coopster at 11:32 am (utc) on Mar. 19, 2009]
[edit reason] Disable graphic smile faces [/edit]