Help with include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Help with include

Post by SyntheticShield »

I was playing around with trying to set an include file in a template so that no matter where I used that template at in the directory structure, it would be able to call the file. However, it keeps giving me the following error:

Code: Select all

 
Parse error: syntax error, unexpected T_STRING
 
Here is the code:

Code: Select all

 
<?php include('.$_SERVER['HTTP_HOST'].'/includes/myfile.php'); ?>
 
Ive tried escaping the quotes and playing around HTTP_HOST and PHP_SELF and so on and nothing is working. Can anyone help me work this out? Ive tried search google and came up with nothing, and Im getting the impression that what Im trying to do is not really common in connection with includes as I can find no examples. Thanks in advance.
Cryophallion
Forum Newbie
Posts: 10
Joined: Fri Apr 24, 2009 9:05 am

Re: Help with include

Post by Cryophallion »

What is the quote period before $_SERVER doing? I assume you were concatenating before, and these are remnants. delete these, to be this:

Code: Select all

<?php include($_SERVER['HTTP_HOST'].'/includes/myfile.php'); ?>
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Re: Help with include

Post by SyntheticShield »

Stupid dot, LOL

Well what I tried to do was cut and past from other code I had working using the HTTP_HOST. I wasnt trying to concatenate, just a stupid mistake on my part coupled with my lack of knowledge in PHP. The quote was in there because in other examples of includes I saw the quote around what was in the parenthesis. Tried to combine examples and obviously it ended in a mess.

If I may, I need to ask one other question in relation to that code.

Is it better to use HTTP_HOST or PHP_SELF or something else?

Now that you have corrected the coding error, its still not finding the file and Ive tried both.

Edit:
Okay, I tried DOCUMENT_ROOT and it sees the file now. But I was under the understanding that was not a good method to use.
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Re: Help with include

Post by SyntheticShield »

I thought I would comeback and see if another thought I had would work any better at this.

Basically what I want to do is when I include a file, to have PHP always reference from the http://www.mysite.com no matter where I use the include from in the directory structure. Ive been searching for some code examples like this but have not had much luck.

Then I thought, what if I declared a variable, say for instance:

Code: Select all

 
$myinc = "http://www.mysite.com/includes"
 
Then use the include function to call the file using that variable, for instance :

Code: Select all

 
<?php include($myinc/myfile.php) ?>
 
The problem is Im not sure how to write that include statement so that it will work. Can anyone help me out with that? Is my idea whacked and should try something else or am I going in the right direction?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help with include

Post by McInfo »

Code: Select all

<?php
$myinc = 'http://www.mysite.com/includes';
include $myinc.'/myfile.php';
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:24 am, edited 1 time in total.
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Re: Help with include

Post by SyntheticShield »

Thank you so much for your help. That is stirring up some ideas Im going to have to try out and play around with. Im learning here and there but its generating a lot of questions too.
Post Reply