Execute PHP on clicking a link - php

My website's pages are (or will) all be identical, except for the content of the #main div. So I was thinking in the <head> I could do <?php $page = "home.html"?>, where home.html is the bare contents (not a full html page - no <html>, <head> etc.) of the main div for the home page. So then inside <div id="main"> and </div> I could have <?php include($page);?>. Then the reason I'm asking this question is I want to change $page when a link is clicked. How can I do this? Thanks.

You could make the links be like this:
Other Page
Then also in the top of the page where you set page to the value of $_GET['page']
<?php
if (isset($_GET['page']))
{
$page = $_GET['page'] . ".html";
} else {
$page = "home.html";
}
?>

Your link has to be like:
home.php?page=test
You will get the value in your page variable like this:
if(isset($_GET['page']))
$page = $_GET['page'].'.html';
else
$page = 'index.html';

You can do something like:
http://yoursite.com/index.php?page=test.html or http://yoursite.com/index.php?page=test (and append .html later).
For instance:
<?php $page = preg_replace('/[^A-Za-z]/','', $_GET['page']);
$path = 'pages/'.$page.'.html'
if (file_exists($path)){ $output = file_get_contents($path); }
else { header("HTTP/1.0 404 Not Found");
$output = file_get_contents('pages/404.html'); }
?>
(Putting the rest of your file after the php, for the 404 header to work.)
Then, you also can use apache rewrites:
Rewrite Engine On
Rewrite Rule ^([A-Za-z]+)$ index.php?page=$1
Then use links like http://mysite.com/page_name
Then in the main div:
<?php echo $output; ?>

Related

Display static items according to page accessed, in PHP

I need to include some js, css type items in specific pages.
So far I've done the following:
define("DIR_ADMIN", "admin");
function fusion($location, $page, $type) {
$dirAdmin = DIRECTORY_SEPARATOR.DIR_ADMIN;
$change = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $location);
$changed = ucfirst($change);
$result = $dirAdmin.$changed;
if($type=='js'){
echo "<script src='$result'></script>\n";
} elseif($type=='css'){
echo "<link rel='stylesheet' href='$result' />";
} elseif($type=='favicon'){
echo "<link rel='shortcut icon' href='$result' />";
} elseif($type=='logo'){
echo "<img src='$result' alt='logo'>";
} else {
echo $result;
}
}
In HTML I called the function like this:
<?php fusion("/assets/vendors/js/vendor.bundle.base.js", "clients", "js"); ?>
First is the file URL, then the page, and finally the file type.
What I'm not able to come up with is the part that receives the value $page.
I created a routing system in php, so all files are accessed like this: index.php?page=clients, except in the case of the homepage which is just index.php.
I need that when the value of $page is set for example with clients it should load all items that have this same when the clients route is accessed.
In some cases the route may have another word attached, for example: index.php?page=clients-add, in which case it should check if there is only the word clients in $_GET.
And if I set the value of $page to all, it should display on all pages. How can I do this?
Within your fusion function you need to pick up which page you're on and then including the displaying of content after that.
Use this snippet to help dictate the page you're on.
$curr_page = "home";
if ( isset( $_GET["page"] ) ) {
$curr_page = $_GET["page"];
}
if ( $page == "all" || $page == $curr_page ) {
//echo items here
}

Load files in subdirectories in PHP

I have a basic semi-static website written in PHP. In the root folder I have a file called posts.php and also a folder called posts, in which there are post1.php, post2.php, and so forth and so on.
the posts.php file is in the root folder. When opened it creates a list from the files inside the posts folder and links to them.
In essence, what I want to do is to open php pages that I create statically and store in the posts/ folder in the browser.
The problem is that when I try to open these posts I am unable to. I can hard-link to them, and this "works", but if I do so my base templating will not work.
When I click links I go from one page to the next, and the URL shows ?p=index or ?p=posts. post1.php should be in something like ?p=posts/post1, but it doesn't work.
There may be a problem in naming, since there is a folder and a php file with the same name (posts), but I'm not sure if that's it, nor how to work around it.
edit: Below are parts of the code that I believe pertain to this problem:
my index.php
<?php
require_once('functions.php');
require_once('header.php');
load_page();
// require_once('init.php');
require_once('footer.php');
?>
My header.php
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf8">
<title>Paulo RSS Alves</title>
</head>
<body>
<div>
<!-- these are working without any issue -->
<h1><a href='?p=index'>Paulo RSS Alves</a></h1>
<div class="bar">
<p>Posts</p>
</div>
posts.php:
<?php
$dir = scandir('./post');
foreach ($dir as $file)
{
$path_parts = pathinfo($file);
if ($path_parts['extension'] == 'php' and ctype_alpha($file[0])){
// the purpose of this code is to only consider files with
// the .php extension and to remove that extension from the url.
$file_f = str_replace('.'.$path_parts['extension'], "", $file);
echo '<li><a href='.'post/'.$file_f.'>'.$file_f.'</li>';
}
}
?>
and functions.php:
<?php
function load_page() {
(isset($_GET['p'])) ? $page = $_GET['p'] : $page = 'index';
if (file_exists($page) && $page != 'index'){
require_once($page);
} else{
require_once('init.php');
}
}
?>
init.php is merely a welcome screen.
and a schema of my filetree:
index.php
init.php
header.php
footer.php
functions.php
posts.php
posts/
post1.php
post2.php
You are not requiring from the posts folder
<?php
function load_page() {
$dir = './posts/';
(isset($_GET['p'])) ? $page = $_GET['p'] : $page = 'index';
if (file_exists($page) && $page != 'index'){
require_once($dir . $page);
// add directory ^^^^
} else{
require_once('init.php');
}
}
?>

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

Changing title based on included file in PHP

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

Categories