PHP include not working for index page - php

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>

Related

How to properly display an HTML page after PHP script?

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;
?>

How to match half URL using switch case for dynamic page title in php?

I want to match URL in switch case for dynamic pages. But i am not able to print variable value in switch case.
here is my html title code in header:
<title><?php echo $pageTitle; ?></title>
and this is php:
$name=$_GET['id'];
switch ($_SERVER['PHP_SELF']){
case "/web/index.php":
$pageTitle = 'Home';
break;
case "/web/new_doc.php":
$pageTitle = 'Document';
break;
case "/web/new_provider.php":
$pageTitle = 'Provider';
break;
case "/web/read.php?id=$name":
$pageTitle = 'Reader';
break;
default:
$pageTitle ='Page';
i want to match URL like this:
read.php?id=amazon prime
book.php?id[]=A Wasted Hour&id[]=20
is there any way that i can match half part of URL in switch case like
read.php?id= instead of full URL.
Header HTML:
<head>
<title><?php echo $page_title; ?></title>
</head>
php code:
$page=$_SERVER['PHP_SELF'];
if($page=='/index.php')
{
$page_title = 'Home';
}elseif($page=='/read.php')
{
$name=$_GET['id'];
$page_title= 'Welcome '.$name;
}elseif($page=='/searchpag.php')
{
$page_title= 'Searchpage';
}

Returning Values php

All i know from the return statement (based on JS) that is stops execution of the script and return the value and whats after it ignored, then how is that possible
(in fact required) in this php code :
<?php
return "<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";
which is completing this :
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
$page = include_once "templates/page.php";
echo $page;
and also is it possible to have return outside a function ????
You were almost there...
With the template/page.php you don't need to run a return all you need to do is as follows:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
Then within the PHP page side of things lets say for example, phpCode.php:
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
include_once "templates.php";
Include will pull all of the information from the file you're including generally.
You can pull information from anywhere providing you have required the php file. PDO / OOP is a great example of how to do this and I'd highly suggest learning it somewhat more to achieve what you're trying to do (from what I could make out in the question).

PHP hit counter issue

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'.
?>

Dynamic Title Tag in PHP

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");
?>

Categories