PHP include doesnt work on files other than index - php

So here is my problem:
I have an index.php in root directory in xampp's htdocs. The purpose of that is to login an user. After he loggs in, he is redirected to the game/index.php, where i have two includes:
<?php include('/game/components/language_declaration.php');
include('/game/components/currency/userdata_func.php');
?>
Here is the directory list:
index.php (which redirects to index below)
---/game/index.php (includes work)
---/game/units.php (the same includes dont work)
---/game/components/language_declaration.php
---/game/components/currency/userdata_func.php
There is a link in this index which points to units.php. And there are the same includes in units.php, except the fact that they dont work. This is the error that i get:
Warning: include(/game/components/language_declaration.php): failed to open stream: No such file or directory in E:\xampp\htdocs\game\units.php on line 7
Warning: include(): Failed opening '/game/components/language_declaration.php' for inclusion (include_path='.;\xampp\php\PEAR') in E:\xampp\htdocs\game\units.php on line 7
Warning: include(/game/components/currency/userdata_func.php): failed to open stream: No such file or directory in E:\xampp\htdocs\game\units.php on line 8
Warning: include(): Failed opening '/game/components/currency/userdata_func.php' for inclusion (include_path='.;\xampp\php\PEAR') in E:\xampp\htdocs\game\units.php on line 8
I have read about 20 posts about this problem, every single one advised to change the path, I have tried to add dots, slashes and so one, even defining the root directory. None worked. Could you help me please?

Your include statements should be like this:
---/game/index.php
<?php
include('components/language_declaration.php');
include('components/currency/userdata_func.php');
?>
---/game/units.php
<?php
include('components/language_declaration.php');
include('components/currency/userdata_func.php');
?>

Related

php Include - file in a folder

I'm using php include in the index file to include banner (graphic on my page).
The files are in the folders:
C:\Users\name\IBM\rationalsdp\workspace\RemSysTempFls\ERIC\www\apachedft\htdocs\fender400\gk\index.php
RemSysTempFls\ERIC\www\apachedft\htdocs\fender400\banner.php
I am unable to include banner.php with the following:
include ('/fender400/banner.php');
and get the errors (the page still runs):
Warning: main(/fender400/banner.php): failed to open stream: No such
file or directory in /www/apachedft/htdocs/fender400/gk/index.php on
line 16
Warning: main(): Failed opening '/fender400/banner.php' for inclusion
(include_path='.:/QOpenSys/php//lib/php') in
/www/apachedft/htdocs/fender400/gk/index.php on line 16
Are you sure that the addresses of each of those files are correct?
Try it without the initial slash and see what it tells you
include ('fender400/banner.php');
or try:
include ('../../fender400/banner.php');

Eclipse PHP file not able to see html file in same project folder

I'm using the Include func in a index.php file that looks for a header.html file and getting the following 2 errors:
Warning: include(includes/header.html): failed to open stream: No such
file or directory in C:\wamp\www\Projects\Chapter Three\index.php on
line 3
&
Warning: include(): Failed opening 'includes/header.html' for
inclusion (include_path='.;C:\php\pear') in
C:\wamp\www\Projects\Chapter Three\index.php on line 3
I verified both index.php & header.html are in the same dir via Explorer so what else should I check? Thanx!
You need quotes around the include URL, like include 'home.html';, If that doesnt work, try throwing a / before the url, include '/home.html';

php include from index.html

I'm trying to use php include to include articles on a web page... My file structure looks like this:
/Articles
/articles
App_Template.html
/tags
/new
index.html
/including
File.html
In App_Template.html I have the piece of code:
<?php include("../including/File.html"); ?>
Which works fine and displays the correct article when you access App_Template.html, However the code:
<?php include(".../including/File.html"); ?>
Within index.html in the new folder doesn't work and throws these errors:
Warning: include(.../including/File.html) [function.include]: failed to
open stream: No such file or directory in
/home/a1696768/public_html/articles/tags/new/index.html on line 79
Warning: include() [function.include]: Failed opening
'.../including/File.html' for inclusion
(include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/a1696768/public_html/articles/tags/new/index.html on line 79
I've tried searching around to find a reason for this but cannot however find any reason as to why this isn't working here but is in App_Template.html, the only thing I can possibly imagine is it's something to do with the fact the second case is an index.html?
You can't use three dots ... in paths, it's either .. for one level up or . for the current level. If you want to go 2 folders/levels up, use ../../yourfile.

PHP include - failed to open stream: No such file

I wrote a website in HTML but then I wanted the header and navbar portion in one file so I just had to change it once. I changed all the pages to .php and added the php includes. However now I get an error on pages that I've put in folders so I could create cruftless links.
Header file: /test/assets/header.php
Works: /test/index.php contains <?php include 'assets/header.php'; ?>
Throws Error: /test/company/index.php contains <?php include '/test/assets/header.php'; ?>
Error:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I am having an issue linking to the header file that is in a folder in the root folder. I believe it is a simple issue of just not knowing how to input the URL. If I attempt to include the full path to the header.php I get a URL file-access is disabled error
If you start a path with a / then it's an absolute path. Absolute mains to the actual filesystem root directory, not the virtual document root. This is why it fails.
Commonly this is what was meant:
include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
// Note: array key quotes only absent in double quotes context
I was also facing the same warnings:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I solved them with the following solution:
You have to define your path then use include function as below:
define('DOC_ROOT_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
require DOC_ROOT_PATH . "../includes/pagedresults.php";
After that, you can use any directory structure.
It's not a URL; it's a filepath (although you can use full URLs when the server configuration allows it).

Fail to use include without getting an error:

I am trying to use this:
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Warning: include_once(../../untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'../../untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
and this:
include_once("/PoliticalForum/untitled/sanitize_string.php");
Warning: include_once(/PoliticalForum/untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'/PoliticalForum/untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
How can I import files safely without having those errors?
Pick one:
Provide a valid path (paths that start with / are absolute, otherwise they are relative).
Add the folder to the include_path.
Use class autoloading (if applicable).
You should make sure the files can be found on the include path, because these errors mean the file cannot be found by PHP.
More info about include can be found in the PHP manual: http://php.net/manual/en/function.include.php
I think those files you include each inlude other files (possibly with require or require_once). These files cannot be found which causes the include to fail.
Include is ignored if the file doesn't exist, but it fails if the file does exists but contains errors.
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Above statement means the directory PoliticalForum is in the same directory of the file which has this statement.
You need to look into the directory structure you are using.
Alternatively, you could use $_SERVER['DOCUMENT_ROOT'] to locate all directories starting from root directory.
ForExample:
`$_SERVER['DOCUMENT_ROOT']`/dirLevel1 // sub directory on root
Try this
include_once "PoliticalForum/StoredProcedure/User/number_login_attempts.php";
include_once"PoliticalForum/untitled/sanitize_string.php";

Categories