Page 1 of 1
Help with include
Posted: Fri Apr 24, 2009 11:50 am
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.
Re: Help with include
Posted: Fri Apr 24, 2009 12:01 pm
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'); ?>
Re: Help with include
Posted: Fri Apr 24, 2009 12:16 pm
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.
Re: Help with include
Posted: Sat Apr 25, 2009 2:56 pm
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?
Re: Help with include
Posted: Sat Apr 25, 2009 4:16 pm
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.
Re: Help with include
Posted: Sat Apr 25, 2009 4:37 pm
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.