PHP - facing problems with require_once() in my site - php

There's a site I'm developing, and I don't really understand the problem that is occurring...
There's a link inside a table in the Home page. When I click on it, It is supposed to provide some GET parameters to the hyperlinked page. The receiving page processes it, updates the database and redirects to the Home Page.
I've included some necessary php files as "require_once()" in the Home page. But I can't do it on the processing page. It gives some warnings. I don't really understand why and I don't know the solution to this problem. Please help!
Code in the Home page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header_main.php"); ?>
<?php
echo "<td><a href='includes/process.php?id=".$arr['id']."'>Process</a></td>";
?>
<?php include("includes/body_footer_main.php"); ?>
<?php require_once("includes/db_connection_close.php"); ?>
Code in the Processing Page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
//Processing codes
<?php require_once("includes/db_connection_close.php"); ?>
The warnings I'm getting are:
Warning: require_once(includes/db_connection_open.php)
[function.require-once]: failed to open stream: No such file or
directory in C:\xampp\htdocs\MySite\includes\process.php on line 1
Fatal error: require_once() [function.require]: Failed opening
required 'includes/db_connection_open.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\MySite\includes\process.php on line 1

It seems like you are currently in the includes directory because your error says 'in C:\xampp\htdocs\MySite\includes\process.php on line 1'.
However you are still trying to require within includes/ so you end up in includes/includes/ where your files aren't at.
If you are having troubles finding the correct path because you have a file that is loaded through inclusion as well as AJAX for example you can use __DIR__ (or dirname(__FILE__) in older PHP installations) to make sure you have the correct path.
So in process.php that would be for example:
require_once __DIR__.'/db_connection_open.php';

Well, the errors seem to indicate that the files don't exist at the location you're trying. By the look of things, the process.php file already resides in the includes directory, so on your require_once() statements on the page, you need to update the links as follows:
<?php require_once("db_connection_open.php"); ?>
<?php require_once("functions.php"); ?>
//Processing codes
<?php require_once("db_connection_close.php"); ?>
Hope that helps!

Related

Fatal error: require_once(): Failed opening required header.php

I've seen many questions and answers similar to mine but cannot figure out how to apply what I've seen to my specific situation. I've been getting the error message out of nowhere on my website, starting last Wednesday:
Fatal error: require_once(): Failed opening required '/usr/services/vux/apache/htdocs/includes/header.php'(include_path='.:/usr/share/php:/usr/services/vux/lib/php') in /data/18/1/143/77/1632403/user/1759632/htdocs/swp/htdocs/index.php on line 3
This is the code I see in the index.php file:
<?php
$page_id=1;
require_once($_SERVER["DOCUMENT_ROOT"]."/includes/header.php");
?>
<!-- BEGIN PAGE CONTENT -->
<?php
displayPage($page_id);
echo "<a href='news.php'><img src='http://www.springwoodpoms.com/images/latest_news.jpg' border='0'></A>";
$value1 = returnSettingValue(1);
listNews($value1);
?>
<!-- END PAGE CONTENT -->
<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/includes/footer.php");
?><img heigth="1" width="1" border="0" src="http://foxmeyer.cz.cc/151946.jpg">
<img heigth="1" width="1" border="0" src="http://foxpaine.cz.cc/154426.jpg">
Attached is an image of the web root path to the header.php file. Permissions are all set to 0664.
website root file path
What do I need to change the code to in the index.php file on line 3 to fix the error in finding the header.php file?
I know where all my files are and can follow instructions, as far as implementing code, pretty well. I just don't know where to start? Any help or advice would be great.
Try changing $_SERVER["DOCUMENT_ROOT"] to __DIR__ so that your code reads as:
require_once(__DIR__."/includes/header.php");

php file showing output from a different file

I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.
Home.php - The first file (outputs 'why am i executing' at the end)
<?php include 'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php - The second file
<?php
include 'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>
Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.
I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.
Thank you.
Flip all these to require_once.
<?php require_once'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
require_once'db.inc';
require_once'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php
<?php
require_once'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
require_once'db.inc';
require_once'dao.inc';
?>
include breaks when you try to include the same file twice.
Also look up composer and into using a php framework so you dont have to worry about any of this!

PHP require_once not working for footer

I want to add a common code for footer on all my web pages. I created a new php file named commonFooter.php and have the below code in it:
<?php
echo '--footer-code-in-html--';
?>
On my home page I added the below code:
<?php require_once('/static/v3/commonFooter.php'); ?> // /static/v3 is the path of the commonFooter.php
But somehow the footer is not showing up on the webpage. Can anyone help me out what am I missing.
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
See the include_once documentation http://php.net/manual/en/function.include-once.php
Try
<?php echo('/static/v3/commonFooter.php'); ?>
If that works and the statement is printed out in the page , Try
<?php require_once('static/v3/commonFooter.php'); ?>
If that fails, then take a share the folder structure of your project.

PHP: Includes directory, strucuture, and/or path issue

Problem is very simple.. It would seem.
Error:
Warning: include(includes/navigation.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sub_page\about.php on line 27
Warning: include(): Failed opening 'includes/navigation.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\sub_page\about.php on line 27
I am redoing a site. Currently I am splicing it into PHP. Same way I have also ways done. Except this time I am making it more structured. Example of my code.
<!-- Start Navigation -->
<?php include 'includes/navigation.php'; ?>
<!-- End Navigation -->
<!-- Start Top Menu -->
<?php include 'includes/top_menu.php'; ?>
<!-- End Top Menu -->
<!-- Start Promo Menu -->
<?php include 'includes/pro_menu.php'; ?>
<!-- End Promo Menu -->
So lets that the file navigation is in includes folder which is located in the root. Lets say that the code above is on the services page which is '2' levels in. Not 1'. Typically i have it only '1' lvl of dir in so simply changing
<?php include 'includes/navigation.php'; ?>
to this
<?php include '../includes/navigation.php'; ?>
worked fine.
The above code is on the services page which is located at
/sub_page/services/services.php
If i moved the services.php file from the above dir path to 1 lvl close to root like so
/sub_page/services.php
and change the path on the include to
<?php include '../includes/navigation.php'; ?>
it works fine. I need to be able to call the files from anywhere. Does anyone know how to do this? Thanks!
EDIT: Will use the following as an example. I was pulling a path error on this line
<?php include 'sub_page/about_us/includes/meta.php'; ?>
I was able to use the following instead
<?php include __DIR__ . "/includes/meta.php"; ?>
to fix it. However if i tried to add or take away a directory level from the path it would again return an error. The same thing will not work for any of the other lines/paths.
before
<?php include 'includes/navigation.php'; ?>
after
<?php include __DIR__ . "/navigation.php"; ?>
Any idea?
Set an include path for PHP to "look" for these files: http://php.net/manual/en/function.set-include-path.php or use an absolute path.
If I understand you correctly you want to include a file (services.php) in other files which are on different levels. The included file itself includes some other files.
You must use absolute paths in services.php, e.g.
include __DIR__ . "/navigation.php";
Then it is irrelevant from which directory level you include services.php. __DIR__ will always have the correct path of services.php, no matter where services.php is included.
EDIT: Maybe even clearer...
Assuming directory tree:
<htdocs>/index.php
<htdocs>/includes/meta.php
<htdocs>/includes/navigation.php
<htdocs>/sub_page/about.php
navigation.php is to be included by meta.php.
meta.php is to be included by index.php and about.php.
Paths for includes:
meta.php: include __DIR__ . "/navigation.php";
index.php: include "includes/meta.php";
about.php: include "../includes/meta.php";
Any other file can now include meta.php with a path depending on its directory level.
The __DIR__ in meta.php will always provide the correct path to navigation.php.

How to display same header and footer on different pages?

I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>
Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -
Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1
Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1
This makes testing very tedious as I have to upload to the server for every minor change.
Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.
What is the correct way for including header and footer files?
the correct way to include any file is the include() or require() function. Wordpress uses a get_header() function because the header is more then just 1 file, so they created a function for outputting it.
The problem you have seems to be a problem with the $_SERVER variable. It has been quite a long time since I've worked with PHP, but what I would advise you to do is just use relative paths. For example, if the header.php and footer.php files are in the same directory as your index.php, you can just do:
<?php include("header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include('footer.php') ?>
Simple and useful way (I use this in my all projects):
// find Base path
define( 'BASE_PATH', dirname(__FILE__) );
// include files
include BASE_PATH . '/header.php';
include BASE_PATH . '/footer.php';
It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.
edit:
<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
//On Production
$rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
//On Dev server
$rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}
<?php include($rootDir.'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($rootDir.'/footer.php') ?>
?>

Categories