PHP require_once not working for footer - php

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.

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');

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!

using include with a word from database

Im having a little problem and cant get it to resolved for some reason. I have built a website and got it running but i have now passed some of the files over to a folder to build more themes. What i am doing is trying to change themes back end and for this instance i will use the theme "smooth"
this is what i have, if i put this:
<?php
include "config_inc.php";
?>
include "themes/<?=$aset['Theme']?>/index.php]";
it will display on the page as the correct path "themes/smooth/index.php" so i know it is connecting to the database correctly. But now if i put it in this format:
<?php
include "config_inc.php";
?>
<?php
include "themes/<?=$aset['Theme']?>/index.php]";
?>
It just shows a blank page.
I have tested the link as in "my_domain/themes/smooth/index.php" and every is displaying correctly
Hope someone can help. Thanks
You shouldn't use php tags inside php tags to insert variable in string. Just use {} to do it:
include "themes/{$aset['Theme']}/index.php";
Or
include 'themes/' . $aset['Theme'] . '/index.php';

Include external code on a web page ( php include() or get_header() )

I've started creating a custom html page for the first time and i've hit a bump. The code in the header will be the same in all my pages , so i want to put it in an external file (header.php) and just put 1 line of code in all my pages to link to it but it doesn't seem to be working .
Note - I want to include just basic html code ( not a function ) - i'm thinking that's why its not working but not sure.
HTML file :
<body>
<?php include('header.php') ?> // didn't work
<?php get_header(); ?> // didn't work
<div class="content"> </div>
</body>
PHP File :
<h1>TITLE</h1>
When i put the code directly on the page - it shows "TITLE"
When i try include or get_header , i get a blank page . I checked the codex and online posts but i've not been able to fix it .
Any help would be appreciated - Thanks
Try in your header.php:
<?php
echo "<h1>TITLE</h1>";
?>
Then where you want to insert the header:
<?php include '/header.php' ?>
The / character makes the path to header.php relative to your root.
<?php include("dyn_layout/cbside_php.php"); ?>
This works for me... just don't forget to rename your files to .php extension... including the file where the insertion is made.

php include breaking

I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>

Categories