The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 10:45:18 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: What is the proper way to do includes in php?  (Read 3086 times)
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« on: January 06, 2008, 06:31:15 AM »

What is the proper way to do includes in php? In coldfusion I can include a file to a page like this:

Code:
<cfinclude template = "/include/com.cfm">

It wouldn't matter what directory the calling page is from, the code would go out to root directory of the domain go back up the tree to the directory include and get the include file. Simple one little line of code.

I can't get that to work in php. I did come up with the following code that does work on my test site. My test site is being hosted by godaddy. It doesn't work on my customer's site, which is on hostigator. The commented out line is what I had first, but I didn't see that variable on hostigator when I did a <? phpinfo(); ?>.

Code:
// $my_path = $_SERVER["SCRIPT_URL"];
$my_path = $_SERVER["SCRIPT_NAME"];
$pathArray = pathinfo_im($my_path);
$thisFileName = $pathArray[basename];
$directoryName = $pathArray[dirname];
if ($directoryName !=  "/") { $d = "../include";$a = $directoryName;} else { $d = "./include";$a = "home";}
include "$d/com.php";

Seems kind of convoluted compared to coldfusion. What am I missing here?

Thanks,
Tom
Logged

No links in signatures please
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: January 06, 2008, 12:52:28 PM »

It should by default use the directory that the script is running from as your base directory. If it's not, you can get it by doing

Code:
<?php
$currentDirectory 
getcwd();
?>

Logged

hai
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #2 on: January 07, 2008, 02:14:38 PM »

Thanks, that worked. What I am finding is the problems lie in the fact that godaddy (my hosting plan) uses version 5 of php while hostigator (my customer's) uses 4.4. A lot of the functions I found in the php online manual are only available with version 5. Says so at the top of the each function description page. Of course I didn't see that at the beginning of this project. So I am having to search for alternatives. I haven't given up, I am getting there.

Tom
Logged

No links in signatures please
ratthing
Journeyman
***
Offline Offline

Posts: 75


View Profile
« Reply #3 on: January 09, 2008, 11:22:14 AM »

You may be able to get hostgator to use PHP 5 by adding this in your .htaccess file:

Code:
AddHandler x-httpd-php5 .php .phtml .phps .php5

My host uses PHP 4 by default, but by adding that I was able to use PHP 5.

=RT=
Logged
Phaėton
Lifer
*****
Offline Offline

Posts: 555


⎝⏠⏝⏠⎠


View Profile
« Reply #4 on: September 15, 2010, 02:47:46 PM »

Is it different on every host?

that didnt work on mine i had to use this:

AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php

Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
Kovacs
Rookie
**
Offline Offline

Posts: 30



View Profile
« Reply #5 on: September 15, 2010, 04:12:32 PM »

Two easy ways to work around path madness are to define an 'includes' directory in php.ini, and also to make some kind of global path config file (in your includes dir).  Mine goes in includes/z_global_config.php and it just contains some simple path variables like:

$z_home_dir = '/home/kovacs';
$z_work_dir = '/home/kovacs/work';
$z_logs_dir = '/home/kovacs/logs';

It makes stuff easy to deploy because when you're uploading to the server, all you have to do is change your base paths in your config file and everything else should still work as is.  You can also put in other platform specific stuff, eg the path to your shell if you're a fan of using exec / passthru etc.
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: September 15, 2010, 04:50:47 PM »

I use Include sitefiles/*.inc so that below my Apache config dirs (the machines are all mine) I have individual files for each virtual host.

Then simply changing a site file from .inc to .site (for example) and cycling Apache, that site disappears. I also know that when I am dicking with a configuration I am only messing with that one particular site, rather than the whole configuration. Apache config includes rock.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!