/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Includes aren’t working

I stopped working on this a while ago. Then I realized the PHP includes weren’t working when I tried again. So now, I’m trying to work on it again. Can someone tell me why this isn’t working?

For some reason, after I include urls.php, the variables don’t stay. I can’t even echo one of them in the command right after the include. Then, the big problem, I can’t include using a variable (defined in urls.php). urls.php is included however, because I can echo the variables in that file, just not outside of it.

I seem to have some sort of problem with includes on all the rest of my webpages on my local host too. It’s weird because when I stopped, I know they all worked. Then I start working on them/looking at them again, and none of the includes work. Usually it says “failed to open stream.”

[upl-file uuid=6b0f393a-6752-41da-aa8f-5148e210e948 size=4kB]website.zip[/upl-file]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelApr 01.2006 — [code=php]//try using
$_SERVER['DOCUMENT_ROOT']
// for includes, not
"http://" . $_SERVER['HTTP_HOST']

// eg.
include $_SERVER['DOCUMENT_ROOT'] . "/ext/includes/urls.php";
# not
include "http://" . $_SERVER['HTTP_HOST'] . "/ext/includes/urls.php";[/code]


When you use HTTP_HOST the server parses the PHP then includes it, like going to the page in the browser, then including what you see in the source into the PHP page, so the variables arn't available because the page has gone "outside" the server.

When you use DOCUMENT_ROOT, the server includes it into the PHP then parses it and the variables are available.
Copy linkTweet thisAlerts:
@aPersonauthorApr 01.2006 — It says undefined index. Odd.

er... whoops.. I still have 4.3.4. Looks like there's 5.1.2 out. Does DOCUMENT_ROOT only exist in the latest version? I'm going to dl+install it now.

The weirdest thing is that it used to work. Then I stopped and when I came back to it, it doesn't work.

Still says undefined index. Do I have to change something in the php.ini file? If so, will this work on my site which is on a subdomain?
Copy linkTweet thisAlerts:
@ScleppelApr 01.2006 — 'DOCUMENT_ROOT' is in PHP4 and PHP5. Are you sure you've spelt it right? You could try using "[color=red]getenv('DOCUMENT_ROOT')[/color]":
[code=php]include getenv('DOCUMENT_ROOT') . "/ext/includes/urls.php";[/code]

PHP predefined variables: http://uk2.php.net/reserved.variables
Copy linkTweet thisAlerts:
@aPersonauthorApr 01.2006 — Yeah I copied it from what you wrote, just to make sure. It still didn't work.

getenv seemed to try to include /ext/includes/urls.php.

Neither works.

Here it is with $_SERVER['DOCUMENT_ROOT']

[upl-file uuid=77b890ec-abbd-47fb-bc35-eab0ccddd0ef size=4kB]website.zip[/upl-file]
Copy linkTweet thisAlerts:
@ScleppelApr 01.2006 — It works for me, maybe when you've reinstalled PHP it'll work.
Copy linkTweet thisAlerts:
@aPersonauthorApr 01.2006 — Hmm well I just installed PHP5 over the old one, and I chose to keep php.ini. Maybe there's a problem in the ini file. I seem to remember many important tweaks I applied in order to use PHP with IIS. If you could tell me where to find these, or if they're even required anymore, I'll try using a fresh ini file.
Copy linkTweet thisAlerts:
@aPersonauthorApr 01.2006 — I changed the doc root in php.ini to http://localhost/ and that seemed to help, but I'm still getting the same problem of the variable not working in the rest of the file. It's weird it works for you.


Notice: Undefined variable: header in C:webpagesjindex.php on line 1

Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:php5pear') in C:webpagesjindex.php on line 1

Welcome to James' website. This site is designed with XHTML and CSS. Blah blah blah blah. Blah blah blah blah. This is the content area. Content area. More stuff. Blah blah blah. This is my site. Blah blah blah. Blah. Some lipsum. Lorem ipsum id per saepe offendit, an esse nullam per, eos te puto audiam antiopam. Quo in unum elitr, eos volumus urbanitas cu. Eos blandit nominati atomorum ut, vix at illum euripidis disputando. Usu vocibus appetere patrioque te, pro at vitae platonem gubergren. Eam affert platonem efficiantur te, no eleifend convenire vim. Cu vel puto populo dissentias, fabulas consequuntur nec ex. Aliquam consetetur ad pro, eius semper fierent eam no, ut pri quem eligendi definiebas. Vide delectus no eum. Ut dicta ornatus pro, no nam nostrud ceteros mediocrem, per puto cetero oporteat ne. Ad adhuc gloriatur ius, usu id unum senserit, docendi takimata cu sea. Et impetus facilisis nam. Luptatum delicatissimi nam eu, vim et percipit prodesset definitiones. Et pri apeirian iracundia. Vel sint cetero inciderint ea, mel enim mutat nihil ne. Ad aperiri tibique pro, ei pro reque scripta, te vocibus graecis eloquentiam vix. Iusto populo menandri id qui.

Notice: Undefined variable: footer in C:webpagesjindex.php on line 8

Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:php5pear') in C:webpagesjindex.php on line 8[/quote]
Copy linkTweet thisAlerts:
@aPersonauthorApr 02.2006 — Nevermind. The reason the variables weren't being inculded was because the php files were being parsed as PHP files and therefore weren't working. The reason it didn't work before was because I was using HTTP_HOST, then I switched the inc files to php (stupidly changed two things at once).

Anyway, the solution was I changed the PHP files to text files
×

Success!

Help @aPerson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...