Writing Head section with PHP - php

I'm converting a couple sites from HTML to PHP for dynamic elements and have been able to do so with the header and footer (using php include()). However, I'm confused on how to do the head section. This is what I have with plain HTML:
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="Liberty Resource Directory. The ultimate curated directory to find what you need."/>
<meta name="keywords" content="ethan glover, lrd, liberty resource directory"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
I can easily add the HTML5Shim script, meta charset, viewport (yes I will remove that max scale), and the stylesheet link.
Here's the problem:
How can I write the .php file in a way that I can pass an individual pages description, keywords and title to it? (That way I can put the entire above code in a php file and just include it on every page.)
Or will I simply have to exclude the description, keywords and title, and rewrite those parts every time?
Here's the answer: (Courtesy of Alejandro Arbiza)
head.php
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description;?>"/>
<meta name="keywords" content="<?php echo $keywords;?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title><?php echo $title;?></title>
</head>
index.html (to include above code)
<?php
$description="Liberty Resource Directory. The ultimate curated directory to find what you need.";
$keywords="ethan glover, lrd, liberty resource directory";
$title="Liberty Resource Directory";
include 'scripts/head.php';
?>
The final result:
http://libertyresourcedirectory.com/

You can use variables for the description and the keywords (or anything else you want for that matter). Then, when the time comes to construct the page, you would just set the variables with the corresponding values.
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description; ?>"/>
<meta name="keywords" content="<?php echo $keywords; ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
So, lets say you have page1.php and page2.php:
<?php
// page1.php
$description = "This is page one";
$keywords = "page one";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
and
<?php
// page2.php
$description = "This is page two";
$keywords = "page two";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
Of course, I am assuming here that the whole HTML header is inside the header.php file, that is including <html>, <head> and <body>.

Related

Japanese character encoding issue on web browser

When I query in MySQL then it returns in Japanese Character. An image is attached below.
But in browser its shown "????". An image is attached below.
Here I enclose my base.html code.
<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link media="screen" href="<?php echo $this->path_to('/common/css/import.css') ?>" type="text/css" rel="stylesheet" />
<script src="<?php echo $this->path_to('/common/js/jquery-2.1.0.min.js') ?>"></script>
<title><?php echo $this->v('page_title') ?></title>
</head>
<body>
<?php echo $this->part_of('body') ?>
</body>
</html>
Use mysqli_set_charset($db,'utf8'); in your dbconnection file. in my case $db is my connection variable

User not redircted back to last visited page after logged out

In my website I am trying to redirect uers back to the last page they visited after they log back in but I am not getting it. I used cookie to track their last visited page, and I created a redirect page to check if the cookie still exist and if it does it should take them to the last page they left off.
here is the header of my website which I included to all the pages:
include('../includes/admins.php');
// Set your cookie before redirecting to the login page
$current_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$_COOKIE['redirect_to'] = $current_page;
$cookie_name = $_COOKIE['redirect_to'];
$cookie_value = $current_page;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<meta name="description" content="Automating your school work.">
<meta name="author" content="<?php echo $set['siteName']; ?>">
<meta name="keyword" content="content, management, system, schools, school management system, schoool portal, chat, link, linkedln, <?php echo $set['siteName']; ?>">
<link rel="icon" href="<?php echo $set['installUrl'].'logo/'.$set['schoolLogo']; ?>" type="image" />
<title><?php echo $fullname; ?></title>
<!-- Favicon-->
<link rel="apple-touch-icon" sizes="57x57" href="images/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="images/apple-icon-60x60.png">
<link rel="manifest" href="images/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="images/ms-icon-144x144.png">
<meta name="theme-color" content="">
<!-- Google Fonts -->
<link href="../afiles/css/font.css" rel="stylesheet" type="text/css">
<link href="../afiles/css/icon.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="../afiles/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Bootstrap Core Css -->
<link href="../afiles/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="../afiles/css/bootstrap-material-datetimepicker.css" />
</head>
If you look at the top you will see I include the cookie.
Here is the redirect page to see if cookie exist:
<?php
include('../includes/functions.php');
if(!isset($_COOKIE[$cookie_name])) {
header("Location: main");
} else {
header("Location: ". $cookie_name);
}
?>
Need someone to point me to the right direction.
In your second block of code you have $_COOKIE[$cookie_name] but since you haven't defined $cookie_name in your file, !isset($_COOKIE[$cookie_name]) will always return false. Possible solutions:
Replace
if(!isset($_COOKIE[$cookie_name])) {
with
if(!isset($_COOKIE['redirect_to'])) {
To match the index value of the first block code.
Second option, if you think you might change redirect_to value, is to define a constant in a separate file and then use it in both of your blocks.

HTML header information appearing in body

I have created a simple webpage, which includes both a header and footer as separate php files, shown below
<?php
$PageName = "Home Page";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
this is the header
<?php
print("<!DOCTYPE html>");
print("<html lang='en-UK'>");
print("<head>");
print("<title>");
print($PageName);
print("");
print("</title>");
print("<meta http-equiv='content-type' content='text/html; charset=utf-8' >");
print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$CSSRoot = "/MyPage/StyleDefault.css";
print("<link rel='stylesheet' type='text/css' href=$CSSRoot>");
print("</head>");
print("<body>");
print("<h1>My Page</h1>");?>
and footer
<?php print("</body></html>");?>
but when I view it the header elements appear in the body as shown below
header information appearing in the body
I want to make clear this does not, yet, cause any problems, but I want to know what the cause is.
Thanks
EDIT
brain fart moment putting the code in the comments, sorry.
new index
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
new header
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
new footer
</body></html>
new output
<html lang="en-UK"><head></head><body>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/MyPage/StyleDefault.css">
<h1>My Page</h1>
</body></html>
this is how you should do this.
header.php
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
index.php
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
footer.php
</body>
</html>
This should work, I don't know why you would want to print all the html tags using php, while you can just output em normal.
I beleive I have found the problem, the sources panel in chrome shows that just before the doctype and end body tags are two characters (ie where the header and footer files begin), represented by red dots, according to http://apps.timwhitlock.info/unicode/inspect?s=%EF%BB%BF%EF%BB%BF this is a ZERO WIDTH NO-BREAK SPACE, I don't know how they appeared and I can't seem to get rid of them, but it seems likely that they are the cause of the problem.
UPDATE
having looked around the prolbem is that the php files where using UFT-8 BOM (byte order mark) which inserted the offending characters, changing it to UTF-8 (which is under encoding in Notepadd++) solved the problem, thanks for all the help

Display data in a section HTML

I have an array I want to select and display data from it depending to criteria in the sidebar.
I would like the show this data in a section in the body, and a change will be made only for this section when I select a criteria on the sidebar.
For the moment my solution is to create a page for each criteria and connect it with link to the criteria in the sidebar.
Normally there is a solution to show it dynamically
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="script.js"></script>
</head>
<body>
<?php
require_once ".\Classes\PHPExcel\IOFactory.php";
$objPHPExcel = PHPExcel_IOFactory::load("zfg01_CAT.xlsx");
$sheet = $objPHPExcel->getSheet(0);
include 'fonctionsUtiles.php';
afficherLesArticlesParGendre($sheet, "GARCON");
?>
</body>
</html>

php included file execution failure

Good afternoon everyone. I'm stuck on including a file that has all of my 'head' information in it. I wanted to break down my page so it's simpler and less complicated. in my index.php file i did this;
<?php
include 'Resources/includes/head.php';
?>
It finds the file and i know this because in dreamweaver when i go on my index page it comes up as a tab underneath it. However when i view my index page on the local server, it's as if none of the styles or scripts have been applied to the page. This is the code that my head.php file has in it.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="description" content="" />
<meta name="keywords" content=""/>
<link rel="shortcut icon" href="../images/design/icon.png" type="image/x-icon"/>
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen"/>
<script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="../js/scripts.js" type="text/javascript"></script>
</head>
You may try something like this by using a base path
<?php
$basepath="http://www.mysite.com/path_to_theme/";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="description" content="" />
<meta name="keywords" content=""/>
<link rel="shortcut icon" href="<?php echo $basepath; ?>/images/design/icon.png" type="image/x-icon"/>
<link rel="stylesheet" href="<?php echo $basepath; ?>/css/style.css" type="text/css" media="screen"/>
<script src="<?php echo $basepath; ?>/js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="<?php echo $basepath; ?>/js/scripts.js" type="text/javascript"></script>
</head>

Categories