PHP - Load header from another file - php

I've run into problem whereby I'm not able to include a header file into a PHP file
In the code below, I've removed the original content as it's quite big & anyway what the code does is irrelevant to including the header file
MAIN.PHP
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
header content
</div>
<br/>
<div>
</div>
</body>
</html>
I would like to have the following code in the header.php file & include it on all pages.
<div>
header content
</div>
When I do so with the main.php file looking as below, the page draws blank.
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
<? php include('header.php') ?>
</div>
<br/>
<div>
</div>
</body>
</html>
I've tried debugging this & following a few solutions on stackoverflow, but evidently have failed to achieve what I'm after.
Could I please request a second pair of eyes to help me spot the problem?

Well, first of all, I don't think you copied the code correctly to here.
As my debugging eyes can see, you got 2 issues:
Line 1: < is missing at the start. Change it to <?php. (Maybe
copy paste error?)
<?php include('header.php'); is missing closing tag. Change it to:
<?php include('header.php'); ?>
If you get a blank page, you probably have some errors. Try finding the "error.log" file in you working directory or add the following code at the start of the page:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Related

Include header and put index inside header html

I have a html header included with php include function however my context inside the index "Hi, World! is showing underneath my included header and not inside a html div from the header itself where I want. My screenshots:
The problem:
What I want:
So what i would like is the that my index:
<body>
<?php include('header.php'); ?>
<div >
<h3>Hi, <span class="">World!</span>
</h3>
</div>
</body>
shows inside the following div from my header.php:
<div class="content"> </div>
The problem was fixed by removing one </div> in this way the content is still opened to html further in an other document.

How to add meta tag's values for seo using PHP?

I am using one header file for every page which will show the HTML head(which includes meta tags and other CSS links)
Here I have used everything as dynamic as if I like in the case of canonical tags I have used $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_HOST'] and all other links to CSS are also accessible even if it can be any file from any directory.
Now I want to create a one-time title meta tag & description tags like this will be my main file looks like
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_content; ?>" />
//Other meta tags & linking to css & js files
</head>
<body>
// Content ........
</body>
</html>
Now here is the twist is that many people will say that just use
$page_title = "Here is the title of the specific page";
$page_content = "Here goes the long-form description describing the page of specific page";
Yes, this would have worked out if the pages were different and had the same place to put that all before including the header file.
But my index page looks like this let me explain it too. (Using Bulma as a framework)
<?php
require 'include/db_connect.php';
require 'include/header.php';
?>
<form name="submitform" method="POST">
<div class="columns is-multiline" id="wrapper">
<div class="column is-6-desktop is-12-tablet" id="main_content">
<div class="box">
<?php
if($country)
{
?>
<?php require 'inc/country.php'; ?>
<?php
}
else if($country && $state)
{
?>
<?php require 'inc/country_state.php' ?>
<?php
}
else if($country && $state && $district)
{
?>
<?php require 'inc/country_state_district.php'; ?>
<?php
}
else
{
?>
<?php require 'inc/other_than.php'; ?>
<?php
}
?>
</div>
</div>
</div>
</form>
<?php require 'footer.php'; ?>
</body>
</html>
The main point here is that I am using the dropdown button which gets auto-submitted using js that's not relevant here but from the top, I have just explained what is the structure of my code.
Now as you can see the if-else structure which includes other files that create dynamic pages but their code starts only from the body and not from the head directly so I am not able to add those title tags and descriptions.
Now how to add title & description tags uniquely to each of these pages.
Any solutions, please... Thanks in Advance
Like already written in the comments. You should define the vars ahead of require 'include/header.php'; or use an MVC structure.
If you still want to stay with the existing code ob_get_contents() might help you.
Use ob_start(); at the beginning to tell PHP to not print any output and instead write the output to a buffer.
You can then at a later point use ob_get_contents(); to fetch the output buffer and print it.
You have to search in the output buffer for an identifier like %page_title% then and replace it with the actual value before sending the output.
This may look like following:
echo str_replace(%page_title%, $page_title, ob_get_contents());
Still I'd rather suggest restructuring your code, as the solution with output buffer is slower, uses more memory and is poor to debug.

Assign php code to smarty template engine

I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
In my template (.tpl) file I have a div-section to load the content from our database:
<html>
<body>
<div id="content">
{$content}
</div>
</body>
</html>
So my code looks like this afterwards:
<html>
<body>
<div id="content">
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
</div>
</body>
</html>
Is there a way that smarty processes the PHP-code before parsing it?
Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.
What i tried:
saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
SmartyBC-Class
You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:
Read the php code from the database.
Save to a temporal php file
Turn on output buffering with ob_start()
include the file you have created
assign the output to a variable with ob_get_clean()
assign the variable to the template
But if I was you, I would try to do the project in another way.

My first php not working

Trying to load my first php site in a browser; Firefox displays odd coding tags, IE asks if I want to "open" the file, instead of loading the page. I think I copied a tutorial 100%, so I suppose it really should work.. Is there anything else I need to do to make this work, or may I have some code error?
functions.php
<?php
function displayContent($content) {
echo implode($content);
}
function displaySidebar($sidebar) {
echo implode($sidebar);
}
?>
template.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<meta name="author" content="SS" />
<link href="site.css" rel="stylesheet"type="text/css" />
</head>
<body>
<div class="container">
<div class="header">
header
</div>
<div class="sidebar">
<?php displaySidebar($sidebar); ?>
</div>
<div class="content">
<?php displayContent($content); ?>
</div>
<div class="footer">
footer
</div>
</div>
</body>
index.php
<?php
//include our functions
require_once ('functions.php');
// declare arrays
$content=array();
$sidebar=array();
//handle page request
$t=$_GET['t'];
if($t == 'about') {
//display about page //can we load a full template?
$content[]="<h2>about</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
} else {
//display the home page
$content[]="<h2>home page</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
}
//Important this is on bottom
//I guess content must be ready on load.
include ('template.php');
?>
OUTPUT: (Firefox)
about"; $content[]="
Content
"; $sidebar[]=""; } else { //display the home page $content[]="
home page
"; $content[]="
Content
"; $sidebar[]=""; } //Important this is on bottom //I guess content must be ready on load. include ('template.php'); ?>
Uh, have you already installed Apache and PHP, and have you put the files in Apache's web root? Because PHP is not HTML: it's not parsed by the browser. It's executed by the server, which then returns the HTML response to the browser.
If you are working locally, try installing wamp or xampp, if you're working on a host. I suggest looking for one that supports php and mysql.
It could be that your server doesn't have PHP set up, or maybe your files aren't ending in .php.
To check, I'd remove what you've done so far, and make a new file called index.php. In it, I'd just write:
<?php
echo "Hi!";
?>
Then go to the page. If that works, then you know your script has some issue, if not, then you know the server isn't set up correctly.
If that does work, you may want to change echo "Hi!"; to say phpinfo();. That tells you all about the PHP install, which may come in handy to know at some point!

Include URL in php

My form create by php script and between teo file:
index.php
header.php
In index.php, i try in
...
<div class="art-header-png"></div>
<div class="art-header-jpeg"></div>
<?php include("header.php"); ?>
</div>
<div class="art-nav">
<div class="l"></div>
<div class="r"></div>
and my header.php
<div class="art-logo">
<h1 id="name-text" class="art-logo-name">Your Title Goes Here</h1>
<div id="slogan-text" class="art-logo-text">Your Slogan Goes Here</div>
</div>
But it didn't show content inside header.php.
How can i make it work?
First thing, either place header.php in the same folder as index.php or specify the correct path relative to index.php when you are using include (same apply to require).
PHP Include
also check to see if your php is configured correctly as well. Save the following line of code in a php file. Run it and see if you get php window:
<?php
phpinfo();
?>

Categories