I have 3 php pages.
Counter.php - I want this file to display how many clicks Counter 2, Counter 3 got.
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visited page Counter2
<?php include 'Counter2.php'; echo $Counter; ?>
Times.
You visited page Counter3
<?php include 'Counter3.php'; echo $Counter3; ?>
Times.
</body>
</html>
Counter2.php - I want to count the amount of click this page has had.
<?php
session_start();
if(isset($_SESSION["counter"]))
{
$counter = $_SESSION["counter"];
}
else
{
$counter = 0;
}
$_SESSION["counter"] = $counter + 1;
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
<?= $counter ?>
</body>
</html>
Counter3.php - I want to count the amount of click this page has had.
<?php
session_start();
if(isset($_SESSION["counter3"]))
{
$counter3 = $_SESSION["counter3"];
}
else
{
$counter3 = 0;
}
$_SESSION["counter3"] = $counter3 + 1;
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visted this page this many times:
<?= $counter3 ?>
</body>
</html>
Now, everything is working how it should be, HOWEVER, when I include the php files <?php include 'Counter3.php'; echo $Counter3; ?>
to log the amount of clicks it continues to increase when I load the Counter.php file. I do not want it to, I just want Counter.php to log how many clicks Counter2 and Counter3 has had.
And stop Counter.php increasing the click number.
You can use $_SERVER['REQUEST_URI'] to add an extra if to both counter2.php and counter3.php, and if the $_SERVER['REQUEST_URI'] points to counter.php, you can skip incrementing the counters
There are several options for this problem:
Save both values in a database and access the values through counter.php, instead of including the files
Use $_SERVER['REQUEST_URI'] to let the counter only increment if the URI does not contain counter.php
Including a page makes the code run, so it would increase all the counters in the pages. Instead just use the $_SESSION[] variable directly.
<?php
session_start()
?>
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
You visited page Counter2 <?php echo $_SESSION["counter2"]; ?> Times.
You visited page Counter3 <?php echo $_SESSION["counter3"]; ?> Times.
</body>
</html>
Also, in PHP you can do the following:
$_SESSION["counter3"] = isset($_SESSION["counter3"]) ? $_SESSION["counter3"] : 0;
//$_SESSION["counter3"] = $_SESSION["counter3"] ?? 0; // only in PHP 7
Also on a side note, you don't have to create this code on every page as it might get messy in the long run.
<?php #counter.php
if(session_status() == PHP_SESSION_NONE){
session_start();
}
if(isset($_SESSION['counter'][$_SEVER['REQUEST_URI']])){
$_SESSION['counter'][$_SERVER['REQUEST_URI']] = $_SESSION['counter'][$_SEVER['REQUEST_URI']] + 1
} else {
$_SESSION['counter'][$_SERVER['REQUEST_URI']] = 1;
}
function countpagetimes(){
return $_SESSION['counter'][$_SERVER['REQUEST_URI']];
}
?>
<?php #somepage.php
include 'counter.php';
echo 'You visited this page ' . countpagetimes() . ' times'.
?>
Related
somehow I couldn't find an answer to the following problem when searching the web.
I'm not familiar with PHP and am trying to get the below PHP code, which I put right at the beginning of the file, to display the HTML page that follows, instead of "Welcome.".
Many thanks for your time!
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo "Welcome.";
?>
<!DOCTYPE html>
<html lang="De">
<head>
<meta charset="UTF-8"/>
...
I found the solution...
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo <<<HTML
--> HTML section/code <--
HTML;
?>
So I would like to print name of the current page in the title tag of the head.
I include my head in every page like this:
include 'includes/head.php';
This is my head:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
I thought this would work, but now it just shows "Head" on every page.
I know I can make it work by just putting the $page variable on every page but I would like to prevent this.
So is there any way to print the name of the current page through the included head.php file without adding anything to every page?
Thanks
EDIT
This is how I fixed it:
$page = pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME);
If you were to create a file head.php with the following content
<?php
$xpage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_BASENAME );
$ypage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME );
echo "
<!--
with extension
{$xpage}
without extension
{$ypage}
-->";
?>
and include in your regular php pages using include '/path/to/head.php' you should get the desired result ~ you will see two options - with or without file extension.
To add this to the document title simply echo whichever option is preferrable
<title><?php echo $ypage;?></title>
Try this enclosing the $page variable in php tags:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
You have several problems, first one is curly bracket in front of the echo, the second one is that end title tag is missing forward slash and probably last one is that page variable is not inside php tags...
So your code should look like:
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?></title>
You could use a function like this:
head.php
<?php
function head($page) {
echo "<head>";
echo "<title>".ucfirst($page)."<title>";
echo "</head>"
}
index.php
<?php
include 'includes/head.php';
$page = basename(__FILE__, '.php');
head(page);
I am trying to use PHP include on my website but I am running into trouble...
To start this is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Future | Origins</title>
</head>
<body>
<?php
if (isset($_GET['p'])) {
$p = $_GET['p'];
include('include/header.php');
switch($p) {
case "about":
include('include/about.php');
break;
default:
$p = "include/main.php";
break;
}
include('include/footer.php');
}
?>
</body>
</html>
It only shows content for my index page if my URL is styled like
localhost/index.php?p=
I would like it to show content for my index page using the default
localhost
I don't have much knowledge of PHP and there never seems to be a straightforward solution. I am including multiple pages in my website using a switch case.
Many thanks
Just move your includes outside the if statement
<body>
<?php
include('include/header.php');
if (isset($_GET['p'])) {
$p = $_GET['p'];
switch($p) {
case "about":
include('include/about.php');
break;
default:
$p = "include/main.php";
break;
}
}
include('include/footer.php');
?>
</body>
I have already search about this question,I want to remove line break from the html,but there a some split php line in the html file,for example:
<html>
<head>
<title><?='abc'?></title>
</head>
<?php
if($_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68'?>
</body>
</html>
how can I remove line break from this php file?
You could use ob_start();
<?php ob_start();
//Start page output
?>
<html>
<head>
<title><?='abc';?></title>
</head>
<body>
<?php
if(isset($_SESSION['test']) && $_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68';?>
</body>
</html>
<?php
//End page output and assign the contents to a variable
$buffer = ob_get_contents();
ob_end_clean();
//Replace the new line with null
echo str_replace("\n",null,$buffer);
?>
Result:
<html><head><title>abc</title></head><body>1234564356713245tryt57u68</body></html>
If you want it in your IDE:
Use replace function, and replace \n by no character
If you want it for the client:
You have to configure your htaccess to remove the lines breaks/indenting when sending the file to the client, ie by using the mod_pagespeed extension:
http://www.the-art-of-web.com/system/mod-pagespeed-settings/ and the collapse_whitespace option
I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page
<?php $title = 'myTitle'; include("header.php"); ?>
And the following on my header page
<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>
But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?
thanks
This works (tested it - create a new folder, put your first line of code in a file called index.php and the second one in header.php, run it, check the title bar).
You should double check if those two files are in the same folder, and that you're including the right header.php from the right index.php. And ensure that $title is not being set back to null somewhere in your code.
Learn more about Variable Scope here.
Edit: Examples of visible changes would be:
TEST1<?php $title = 'myTitle'; include("header.php"); ?>
<title>TEST2<?php if ...
Are you including the header file before or after you set the title variable? If you're including it before, then of course it won't be set.
if you're doing something like this in your index.php:
<?php
include('header.php');
$title = "blah blah blah";
?>
then it won't work - you include the header file and output the title text before the $title variable is ever set.
try to declare the variable before using it
$title = '123';
require 'includes/header.php';
Hi Try this old school method ..
In your Header file (for e.g. header.php)
<?php
error_reporting(E_ALL);
echo '<!DOCTYPE html>
<!--[if IE 7 ]><html class="ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie8" lang="en"><![endif]-->
<!--[if IE 9 ]><html class="ie9" lang="en"><![endif]-->
<!--[if (gte IE 10)|!(IE)]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<!--<![endif]-->
<head>';
?>
<?php
if($GLOBALS['title']) {
$title = $GLOBALS['title'];
} else {
$GLOBALS['title'] = "Welcome to My Website";
}
if($GLOBALS['desc']) {
$desc = $GLOBALS['desc'];
} else {
$desc = "This is a default description of my website";
}
if($GLOBALS['keywords']) {
$keywords = $GLOBALS['keywords'];
} else {
$keywords = "my, site, key, words";
}
echo "\r\n";
echo "<title> ". $title ." | MyWebsite.com </title>";
echo "\r\n";
echo "<meta name=\"description\" content='". $GLOBALS['title']."'>";
echo "\r\n";
echo "<meta name=\"keywords\" content='".$GLOBALS['title']."'>";
echo "\r\n";
?>
In you PHP Page file do like this (for example about.php)
<?php
$GLOBALS['title'] = 'About MyWebsite -This is a Full SEO Title';
$GLOBALS['desc'] = 'This is a description';
$GLOBALS['keywords'] ='keyword, keywords, keys';
include("header.php");
?>
I assume your header is stored in a different file (could be outside the root directory) then all the above solutions will not work for you because $title is set before it is defined.
Here is my solution:
in your header.php file you need to set the $title to be global by: global $title; then echo it in your title so:
<?php global $title; ?>
<title><?php echo isset($title) ? $title : "{YOUR SITE NAME}"; ?></title>
Then in every page now you can define your title after you have included your header file so for example in your index.php file:
include_once("header.php");
$title = "Your title for better SEO"
This is tested and it is working.
We can also use functions and its a good way to work on real time web sites.
Do simple:
create an index.php file and paste these lines:
<?php include("title.php");?>
<!doctype html>
<html>
<head>
<title><?php index_Title(); ?></title>
<head>
</html>
-- Then
Create a title.php file and paste these lines:
<?php
function index_Title(){
$title = '.:: itsmeShubham ::.';
if (isset($title)){
echo $title;
}else{
echo "My Website";
};
}
?>
It will work perfectly as you want and we can also update any title by touching only one title.php file.
<?php
echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>
This works. Since I'm using PHP I don't check for other extensions; use pathinfo['extension'] in case that's required.
You can achieve that by using define(); function.
In your header.php file add following line :
<title><?php echo TITLE; ?></title>
And on that page where you want to set dynamic title, Add following lines:
EX : my page name is user-profile.php where I want to set dynamic title
so I will add those lines that page.
<?php
define('TITLE','User Profile'); //variable which is used in header.php
include('header.php');
include('dbConnection.php');
?>
So my user-profile/.php file will be having title: User Profile
As like this you can add title on any page on your site
Example Template.php
<?php
if (!isset($rel)) {$rel = './';}
if (!isset($header)) {
$header = true;
?><html>
<head>
<title><?php echo $pageTitle; ?></title>
</head>
<body>
<?php } else { ?>
</body>
</html><?php } ?>
Pages Your Content
<?php
$rel = './'; // location of page relative to template.php
$pageTitle = 'This is my page title!';
include $rel . 'template.php';
?>
Page content here
<?php include $rel . 'template.php'; ?>
I'm using your code in my project and it works properly
My code in header:
<title>
<?php
if (isset($title)) {echo $title;}
else {echo "عنوانی پیدا نشد!";}
?>
</title>
and my code in index.php:
<?php
$title = "سرنا صفحه اصلی";
include("./include/header-menu.php");
?>