Php include not executed - php

I'm learning Php
And i made a main index.php page
And at some point it contains the line
<?php
include './BaseTemplate.php';
?>
BaseTemplate.php contains a lot of common plain html that is equal between all pages.
It looks like (but these are only a few lines) :
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
But these echo commands dont get executed how should i resolve this ?

When you are including any file into your page the code for that file will come into the page.
So if the included file and the page where you are including it are in not in same path will create a problem.
It seems that you are getting that only. So please make the path in BaseTemplate.php according to the index.php, and it will work.

I solved it. I was thinking that include would put all code in a page from an external php source file. That's not the case, include files are libraries with functions which can be called.
So to resolve it, I rewrote it the BaseTemplate.php like this
<?php
Function WritePageBase() { // now i put it all inside a callable function
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
}
?>
Now my other pages can call it like
<?php
include './BaseTemplate.php'; // include the file with functions
WritePageBase(); // call the function to echo all html code in page
?>
which reduces a lot of html code which now can be edited by a single file (and all other pages using it will change too).

Related

How can I stop html outputting?

I am writing an anti-ddos php script, and I want to write a custom load page, but the html page of the orginal html page would be rendered. Is there anyway for me to prevent the html showing up while showing up my php load page
<?php require "antiddos.php"?>
#Samuel Wang, you can try check that out by one of this examples.
below assume code of antiddos.php and other file from where you include it.
1. antiddos.php :
E.g. before your php code or you can keep html inside the file that won't be an issue.
<?php
echo"<p>This is a test html content of antiddos.php</p>";
function testcall($TextReceiver ){
return $TextReceiver;
}
function MathMultiplication($Prt1,$Prt2){
return $Prt1*$Prt2;
}
?>
2. testprocess.php :
You can use the ob_clean(); function next to your include file statement that will discards the contents / html output from the antiddos.php.
<?php require_once("antiddos.php");ob_clean();
echo testcall("Hello this is print call from testprocess.php to antiddos.php");
echo "<br>";
echo "MathMultiplication= ". MathMultiplication(4,3);
?>
Conclusion by both the files output: First from the antiddos.php and second one from the testprocess.php

How to printf an include html

I included an html file like this, so that it is not displayed when the site loads:
<div id="menugrp0" class="menuhide">
<?php include 'menugrp0.html'; ?>
</div>
Now I want it to be shown at a specific spot. I am using this php code, to get some variables which are transported with the $_SESSION. I am using this kind of question for some simple html links, in which case it works perfectly:
if ($_SESSION['gruppe'] == $h['gruppe']) {
printf(' menugrp0.html');
}
I know that this is not working at all at the moment for this included html. I also tried to add the <?php [...] ?> tag inside the printf, which is also not working.
Is it possible to show a hidden included html file with a printf tag?
Try this one.
<?php
if($_SESSION['gruppe'] == $h['gruppe']){
echo 'Foo';
include ('/path/to/menugrp0.html');
echo 'Example: one';
}
?>
readfile('menugrp0.html'); // Reads a file and writes it to the output buffer. It is like read then "echo"
How to echo the whole content of an .html file in php?
Thanks to Daan Meijer, this works.
echo file_get_contents('...');
}

Generating PDFs from template files using wkhtmltopdf

I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>

how to echo the file name that includes a file [changing page title depending on file running]

I tried using php predefined variables but no function seems to work for me.
index.php, page2.php, page3.php contains:
include 'header.php';
now, i want my header.php to echo different title tags depending which file called it.
I tried running echo FILE but echo header.php, rather than index.php or page2.php,.. etc.
is there a way to handle this?
What you could do is set a variable like $title on every of your pages (i.e. index.php, page2.php and page3.php) with your desired title and then echo this variable in the header.php like this (example for index.php):
$title = "Welcome Page";
include("header.php");
And in header.php:
echo $title;
To my knowledge there is no way in determining which file A included an other file B from within file B. For the opposite, determining all included files there however exists the function get_included_files().

Using .php parsed as javascript source, how to wrap javascript code between php brackets using conditionals

Currently i'm working on a project which requires dynamical content parsed using some simple javascript functions, which i need to load depending on which 'view' is currently using a given user.
I'm having issues using something like this on a .js or .php file:
** .php file that calls javascript.
<script type="text/javascript" src="*.js/.php" />
** .js file
<?php header(Content-type: 'application/javascript'); ?>
alert('this shows correctly);
<?php if(isset($_REQUEST['view']) {
if($_REQUEST['view']=='private_view') { ?>
var xname = <?php echo $_SESSION['x']; ?>
//Array stuff filled from MySQL queries.
<?php } ?>
<?php if($_REQUEST['view']=='public_view') { ?>
var other_variable = <?php echo $xxx; ?>
$(element).functions();
<?php } ?>
The php code i'm using works correctly, already tested it. I'm also convinced that there are other ways to work this around, like using different files and choosing them with a conditional right on .php where I define script call, but i'm so curious about why this isn't working.
I'm used to work like this wrapping HTML content between brackets, to hide or show depending on given conditions.
The output of this will be only alert call, no PHP error/warning/notification,
I can't seem to find a correct way to do this, have been searching for a while but only find how to parse .js as php modifying .htaccess file.
You should simply rename the .js file to a .php file.

Categories