PHP omit an include from specific page - php

I have a PHP site that includes a header (menu nav and logo).
I have a section in my site that I would like to include a DIFFERENT header
I am struggling with this. Any help would be appreciated!
Here is my code:
<div id="wrapper">
<? include('inc/header.inc.php'); ?>
<? require('process/build_page.php'); ?>
<? require('inc/footer.inc.php'); ?>
</div>
I want to include the following header on a specific section called "my-association"
<? include('inc/header_ma.inc.php'); ?>
The process/build_page code looks like this:
<?
global $full_uri;
class BuildPage
{
function BuildPage()
{
global $full_uri;
if($full_uri)
{
$this->build_path();
}
else
{
exit();
}
}
function build_path()
{
global $full_uri;
$uri_array = explode("/", $full_uri); # make uri into an array divided by /
$clean_uri = array_filter($uri_array); # remove empty elements
unset($clean_uri[1]); # remove base part of uri
$clean_uri = array_values($clean_uri); #reset array key to start with 0
# if array is empty the homepage is being requested
if(empty($clean_uri)){
$clean_uri[0] = 'home';
}
# build uri to point to include file
$new_path = "content/";
$new_path .= implode("/", $clean_uri);
$new_path .= ".inc.php";
$this->build($new_path);
}
function build($path)
{
$output = "";
$output .= $this->get_include_contents($path);
echo $output;
}
function get_include_contents($filename)
{
if (is_file($filename))
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
function print_array($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
die();
}
};
$buildpage = new BuildPage();
?>
Please advise!
Thanks

Please let me know if I've missed something but wouldn't this do the trick?
<div id="wrapper">
<?
$change_header = array('/special_page_1','/special_page_2','/special_page_3');
if(in_array($full_uri,$change_header))
include('inc/header_ma.inc.php');
else
include('inc/header.inc.php');
?>
<? require('process/build_page.php'); ?>
<? require('inc/footer.inc.php'); ?>
</div>

Related

Why output control doesn't catch sql?

I wrote a code which is changing headings h1 to h2 if it's not first heading of that type. It works, but only when I'm using just PHP. If I want to use it on website, which one is using database to output content, that code doesn't work.
I'm using ob_start() before <html> and ob_get_contents() + ob_end_clean() + rest of my code after </html>, so I think that has to be something wrong "catching" content from database, but I'm not sure. I tried to use that code on a website based on WordPress.
My code (I know it isn't probably the best solution, but it works when I'm using it on any website without CMS):
<!DOCTYPE html>
<?php
ob_start();
?>
<html>
<head>
<title>random page</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-5">
<h1>afdsfdassfdfdsa</h1>
</div>
</div>
</div>
<h1>sadffsadf afdsfdsa afsdfs?</h1>
<h1 class="superclass">random text?</h1>
<div>
<p>some ending content</p>
</div>
</body>
</html>
<?php
$bufferContent = ob_get_contents();
ob_end_clean();
$matchedContent = '';
$endContent = '';
$modifiedContent = '';
$firstHeadingBoolean = true;
$h1Pattern = array();
$h1Pattern[0] = '%<h1(.*?)>%';
$h1Pattern[1] = '%</h1>%';
$h1Replacement = array();
$h1Replacement[0] = '<h2$1>';
$h1Replacement[1] = '</h2>';
if(preg_match_all('%((.|\n)*?)(<h1.*?>.*?</h1>)%', $bufferContent, $contentMatches)){
foreach($contentMatches[0] as $matches) {
$matchedContent .= $matches;
}
$endContent = str_replace($matchedContent, '', $bufferContent);
foreach ($contentMatches[0] as $matches) {
if(!$firstHeadingBoolean){
$firstHeadingBoolean = false;
} else {
$matches = preg_replace($h1Pattern, $h1Replacement, $matches);
}
$modifiedContent .= $matches;
}
echo $modifiedContent;
echo $endContent;
} else {
echo $bufferContent;
}
?>
EDIT: I tried to use solutions from there but nothing has changed: WordPress filter to modify final html output
Now, after some testing, I can see it's not working because preg_match_all doesn't work correctly. Anyone has an idea what is wrong with that preg_match_all? I tested that regex pattern inside that in regex101 and on my localhost and everything worked fine. I don't understand, why isn't it working here?
Ok, so that wasn't problem with sql but with regex. To solve that problem I used code like that:
<?php
ob_start();
?>
<!-- html code -->
<?php
$bufferContent = ob_get_contents();
ob_end_clean();
$bufferContent = preg_split("%(?=<h1)%",$bufferContent);
$i = 0;
$h1Pattern = array();
$h1Pattern[0] = '%<h1(.*?)>%';
$h1Pattern[1] = '%</h1>%';
$h1Replacement = array();
$h1Replacement[0] = '<h2$1>';
$h1Replacement[1] = '</h2>';
foreach($bufferContent as $bufferElement){
if(preg_match("%<h1%", $bufferElement)){
if($i > 0){
echo preg_replace($h1Pattern, $h1Replacement, $bufferElement);
} else {
echo $bufferElement;
$i++;
}
} else {
echo $bufferElement;
}
}
?>

Best way for passing string while including file

I'm having a trouble with passing string while using include() function. I have index.php which serves as template file and is including any other pages.
For example, if I want to go to the home page, the address is http://somethingsomething.com/index.php?page=home and included file is pages/home.php. Bellow you can find how the template system works. I want to change of the page you're currently are but I don't know what is the best way to pass that string from the title of the page. So if I'm on the Catalogue page - http://somethingsomething.com/index.php?page=catalogue, it will include file pages/catalogue.php and I've tried to pass string as SESSION but it didn't worked. Could you please help me?
Source URL: http://somethingsomething.com/index.php?page=catalogue
index.php file
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
?>
</body>
</html>
pages/catalogue.php file
<?php
$title = "Catalogue";
?>
Something like this?
<?php
ob_start();
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
$output = ob_get_clean();
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
echo $output;
?>
</body>
</html>

setting a value for index.php page

is there a way set a value for the index.php page on its first load?
i would like index.php it to load like so "index.php?subject=1" when it loads for the first time.
as this value will change as they move around in the site i don't want it to be a fixed value.
some one sugested
if(empty($_SESSION['visited'])) {
//DO STUFF
$_SESSION['visited'] = true; }
i cant seem to get that to work with my function.
find_selected_page()
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_page = null;
$current_subject = null;
}
}
index.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/db/dbcon.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php');
$context = "public";
find_selected_page ();
?>
<html>
<head>
<title>Home</title>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/style.php'); ?>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');?>
<div id="p2dbg">
<div id="p2dcontent">
<div class="p2dcontent">
<h1><?php echo htmlentities($current_subject ["menu_name"]); ?></h1><br />
<?php if ($current_page) { ?>
<p><?php echo nl2br($current_page ["content"]); ?></p><br />
<?php } else { ?>
This page does not exist! please slect a page from the menu.
<?php } ?>
</div>
</div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>
</div>
</body>
</html>
Instead of setting everything to null if your value isn't passed, just assume the default value:
// Set this to the value you want as your default
define("DEFAULTSUBJECT",1);
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_subject = find_subject_by_id (DEFAULTSUBJECT);
$current_page = find_default_post($current_subject ["id"]);
}
}

Different page titles with one header file?

How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got
error_reporting(0);
if ($_GET["error"]=="404") {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
So i would have the header before anything else. So how would i manage to make so that
index?error=404 and index have different titles? Thanks in advance.
In overall_header.php
<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
$title = "Error";
}
?>
<title><?php echo $title; ?></title>
Use JavaScript and document.title.
Example:
<script language="javascript">document.title = "My Title"</script>
JS can be used in body.
Another method is to set a $GLOBAL variable before including everything.
Example:
error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") {
$GLOBALS['404'] = 1;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
$GLOBALS['404'] = 0;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
In your overall_header.php:
if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';
You could try a switch
<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
case "index.php":
echo "<title>My Homepage</title>";
break;
case "apples.php":
echo "<title>The Best Apples!</title>";
break;
case "bananas.php":
echo "<title>We Sell Bananas</title>";
break;
}
?>

Are PHP script read from Top to bottom?

Are PHP scripts reads from top to bottom? like HTML? because in this code
<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>
<?php
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
?>
<?php include("includes/header.inc.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class = "subjects">
<?php
$subject_set = get_all_subjects();
while($subject = mysql_fetch_array($subject_set))
{
echo "<li";
if($subject['id'] == $sel_subj['id']) {echo " class =\"selected\"";}
echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
echo "<ul class = 'pages'>";
$page_set = get_pages_for_subject($subject['id']);
while($page = mysql_fetch_array($page_set))
{
echo "<li";
if($page['id'] == $sel_page['id']){echo " class = \"selected\"";}
echo"><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if(isset($sel_subj)){?>
<h2><?php echo "{$sel_subj['menu_name']}";?></h2>
<?php } ?>
<?php if(isset($sel_page)){?>
<h2><?php echo "{$sel_page['menu_name']}"?> </h2>
<?php }?>
</td>
</tr>
</table>
<?php require("includes/footer.inc.php"); ?>
specifically this part
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
How is this if-else block being called if it's on top of the page?
It is run top-to-bottom one time per page view. On the initial view, assuming the URL has no parameters, then neither $_GET['subj'] or $_GET['page'] will be set.
If the link pointing back to the same page is clicked, then the entire PHP file will be reprocessed. If that link contained subj or page in the URL as a query variable, then the corresponding if block will be executed and the page will be altered accordingly.
Think of the PHP server as dynamically creating some HTML file that is sent to the web browser. Once it is sent, the server is done, and the PHP code is "gone." The only way to run more PHP code is to request a new page, where the process starts over.
(Even AJAX follows the same principles, although generally then you are dealing with partial data requests as opposed to full page views.)
Yes, scripts are run from top to bottom. I don't understand why you think that if-else block is any different? Those if clauses are run to set the $sel_subj and $sel_page variables before the rest of the page is executed and output.
PHP scripts are executed from top-to-bottom. What exactly is your problem?

Categories