How generate .php files that refer to the same head.php file? - php

Here is what I have so far
file_put_contents('./'. $dir .'/'. file .'.php','<?php include "../head.php"; echo $head; ?>' . $body);
I also tried
file_put_contents('./'. $dir .'/'. file .'.php','<?php include "../head.php"; ?>' . $body);
and then echo $head; in the /head.php
I want a head.php that is called by each of the generated files. There for I need to insert the php code <?php include "../head.php"; ?> on top of the files.
with the given code <?php include "../head.php"; ?> it shows  on top.
head.php
echo '<html>...';
echo $_SERVER['REQUEST_URI'];
...
The issue is, it shows .
I don't think this is a dupe.

Related

Why my php included file changes does not reflect

I made a website in php and created three include files.
Header.php
Footer.php
and my content files.
about.php, our-services.php, contact-us.php and 404.php
and called these inside my default.php
<?php include('includes/header.php'); ?>
<div id="main">
<?php
$url = '';
$urlX = 'includes/404.php';
if (!empty($_GET['post_slug'])) {
$url .= 'includes/';
$url .= $_GET['post_slug'] . '.php';
$GetPage = $_GET["post_slug"]; // validate string!!
if (!file_exists('includes/' . $GetPage . '.php')) {
include $urlX;
}
else{
include $url;
}
}
?>
</div>
<?php include('includes/footer.php'); ?>
my .htaccess file is coded like this
<IfModule mod_rewrite.c>
RewriteRule ^pages/([A-Za-z0-9-]+)/?$ /default.php?post_slug=$1
[NC,L]
</IfModule>
Now my about.php file has some basic information.
so when i write domain.com/pages/about it shows my about.php and it works :)
the problem is when I make changes inside header or footer or in any file, my changes are not reflected. I have to press ctrl + f5 on every page. If I am at domain.com/pages/about it will reflect the changes on this page only so when i go to another page, the header and footer shows previous cached files and i have to again press ctrl + f5 on every page to see the changes. Every change on any included file doesn't reflect... please let me how to fix this problem.
Thanks
I have tried to add clearstatcache(); like this
<?php
clearstatcache();
$url = '';
$urlX = 'includes/404.php';
if (!empty($_GET['post_slug'])) {
$url .= 'includes/';
$url .= $_GET['post_slug'] . '.php';
$GetPage = $_GET["post_slug"]; // validate string!!
if (!file_exists('includes/' . $GetPage . '.php')) {
include $urlX;
}
else{
include $url;
clearstatcache();
}
}
?>
but doesn't work...

Print name of current page through included <head> file

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

Logged in user name and last name wont show on the navbar

I'm a beginner in php and I want the name and last name of the logged in user to show on my navbar. Instead it shows the php code. This is my code inside the navbar.
<?php
echo "<p>" . $_SESSION['vorname'] . " " . $_SESSION['nachname'] . "</p>";
?>
This is what it shows instead.
The output on the screen.
I changed the file extension to .php and this is what it outputs now.
PHP-Files have to be saved with the .php file extension to be executed. I suppose the other working files all have the right file extension?
You have to save all files with php code as .php.
Inside them you can use clean HTML:
<html>
<head></head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
or HTML mixed with PHP:
<html>
<head></head>
<body>
<?php echo '<h1>Hello, World!</h1>'; ?>
<?php echo '<p>You are logged as '.$_SESSION['vorname'].' '.$_SESSION['nachname'].'</p>'; ?>
</body>
</html>
And remember to use PHP server (XAMPP, WAMP) on localhost - PHP files will not work if you open them directly in web browser (i.e. file:///C:/xampp/htdocs/hello/index.php )
The notice suggests that session is not started in your script, so you need to call session_start() prior to accessing $_SESSION variable. It would also be wise to check whether the user is logged in or not:
<?php
session_start();
if (isset($_SESSION['vorname'], $_SESSION['nachname'])) {
echo "<p>" . $_SESSION['vorname'] . " " . $_SESSION['nachname'] . "</p>";
} else {
echo "<p>No user is logged in</p>";
}
?>

How i include /master/header.php in /about/index.php?

How i include /master/header.php in /about/index.php?
localhost:8888/about/index.php:
<?php
include '/master/header.php';
?>
<?php
include '../master/header.php';
?>
Two dots mean back to main folder
Or:
include($_SERVER["DOCUMENT_ROOT"] . '/master/header.php');

echo name of file 'including' not 'included'

Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>

Categories