php file showing output from a different file - php

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!

Related

Why does my included/required file with variables stop working when I upload everything?

My index.php gets all parts for the website seperated and it looks like this:
<?php
include 'var1.php';
?>
<?php
include 'var2.php';
?>
<?php
include 'https://www.example.de/city/head.php';
?>
<?php
include 'https://www.example.de/city/header.php';
?>
<?php
include 'https://www.example.de/city/content.php';
?>
<?php
include 'https://www.example.de/city/footer.php';
?>
<?php
include 'https://www.example.de/city/bottom.php';
?>
The main parts like head, header etc. are always the same but for each city I need different variables and thatswhy I got a folder (for example 'Berlin') with only index.php, var1.php and var2.php in it.
This is working fine on my computer but as soon as I upload it to get it online, the variables from var1.php and var2.php are not included anymore.
Can anyone see something wrong here?
Fixed it with:
include($_SERVER['DOCUMENT_ROOT'].'/city/head.php');

Maintain central header in deeper directories

On my site within /public_html I have a file called /public_html/header.php. This file is called on every page using;
<?php echo file_get_contents("./header.php"); ?>
which works for any pages within the /public_html directory.
Same goes for the index page of the blog at /public_html/blog/ and its index page. It just uses;
<?php echo file_get_contents("../header.php"); ?>
instead. The issue arises when including the header within the articles subdirectory at /public_html/blog/articles/ariclename.php. I'd tried;
<?php echo file_get_contents(".../header.php"); ?>
but that doesn't work. I've also tried different ./././, ../../../ combinations but can't seem to get it to work.
Any ideas would be greatly appreciated. Thanks in advance :)
You can use a constant to set your path globally across all your pages.
This is how you would do that.
Create a file to configure the constant with the path to the header.
config.php
<?php
define('MY_HEADER', 'full/path/to/header.php');
?>
Generally you want to include the config.php in a file that gets called every time your script loads so that it always gets called.
In a file that gets called every time your script loads you would require your config.php file like so.
require 'path/to/config.php';
Then on any page where you need to use header.php all you need to do to get the header is this:
<?php echo file_get_contents(MY_HEADER); ?>

How to stop PHP echoing a "1" after being called from "include" within a HTML file?

I'm trying to call my navigation bar by using the following within my HTML code:
<?= include 'subfolder/topbar.php'; ?>
However, because the script is successfully running, it's echoing a "1" on completion, before my page continues rendering more HTML.
This is then screwing up the spacing within my page.
I've tried to read up on this (and looked at similar threads) but can't find a working resolution.
How do I include the contents of the PHP file without it echoing "1" on completion?
I've also tried putting the include in a variable, but it still outputs the "1":
$myX = (include 'subfolder/topbar.php');
I've also tried using:
include 'subfolder/topbar.php';
return ob_get_clean();
But this just halts the output altogether (so the rest of the page doesn't render), much like using the "exit" command.
The correct way should be
<? include 'subfolder/topbar.php'; ?>
or
<?php include 'subfolder/topbar.php'; ?>
Your code
<?= include 'subfolder/topbar.php'; ?>
is like saying
<? echo include 'subfolder/topbar.php'; ?>

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.

Including a uri "/user/sidebar" page (Kohana Framework) in PHP

I would like to have the page on user/sidebar on the right in the template design.
Normally, I would include a php file. But I am using Kohana Framework so I have created a view and a controller for this sidebar, and exists on mysite.com/user/sidebar
Now how would i <?php include "/user/sidebar"; ?> correct? I get no such file og dir error for this. I tried full url, but allow_url_include=0
Just looking through the Kohana documentation...
It seems like you can include a "request" inside a view with the following command.
<?php echo Request::factory('user/sidebar')->execute() ?>
See this page for more info: http://kohanaframework.org/3.0/guide/kohana/mvc/views
AndrewR's comment is close. For Kohana 3.2, you'll want to load a view within a view and not a request within a view:
<?php echo View::factory('/user/sidebar'); ?>
or
<?php include Kohana::find_file('views', 'user/sidebar') ?>
Either is acceptable to do.

Categories