I'm not really familiar with php and would like to ask you to help me.
I want to insert css styling data from styles.css file into the <style> tag inside of index.php, and not sure how to do so with secure and optimized way.
Should I use <?php include 'styles.css;?> or <?php require 'styles.css;?> or what exactly should I do in order to insert the data in the optimized and secure way, please?
I use the readfile() for this. I think it does not load the whole file into the memory, so it is safer for outputting a large files. So the code can be:
<?php readfile('styles.css') ?>
require/include is for parsing a PHP file, not copying content.
Just read the file and echo the contents.
<?= file_get_contents('styles.css') ?>
where <?= is a shorthand for <?php echo.
Note that the include path in PHP environment may be different from that in the HTML, as understood by the browser. Better use __DIR__ . '/styles.css' to prevent problems.
Related
I have several html documents which share the same header and footer and would like to extract header and footer to separate html files and include them to other html documents.
The idea is very simple: when I change something in header, the change has to be made on all pages.
Until now I used PHP include function. But I have read some articles that this has impact on performance.
What is the best way to do this for clean html pages ?
Is the PHP the way to go ?
If it is, should I insert the whole html content into PHP echo ?
There isn't really a good way doing it in pure HTML, since iframe or an ajax-request using Javascript wouldn't be a good solution here.
I would say the best way is to use the PHP include or PHP require-function:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
You won't see any performance issues. Remember that include is a little bit faster than include_once, since include_once will have to make an extra check if the file has already been included.
You want to include header in all html pages, so php include is better.
no need of echo, just create a header.html(or any html file).
Then use
<?php
include "header.html"; //or name of your headerfile
?>
add this php code to all your pages.Thus all pages will point to same header file, and i dont think it will affect performance. Same idea for footer too :)
I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to #br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.
<p><?php footertext.php ?></p>
is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).
How can I go about doing this? I'd prefer an answer involving PHP.
UPDATE:
This is the exact code I'm using. Everything is in the same directory.
Main File:
<html>
<head>
</head>
<body>
<p> Content: <?php include "footertext.php ?></p>
</body>
</html>
Footer Content:
echo 'Test numba TWO!';
Use the include function. Just give it the path to your file. Example:
<?php include "footertext.php"; ?>
There are a few other functions that do similar things, such as require_once(). You can read more about that here.
Response to update
You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.
The best fit solution for your query is include or require functions of php. now you need to identify which one out of those are your choices based on their functional behavior.
PHP include and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename.ext';
or
require 'filename';
You may like to go through the details of
Include,
Require,
Require_once &
Include_once.
Enjoy!
Anand Chavan
Is it possible to include a php file without including its contents? I just want to access the functions and variables in that file without displaying any content. I tried this
<?
ob_start();
include('$file');
ob_end_clean();
?>
But this will hide only contents in php tag. I want to know how to hide others as well.
How to hide? Redesign your solution and separate your concerns! Do not mix logic with UI and so on.
Maybe you should apply the MVC or similar pattern(s).
While I totally agree with Peter's answer
I just tried this because I've never tried it before..
File toinclude.php:
<p>Loads of text</p>
<?php
function my_test()
{
echo 'Hello';
}
?>
Ooh a link
File includer.php:
<?php
ob_start();
include('toinclude.php');
ob_end_clean();
my_test();
?>
And it does work!
Output:
Hello
No you can't.
Include is meant to execute everything inside the file, there are no ways to prevent execution of some part of the file.
The only way is to edit the included file.
If your file is not printing any output You may try eval($fileContent).
Am sure the question is vague.
Let me try to explain.
Assume zend frame work - PHP - jquery combination.
I include jquery files in layout.phtml.
i include some files in controller.php.
some file in view.phtml
Atlast when i run and view the page . Is there any way or any tool to find which file is included through which file (layout controller or view) ??
In addition can some one explain which is the best way include js files and where . using zend framework in layout or controller or view
The only way to find where a public, static asset (JS, CSS, image, etc) is included is to trawl through the source code (using something that can "find in files" would save time).
In regards to how and where to include such assets... for global includes (common stylesheets, scripts, etc), include these in your layouts.
For specific page includes, place these in your views.
The best way to include a static asset is using the appropriate view helper. These are generally displayed in your layout file, for example
<?php echo $this->doctype() ?>
<html>
<head>
<?php
echo $this->headMeta()->prependHttpEquiv('Content-Type', 'text/html; charset=' . $this->getEncoding());
// I use "prepend" here so it comes before any page specific stylesheets
echo $this->headLink()->prependStylesheet($this->baseUrl('/css/common.css'));
echo $this->headScript();
?>
</head>
<body>
<!-- content -->
<?php echo $this->inlineScript() ?>
</body>
</html>
You can then add to these placeholders in your view scripts, for example
<?php
// index/index.phtml
$this->inlineScript()->appendFile('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js')
->appendFile($this->baseUrl('/js/my-jquery-script.js'));
To "include" a file means very different things in PHP (where it is analogous to copying and pasting source code from another file into the current file) and HTML/JavaScript (where you are referencing another file, which the browser must make a separate HTTP request to download). What do you consider "including"? Are image tags "including" the images? At least we can easily count those references by examining HTTP requests; from the client side, it's impossible to tell what include()s went into the source code behind the rendered output. Even naive source code searching couldn't tell you thanks to autoloading. As is, your question is not well enough defined to provide a clear answer.
Controversal answer:
You don't need that.
If you need that then it's something wrong with the way your designed your application.
Note: I've learned (trial and error) that 90% of things I don't know how to do and that seem to be impossible in ZF are a result of wrong application design.
I am wondering how I can break up my index.php homepage to multiple php pages (i.e. header.php, footer.php) and build a working index.php page using those separate php pages. I know WordPress uses this with different functions like:
GetHeader();
GetFoodter();
But when I tried to use those functions, it errors. I am guessing they are not native functions to PHP.
What would I need to do to get this functionality?
include 'header.php';
include 'footer.php';
Go with an MVC framework like Zend's. That way you'll keep more maintainable code.
You could do the following:
<?php
include('header.php');
// Template Processing Code
include('footer.php');
?>
The include() statement includes and evaluates the specified file.
so if you create index.php as:
<?php
include("1.php"); include("2.php"); include("3.php");
?>
processing it will combine three php files (result of parsing them by php) into output of your index.php ... check more at http://pl.php.net/manual/pl/function.include.php
Also, if i recall correctly, you can also use
<?php
require('filename');
?>
the difference being, if php can't find the file you want to include, it will stop right there instead of keep excecuting the script...
If your server is configured accordingly, you can use PHP's built in auto append/prepend settings and set it in a .htaccess file:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
Info:
www.php.net/manual/en/configuration.changes.php#configuration.changes.apache
www.php.net/ini.core#ini.auto-prepend-file
www.php.net/ini.core#ini.auto-append-file
I realize this is an old question, which already has a perfectly valid accepted answer, but I wanted to add a little more information.
While include 'file.php'; is fine on it's own, there are benefits to wrapping these sorts of things up in functions, such as providing scope.
I'm somewhat new to PHP, so last night I was playing with breaking things into files such as 'header.php', 'footer.php', 'menu.php' for the first time.
One issue I had was that I wanted to have the menu item for a page/section highlighted differently when you were on that page or in that section. I.e. the same way 'Questions' is highlighted in orange on this page on StackOverflow. I could define a variable on each page which would be used in the include, but this made the variable sort of global. If you wrap the include in a function, you can define variables with local scope to handle it.
You could also look into a template engine like Smarty. That way you define the the header and footer and all other common elements in a single file, then fill in the rest through smaller templates or direct output.
Use include statements to just include those files to your Page
I think it's
include '[filename]'