I was wondering how you guys set your page titles while you use global headers. I would like to be able to change my page title from page to page... for example, "Site Name : News Archives". Would the best way be to use JavaScript? If I did this with JS, would the new changes take effect in search engine results? Just wanted to get some input on this thought.
<?php
include('header.php');
switch($_GET['p']){
case "news":
include('news.php');
break;
default:
include('indexBody.php');
}
include('footer.php');
?>
You can define a variable BEFORE including the header.
This variable can be used then in header.php.
<?php
$pagetitle = "Site Name : News Archives";
include('header.php');
...
?>
If your looking to go down this road, perhaps try something like the following:
Store your variables in a data array and then pass them to your header/footer/content views before echo'ing out.
$data['title'] will be seen by your header and footer ect as $title
<?php
$page = (!empty($_GET['p'])?$_GET['p']:'index');
$data = array();
switch($page){
case "news":
$view = 'news.php';
$data['title'] = 'Site Name : News Archives';
break;
default:
$view = 'indexBody.php';
$data['title'] = 'Site Name : Home';
break;
}
echo load_view('header.php',$data);
echo load_view($view,$data);
echo load_view('footer.php',$data);
function load_view($path,$data) {
if (file_exists($path) === false){
return 'View not found: '.$path;
}
extract($data);
ob_start();
require($path);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
?>
Related
I have to make some changes on my old website where I'm not using any templating system. I'm loading the content for some pages from a database based on ?page parameter. So I have something like this:
<title>Page title</title>
...
...
...
$page_id = $_GET['page'];
include 'page.php'; //escaping is done in this file
Inside the page.php file I'm actually loading the information about the page. Based on this information I have to change the title of the main page.
I know that this design is not good at all and I wouldn't do this way these days, but to change everything on this website would be too complicated.
Thank you for your ideas.
Try to add php code before title
<html>
<?php
$page_id=$_GET["page"];
include('page.php');
echo "<title>".$page_title."</title>";
?>
<body></body></html>
Inside page.php:
echo '<script>
document.title = "This is the new page title.";
</script>';
How to dynamically change a web page's title?
Enjoy !
<?php
$page_id = $_GET['page'];
if ($page_id == 'first value') {
$title = 'first title';
} else {
$title = 'second title';
}
?>
<title><?php echo $title?></title>
...<?php
include 'page.php'; //escaping is done in this file
I have a web in different languages. I make an include for the footer. The easiest way could be to have a different footer for each language. But is it possible to have just one footer and change the few sentences that are different in each language?
In all pages put the same include:
<?php include('footer.php'); ?>
Then, in the includes just change what is different. Something like:
<footer>
<?php echo $text; ?> <br><br>
</footer>
</body>
</html>
<?php
if ('<html lang="en">')
$text = 'Some text in English';
elseif ('<html lang="fr">')
$text = 'Français';
?>
(In each page I have the html lang= )
What is the better way to have a footer in different languages?
(I am just learning php, so please, just help me with the basics, where to begin)
Okay so first you need to create translation files for all languages you wish to support. Store them in "/lang/en.php" and "/lang/fr.php".
"lang/en.php"
<?php
return [
"title" => "My site",
"welcome" => "Welcome",
"goodbye" => "Goodbye"
]
?>
"lang/fr.php"
<?php
return [
"title" => "Mon site",
"welcome" => "Bienvenue",
"goodbye" => "Au revoir"
]
?>
Next, you include the appropriate language file in your php page:
"index.php"
<?php
$locale = $_SESSION['locale']; // this is "en" or "fr", depending on a choice the user made earlier
$lang = require("/lang/$locale.php"); // load "/lang/en.php" or "/lang/fr.php"
$user = $_SESSION['username']; // e.g. "Bart"
?>
<html>
<head>
<title><?php echo $lang['title']; ?></title>
</head>
<body>
<?php include('header.php'); ?>
<main>content</main>
<?php include('footer.php'); ?>
</body>
</html>
And in your header/footer you can just use $lang as well:
"header.php"
<header>
<p><?php echo $lang['welcome'] . ', ' . $user; ?></p>
</header>
It's important to know that you should include your language file only in pages that a user will view directly (i.e. don't include it in partial views like header.php)
You can create a poor man's translation function:
function translate($sentence, array $vars = null, $lang = 'en') {
static $table = array();
if ( ! isset($table[$lang])) {
$table[$lang] = require(ROOT."/lang/{$lang}.php");
}
$trans = isset($table[$lang][$sentence]) ? $table[$lang][$sentence] : $sentence;
if ( ! empty($vars)) {
$trans = strtr($trans, $vars);
}
return $trans;
}
You can then create some language files, such as:
<?php
// ROOT/lang/de.php
return [
'Welcome :name' => 'Willkommen :name',
'Thank you' => 'Danke',
];
And then in your scripts you can translate stuff:
<header>
<?php echo translate('Welcome :name', [':name' => 'Bob'], 'de') ?>
</header>
Instead of using a function you could also just include the language file and then use that.
<?php
// some-page.php
$lang = require(ROOT."/lang/{$_SESSION['user.lang']}.php");
$name = $_SESSION['user.name'];
?>
<header>
<?php echo str_replace(':name', $name, $lang['Welcome :name']) ?>
</header>
It will require you to do some more work, but if you find it to be more to your liking then okay.
You could set a session that contains the language such as:
<?php
session_start();
$_SESSION['language'] = "EN";
?>
Then in the footer:
<?php
switch($_SESSION['language']) {
case 'EN':
$sentences['site_slogan'] = "This is your site slogan";
$sentences['site_messag'] = "This is your site message";
break;
case 'FA':
$sentences['site_slogan'] = "This is your site slogan in FA";
$sentences['site_messag'] = "This is your site message in FA";
break;
}
echo $sentences['site_slogan'];
echo $sentences['site_messag'];
I just created a new website using a tutorial as reference. My problem is mainly the title that doesn't change. But it's not possible to change anything either. Everything is static in the header defined.
I'm brand new to PHP, so any help appreciated.
This is my current code:
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
if (isset($_GET['content'])) {
$content = strtolower(trim($_GET['content']));
} else {
$content = '';
}
include_once 'inc/header.php';
switch ($content) {
case 'main':
include 'inc/main.php';
break;
case 'blog':
include 'inc/blog.php';
break;
case 'portfolio':
include 'inc/portfolio.php';
break;
case 'lebenslauf':
include 'inc/lebenslauf.php';
break;
case 'kontakt':
include 'inc/kontakt.php';
break;
default:
include 'inc/main.php';
}
$content = "main";
include_once 'inc/footer.php';
?>
Do you know how to edit this code to be able to change titles or meta tags dynamically or do you know a tutorial for something like a best practice include of PHP files?
Absolutely simplest bare-bones "dynamic" header:
header.php:
<title><?php echo $title ?></title>
index.php:
$title = 'Hello, world!';
include('header.php');
Try below Code.
<?php
$title = "";
$description = "";
$keywords = "";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($url, 'index') !== false){
$title = "Home | Your Site Name";
$description = "Your Site description ";
$keywords = "Your Site Keywords";
}elseif(strpos($url, 'about') !== false){
$title = "About Us";
$description = "About description ";
$keywords = "key words";
}
?>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
I'm not sure of the reason why you have routed to pages like this, however i'm guessing your titles/metas are in the header.php file.
To change them you can create an array for each like this:
$titles = array(
'blog.php' => 'title for blog',
'main.php' => 'title for main',
//etc
);
In your switch, assign a variable $include or something the correct page eg:
case:'main':
$include = 'main.php';
include(main.php);
break;
Then use that $include to get the title in your header.php, e.g:
<title><?php echo $titles[$include];?></title>
My current project routes everything through index.php. Then I see what the $_SERVER['REQUEST_URI'] is, check against a whitelist, then perform some logic to include the relevant html template. Like this:
<?php
include("header.html");
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}
include ("footer.html")
?>
My problem is that I want to use $page_title in the header template, like this...
<title><?php echo $page_title?></title>
But obviously it will not work because when the header is included, $page_title has not been set. Can anyone suggest how to get around this? Might "output buffering" help? If so, how?
If there is something majorly wrong with this way of using php to produce pages, please say so ! Thanks
EDIT: I should add, the conditional logic is quite a bit more complicated in my real project, and I've simplified it a lot here. I don't want to have to keep repeating my includes of header and footer, so that's why they're not included. I'd really rather keep them out of all the logic if possible.
You can reorder your code to achieve this:
<?php
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include("header.html");
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}else{
//Make sure we include header if page not in white list
include("header.html");
}
include ("footer.html")
?>
Edit: Actually thinking about it you could probably decouple the logic and the includes:
<?php
$requested_page = $_SERVER['REQUEST_URI'];
$includes = array();
$includes[] = 'header.html';
if ($requested_page in $whitelist){
$page_title = $requested_page;
$includes[] = $requested_page . "_controller.php";
$includes[] = $requested_page . "_template.html";
}
$includes[] = "footer.html";
foreach($includes as $include){
include($include);
}
?>
I wrote this for my load MVC library to load views, but I think this will become handy:
$target = "myfile.php";
$data = array(
'page_title' => 'page title'
);
ob_start();
if(count($data) > 0) extract($data);
include_once($target);
$content = ob_get_clean();
echo $content;
And you're able to use $data keys as variables with use of extract() function.
Easy
<?php
$requested_page = $_SERVER['REQUEST_URI'];
$page_title = "";
if ($requested_page in $whitelist) $page_title = $requested_page;
include("header.html");
if(!empty($page_title)) {
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
}
include ("footer.html")
?>
What about..?
<?php
$page_title = $requested_page = $_SERVER['REQUEST_URI'];
if (!in_array($requested_page, $whitelist)) {
header('Location: http://example.com/your404');
exit(0);
}
include 'header.html';
include "$requested_page_controller.php";
include "$requested_page_template.html";
include 'footer.html';
Try redirecting back to your index.php page with a $_POST or $_GET, with the page title.
If these are set, you can read them from the start of the page.
<?php
if (isset($_GET['pagetitle'])){
echo "<html><head><title>".$_GET['pagetitle']."</title></head>";
}
include("header.html");
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page in $whitelist){
$page_title = $requested_page;
include ($requested_page . "_controller.php");
include ($requested_page . "_template.html");
header( 'Location: http://www.yoursite.com/index.php?pagetitle='.
urlencode('This is the page title'),'"' )
}
include ("footer.html")
?>
I'am a newbie also my language maybe bad, and looking for solution for my learning php code
let say i have many page i.e. etc page1.php ... page1001.php each page maybe:
inside of page1.php:
$color = "red";
$pages = "two";
$theme = "ocean";
$lang = "eng";
$charset = "ISO-8859-1";
etc (more..)
inside of page2.php :
$color = "blue";
$pages = "two";
$theme = "ocean";
$lang = "it";
$charset = "UTF-8";
etc (more..)
now i need to put the variable of each pages to one page, n just put a simple code in each pages to setting them so next time easily to edit, note I using plain text (flat file)
anybody help me? i give appreciate and say thank you
You can try to use "include"
or you could use:
require('page1.php');
This will work similarly, but cause an error if the page cannot be located.
see: here
oh sorry maybe i give wrong explained:
I mean all content in page1.php up to page1001.php above i will move to one page call it as parameter.php
so parameter.php become:
<?
//the value of page1.php
$color = "red";
$pages = "two";
$theme = "ocean";
$lang = "eng";
$charset = "ISO-8859-1";
etc (more..)
//the value of page2.php :
$color = "blue";
$pages = "two";
$theme = "ocean";
$lang = "it";
$charset = "UTF-8";
//etc (more..)
//the value of page3.php :
$bla-bla = "bla-bla";
?>
now how to call the value of page1.php also page2.php in parameter.php above? if i using include "parameter.php"; in each page (page1.php up to page1001.php) sometimes not suitable to each of page...
Judging by your data, you could put your values for a certain page into an associative array such as this:
//Page1.php
$page1Information('color' => 'red', 'pages' => 'two', 'theme = 'ocean');
//Page2.php
$page2Information('color' => 'blue', 'pages' => 'five', 'theme = 'forest');
Then you can include all the pages in your parameter.php file, and call upon the data in any one file by going:
echo $page1Information['color'];
//Prints out "red"