Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How does "include" really work?
Old 08-25-2007, 10:41 PM How does "include" really work?
Nathand's Avatar
Extreme Talker

Posts: 233
Location: USA
Trades: 0
I'm confused with the "include" function in php. I assumed that the code would be included into the file that called it in realtime. But apparently I'm wrong. For example; I have a file named "example.php" located at "example.com/dir/" and it's contents are:

PHP Code:
echo($_SERVER['SCRIPT_NAME']); 
And I include that file into another file that's located at "example.com/anotherdir/" like this:
PHP Code:
<?php include"example.com/dir/example.php" ?>
I would expect "dir/anotherdir" to be echoed. But I keep getting "dir/" instead. I'm getting the location of the included file, instead of the location I want?

Last edited by Nathand; 08-25-2007 at 10:46 PM..
Nathand is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-26-2007, 12:58 AM Re: How does "include" really work?
coolkbk585's Avatar
Be good this Christmas!

Latest Blog Post:
KBlog has been deativated
Posts: 642
Name: Kyle
Location: Ada, MI
Trades: 0
that would be

PHP Code:
<?php include("example.com/dir/file.php"); ?>
__________________
<?php if($Adsense_Revenue > 0): define('HAPPINES','100%'); else: define('HAPPINESS', '0%') endif; ?>
coolkbk585 is offline
Reply With Quote
View Public Profile Visit coolkbk585's homepage!
 
Old 08-26-2007, 07:17 AM Re: How does "include" really work?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
thats because i think the echo s probally being executed and then included like
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-26-2007, 08:50 AM Re: How does "include" really work?
Ultra Talker

Posts: 483
Trades: 0
I think the reason you're seeing this is because the variable you are using is kind of special.

The biggest problem is that even though when you include a file it executes it from the same directory as the script that is including it, it still knows what and where the included file was / is. $_SERVER['SCRIPT_NAME'] is one of those variables almost made specifically for use in included files.

The way you think the includes works is pretty much correct, it's just that the variable you are using is designed to work a little 'counter' to that.

If it were me, I'd just suck it up and set a variable in the including file, then echo it in the included file.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 08-26-2007, 09:19 AM Re: How does "include" really work?
Nathand's Avatar
Extreme Talker

Posts: 233
Location: USA
Trades: 0
The problem is I'm trying to make this included file dynamic. I want to be able to call it from anywhere on my site, and depending on where it was called from I want it to behave a little differently. That's why I was trying to what directory it's in.
Nathand is offline
Reply With Quote
View Public Profile
 
Old 08-26-2007, 01:55 PM Re: How does "include" really work?
Nathand's Avatar
Extreme Talker

Posts: 233
Location: USA
Trades: 0
Arrrrrg........ I tried what you said TwistMyArm, but can't get it to work

When I use:
PHP Code:
<?php include ("http://www.2008.huntingtonrobotics.org/header.php"); ?>
<?php $Loc 
"/media/index.php"?>
<?php 
include ("http://www.2008.huntingtonrobotics.org/media/media.php"); ?>
The "$Loc" is empty. But if I use:

PHP Code:
<?php include '../header.php'?>
<?php $Loc 
"/media/index.php"?>
<?php 
include './media.php'?>
It works. However one of my pages is nested very very deep in directory's, and I don't want to have to go "../../../../../../../../" etc.. Why is this happening? Is it a bug in Php?

Thanks,
Nathan
Nathand is offline
Reply With Quote
View Public Profile
 
Old 08-26-2007, 11:20 PM Re: How does "include" really work?
coolkbk585's Avatar
Be good this Christmas!

Latest Blog Post:
KBlog has been deativated
Posts: 642
Name: Kyle
Location: Ada, MI
Trades: 0
Try just using the url:

Code:
http://2008.huntingtonrobotics.org/header.php
get rid of the WWW in the start, if it's already a subdomain, you shouldn't need the WWW at the start.
__________________
<?php if($Adsense_Revenue > 0): define('HAPPINES','100%'); else: define('HAPPINESS', '0%') endif; ?>
coolkbk585 is offline
Reply With Quote
View Public Profile Visit coolkbk585's homepage!
 
Old 08-27-2007, 07:50 AM Re: How does "include" really work?
Ultra Talker

Posts: 483
Trades: 0
OK, so the thing is that you are now accessing it via HTTP, which is different to what you were doing the first time and 99% of the time is absolutely wrong.

This is the best thing I can tell you: despite what a lot of people on forums think, including via HTTP is generally wrong and UNLESS you KNOW WHY you are including via HTTP, you should just include it via the file system.

So the reason the first example fails is because by including via HTTP you are NOT including the PHP code... you are including the parsed result of the PHP code. It includes the same output as if you were accessing it via the browser. From that you should be able to see that because you haven't handed the loc value to the script via HTTP in any way there is no way for it to know what you have set the loc variable to.

Now, regarding your 'deep directory' structure. If you don't want to have to use the '../../../../' and so on (and I can't blame you for not wanting to use it), look at the $_SERVER['DOCUMENT_ROOT'] variable. That will give you the filesystem path to your webserver root. So if you want to include the file 'include.php' in the directory 'includes' that is located in your webroot, no matter where the including file is you should just be able to use:
include( $_SERVER[ 'DOCUMENT_ROOT' ] . '/includes/include.php' );

Hope that all makes sense and helps... By the way, you will find there's another PHP 'issue' with including files via relative paths in the filesystem, so I always use something similar to the above to make sure that although I'm using an absolute path, I don't actually need to know the full path name...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 08-27-2007, 09:32 AM Re: How does "include" really work?
Nathand's Avatar
Extreme Talker

Posts: 233
Location: USA
Trades: 0
Quote:
So the reason the first example fails is because by including via HTTP you are NOT including the PHP code... you are including the parsed result of the PHP code.
Wow! I never even thought about that... No wonder it wasn't working! I'll try the $_SERVER[ 'DOCUMENT_ROOT' ] then.

Thanks a million,
Nathan d
Nathand is offline
Reply With Quote
View Public Profile
 
Old 08-27-2007, 10:00 AM Re: How does "include" really work?
Ultra Talker

Posts: 483
Trades: 0
For what it's worth, a lot of people use this 'include via HTTP' when they first start out and are only including in various pieces of common HTML (eg. headers and footers). Because (in these basic instances) these aren't really dynamic at all, it does work (the output of the script alone produces what they are actually wanting).

The big problem is that the assumption is that this should work all the time (which of course it doesn't). Besides the "it doesn't work" reason for not doing this, there is another reason for not doing this even when just including basic HTML: because you are including via HTTP, your server makes a proper HTTP connection to itself to get the data. This means that it actually has to bring up a connection, request the data via that connection then pull that connection down, as opposed to just reading the file directly. This whole process will actually slow down your site's responsiveness...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 08-27-2007, 10:18 AM Re: How does "include" really work?
damien_ls's Avatar
Layershift

Posts: 474
Name: Damien
Trades: 0
Quote:
Originally Posted by TwistMyArm View Post
This whole process will actually slow down your site's responsiveness...
And spare a thought for the poor hosting company that has to put up with you wasting server resources :P

Seriously, if/when your site gets really busy and you're hosting it on a VPS/dedicated server then this issue will be important to you directly as it will ultimately save you money to have more efficient code
__________________

Please login or register to view this content. Registration is FREE
:: DDS & Dedicated, UK & USA-based
Please login or register to view this content. Registration is FREE
, Reseller & Shared Hosting
Experienced Parallels Platinum Partners (Plesk since 2001, Virtuozzo since 2003)
damien_ls is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How does "include" really work?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33143 seconds with 12 queries