Page reapets it self when using include - php

So Lately i was working on a website on LocalHost Using XAMPP Application.
So i created header.php with the code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="A short description." />
<meta name="keywords" content="put, keywords, here" />
<title>Website Name</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="menu">
<a class="item" href="index.php">Home</a>
<a class="item" href="../forum/">Forums</a>
<a class="item" href="live-chat.php">Live Chat</a>
<a class="item" href="Login.php">Log In</a>
<a class="item" href="Register.php">Register Now!</a>
<div id="userbar">
<?php
error_reporting(E_ALL & ~E_NOTICE);
include 'search.php';
if($_SESSION['signed_in'])
{
echo 'Welcome <b>' . htmlentities($_SESSION['user_name']) . '</b>. Not you? <a class="item" href="logout.php">Log out</a>';
}
?>
</div>
</div>
<div id="content">
And a Login.php with this code ( Here is the 4 lines of it ) :
<?php
include 'connect.php';
include 'header.php';
etc..... php code....
Ok so the problem is When i try to open the login.php in web browser i got the code in header.php duplication many many times like it don't end duplicating it self and if i open the source code of login.php i will got unlimited number of the code used in header.php like all the source code is header.php repeatedly.
So I'am asking you guys for help on how to fix this and what is the error ??
NOTE: Sorry if their was a thread duplication but i didn't know on what to search exactly.
If you want anymore information I'am ready.
Thanks all much appreciated

Use
include_once('header.php');
everywhere instead. It will check to see if the file has already been included.

use require_once instead, it will load the file just once, and only if needed.
You have more info here

Related

PHP with XHTML MP 1.2

I have the following code: index.php
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN"
"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta content="metadesc" name="description" />
<meta content="metakey" name="keywords" />
<meta name ="googlebot" content="index,follow" />
<meta name="robots" content="INDEX, FOLLOW" />
<link rel="stylesheet" href="core.css"/>
</head>
<body>
<div class="page">
<div class="head">
<div class="menu">
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
</div>
</div>
</div>
</body>
</html>
my only issue is that when i try and preview the document, nothing shows no source code, no error or anything.
I have tried with the following in the header before any content to try and rectify the issue as i have looked and mobile xhtml can't be served as text/html. I have tried the following (obveously not at the same time):
<?php header("Content-type: application/xml ");?>
<?php header("Content-type: application/xhtml+xml ");?>
<?php header('Content-type: application/vnd.wap.xhtml+xml'); ?>
Still no output/errors or anything. Anyone any advise on what the issue could be?
Well it turned out display errors was off and i was getting a http 500 error in php as short tags wasn't turned off and it was trying to parse the <? in the xml

How to properly include a header file in a webpage

So I have a header file:
<html>
<head>
<link href="/design_header.css" rel="stylesheet" type="text/css" />
</head>
<body>
content
</body>
</html>
And I want to place this header in my file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
The problem is now I have regular <html>, <head>, <body> tags inside the <body> tag of my webpage. In the html validator I receive errors like
Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th
e same type was already open.
How to do it properly? Btw. I also place a footer to the bottom of the webpage the same way.
Your header file should only contain the HTML text you want for the header.
As it will be inserted into another webpage, it should not be a full HTML document.
Having only HTML for Header in Header
One option is to instead include in your header file only the HTML that is for the header (used by all pages that include it). However this has the downside that your not following the recommendation to have CSS loading before is rendered.
See: https://stackoverflow.com/a/1642259/1688441
Header File
<link href="/design_header.css" rel="stylesheet" type="text/css" />
content
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
Having only HTML for Header in HeaderFile and Separate HeadFile
You could have have a separate global_head_include.html (or txt, php, etc) file and put your CSS code there.
Header File (for include)
Header content
File with CSS includes (for include)
<link href="/design_header.css" rel="stylesheet" type="text/css" />
Whatever else is global...
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
<?php include 'global_head_include.php'; ?>
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
This is how I set out my headers and footers in my current PHP site:
header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>James Wright - Young Software Developer</title>
<link rel="stylesheet" href="style.css" />
<meta name="description" content="The online home of a young web designer and software developer." />
<link rel="icon" type="image/png" href="/img/ico.png" />
</head>
<body>
<div id="holder">
<div id="menu"></div>
<div id="mainContent">
footer.php:
</div>
<div id="mainReflect"></div>
<p class="footer">© James Wright <?php echo date("Y"); ?>. Minimum resolution: 1024x768 <a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" style="vertical-align: middle;"/></a>
<a href='http://www.powermapper.com/products/sortsite/'><img src='http://www.powermapper.com/images/badge-v1/sortsite-badge-small-5.png' width="80" height="15" alt='Broken link checker and accessibility checker top 5% - SortSite' style='vertical-align: middle;'/></a>
</p>
</div>
</body>
</html>
Then whenever I create a new page, all I need to do is this:
<?php include("header.php"); ?>
<p>Main body content!</p>
<?php include("footer.php"); ?>

How to insert content in a header of web page from other file?

I have a problem with an included header in a phtml file.
I have an MVC project and the view file is builded like this
<?php
require_once'header.phtml';
//SOME PHP COMMAND TO PRINT SOME STUFF
reuire_once 'footer.phtml';
?>
When I press F12 in Google Chrome the header content apears in the body of the page, like this :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css"></style></head>
<body>
<title>UserList</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/Mystyle.css">
<meta name="description" content="MVC MiniShop Project">
<meta name="keywords" content="MVC Online Store">
<meta name="author" content="Vasile Vetisan">
<meta charset="UTF-8">
<div class="menu">
<ul>
<li>Home</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
<br style="clear:left">
</div>USER LIST IS:<br> <table border="1"><tbody><tr><td>USER_ID</td><td>FIRST_NAME</td><td>LAST_NAME</td><td>USERNAME</td><td>EMAIL</td><td>PASSWORD</td></tr><tr><td>2</td><td>Test</td><td>Unu</td><td>test1</td><td>test1#gmail.com</td></td></tr></tbody></table>
</body></html>
Even if I change the structure of the ViewUser page like this:
<html>
<header>
<?php
require_once 'header.phtml'
?>
</header>
<body>
//SOME GENERATED STUFF WITH PHP
</body>
</html>
The result is the same as previous :( .
So as summary of my post I need a method to insert content in header of a phtml page from another page using php or phphtml extenxions.
I have tried a lot of stuff so I need some new ideas or answers.
I think the very good, easy and effective way of including parts is using php, where part.html is file with your code.
<?php
include "part.html";
?>
Example:
part.html
<p> Hello world! </p>
index.php
<body>
<?php
include "part.html";
?>
</body>
output should be
<body>
<p> Hello World! </p>
</body>

How can I print a php variable from a session in the footer of an html/jquery page?

I currently am trying to use print inside of the JQuery footer by writing it is not working. Instead nothing shows up. In the earlier php page I stored it into the session using...
session_start();
$_SESSION['username'] = $_POST['username'];
my code in my html page is as follows...
<?php session.start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Food For Thought</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css" />
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header" id = "hdrMain" name = "hdrMain" data-nobackbtn = "true">
<h1>Food for Thought</h1>
</div><!-- /header -->
<div data-role="content" align ="center">
Play
Profile
Logout
</div><!-- /content -->
<div data-role="footer">
<p>Logged in as: <?php print $_SESSION["username"]; ?> </p>
</div>
</div>
</body>
</html>
Thank you any help is appreciated.
Edit: Problem was that the file I was trying to run php code ended in .html instead of .php. Thank you all who tried to help.
Well, you're HTML page has:
session.start();
it should be
session_start();

How to restrict users from entering a page without being logged on

i used sessions to log a user in so basically the user is stored in a session. what code do i use to stick into my following file so a user cannot access the page unless he/she is logged in.
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cook It Dot Com</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>
<body><div id="wrap">
<div id="header"></div>
<div id="nav">
<ul id='menu'>
<li><a href="../usersloggedin/starters.php" >Starters</a></li>
<li><a href="../usersloggedin/mains.php" >Mains</a></li>
<li><a href="../usersloggedin/vegeterian.php" >Vegeterian</a></li>
<li>Desserts</li>
</ul>
<ul id="rightmenu">
<li> Logout</li>
</ul>
</div>
<div id="content">
<div id="info" align="justify"><FONT COLOR="white">
My Account -
<?php
session_start();
require_once '../database.php';
if (isset($_SESSION['myusername'])){
echo "Welcome ".$_SESSION['myusername'];
}
?>
<hr />
<br /> Upload Your Recipes<br /><br />
Upload Starter
Upload Mains
Upload Vegeterian
Upload Desserts
<br /><br />Edit/Delete Your Recipes<br/><br/>
Starter
Mains
Vegeterian
Desserts
</div></div>
<div id="footer"><div id="footerinfo" align="center">Copyright Cook It Dot Com 2011 - Designed By Jahedul Hussain - </a></div></div>
</div>
</body>
</html>
Thanks A LOT!
How about something like
if(!isset($_SESSION["user"]))
{
header("Location: homepage.php");
}
Basically, if there is not a user session redirect to the homepage.
The exit() or die() functions, or, if you want to show certain content, a simple if statement containing HTML. E.g.
<?php if($logged_in) { ?>
<p>You are logged in!</p>
<?php } else { ?>
<p>You're not logged in. Go to the login page.</p>
<?php } ?>

Categories