Include a file to all php files recursively - php

I have this file that I want to run first before each PHP file. How can I achieve that? Now, I only use a classic php way.
run_me_first.php
and I want to tinclude it in my all of my PHP file. Besides putting it manually, is there a way not doing it manually?

Please try it with an .htaccess file:
php_value auto_prepend_file "/path/to/file/run_me_first.php"
If it's a free FTP, they might have some restrictions on what settings you can alter.

This could be achieved by using a require or include function to include the file in each of your php files. You haven't provided a great deal of information, so any response I can offer will be broad and may not pay to your particular needs.

I think You have an header file. If You dont't have then You create an header.php . Then include all Your files (.php , .js) which You want to include in all Your php files. Then You need to only include the header.php in the above of all Your php file.
Then this header.php will automatically include all Your required files.
This will reduce time and also will reduce the probability of error. This is the process which I used to follow in all of my project.

Related

Including a PHP file of another server

I am trying to communicate between two servers through PHP. Lets say, there is one PHP file "a.php" on my localhost and another PHP file "b.php" on a remote server. I want to include b.php in a.php. I am trying to do this through include method by giving a full path of remore server "http://ip/b.php" but nothing happens.
Actually I want to run a part of script from a.php file then I want to communicate with b.php file and then return back to a.php file.
Please guide me how to do this. I know there are similar questions asked and I have tried to resolve this issue using those techniques but in vain.
Thank you
Nope, this setting is disabled/not allowed by default in most web servers (php.ini) so you can not use the include to include the files from a remote addresss for security reasons.
If you still want to allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini
But again it is a bad practice, in a security-oriented point of view ; and, so, it is generally disabled (I've never seen it enabled, actually)
If you want to read the contents of a remote file though, you can use the file_get_contents function instead BUT this will be returned as pure HTML markup code, there won't be any server-side code.
including php file from another server with php
Another Solution is
Save your file as a text file removing the from it.
Access the file using file_get_contents
Example
http://ip/b.php - save this file as b.txt
<?php
$filedata = file_get_contents('http://ip/b.txt');
eval ("?>$filedata");
?>
As much as it is unsafe and bad practice, you can always turn off php for particular directory, using .htaccess (php_flag engine off).
Then your files will be served directly as a text files to whoever know url. That way you can include them via allow_url_include or as Mad Angle said get_file_contets.
But either way - its bad idea. So you can try it for science :)

Use of php within an html file and the position of the html file within a directory

I am new to php, so please assume I know nothing!
I have a website in which I want to use "require" to add a header and footer to each page. I'm assuming this is the best way to allow me the flexibility to change the header file and automatically have all pages that include it update.
The index, header and many other files are in the root directory. Using:
<?php require("header.php"); ?>
work fine for all pages located in the root directory.
But I am having trouble using the same process for an html file located within (e.g.) root/sub-directory/example.html. Using the above code does nothing. The header is not included, which I first assumed was to do with incorrect reference to the header file from the sub-directory. I have tried reading about different methods of referencing to no avail, but I now wonder if that might not be the problem I'm having here: I have noticed that I do not even get an error associated with not finding the header file (the reason why I used require initially). It appears as though the above code within the html file in the sub-directory is completely ignored by the browser. (It is worth noting that all of the other text is read correctly and references out of the sub-dir seem to be working).
Does anyone know why this might be?
PHP code will only be processed if the file format is .php, so change that and it should work.

.php with HTML versus .html with PHP

When I am starting to build a site that is going to require both HTML and PHP, should I be making a .html file with PHP in it (as in the file would be, say, index.html but within it there would be various tags)? Or, should I be making the files .php files and simply include HTML within it (as in the file would be, again say, index.php and it would start as PHP and I would simply intertwine HTML)?
TL;DR: Should I be weaving HTML into .php files or weaving PHP into .html files?
It should be a PHP file with HTML "weaved" into it. By default if your server sees an HTML file it does not think it needs to process scripts on the page and will render it. If it sees a PHP extension, it knows it needs to run through the PHP Processor.
You can modify your htaccess to allow HTML to be rendered through the processor, but there really is no need for you to be modding that, especially if you are a beginner.
You use PHP files with HTML in it
You should "weave" html into php files That way you know for sure your code will work on any server, and not just on servers that renders html files as php.
You need to specify in your .htaccess file to be able to parse PHP inside of a .html file. The easier way to go is just to make everything .php.
Inevitably, when you get more comfortable with PHP, you'll learn that you'll always have a little PHP in the file (like a require or something), so best to plan for that.
If you are new to PHP, I would recommend creating files with the .php extension, as the .php file can be executed by default. Depending on your server configuration, you may have to add some .htaccess directives to allow php code to run in an .html file.
If you like .html extensions, you can use .phtml files for templating your system, but only for the files that containing html code. And I prefer to use .php files that containing only php code like classes etc (this is what Zend or similar libs do).

Include external code in php

I need to include to my php script external php code which is situated for example by link http://site.com/code.php How could I do it? I tryed all ways which I found in internet but no one works. All methods are good to include text but not php script.
You can only include the code if it is served as text: otherwise everyone would be able to see / use your code.
So the options you have:
Get the file trough ftp and include it with include or require
Get the file in plaintext, by serving .php files on "site.com" as text. This is ofcourse not a good idea, as everyone could see your source from there.
Put the file on the same server as the script that wants to include it.
If you need just the file to be 'run', you can curl it. You won't get the source (cannot use its functions etc) but any actions it performs (make file? add something to the database) will be run.
It is not possible, unless you can get the source code to it (aka. its published somewhere or it is on a file system you can access).
According to the PHP documentation (http://php.net/manual/en/features.remote-files.php) "As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the include(), include_once(), require() and require_once() statements (since PHP 5.2.0, allow_url_include must be enabled for these)."

Is parsing an ini file in PHP better than having variables defined in code?

I was reading about PDO and I came across the parse_ini_file function. A number of developers suggested using this function to parse in db settings rather than hard coding the db settings in code for security reasons.
My question to you is, does it make sense to do a file read for every load of your PHP application for this extra "security" ?
I wonder how expensive this file read is..
php 5.3
in the comments
http://www.php.net/manual/en/class.pdo.php
I don't really see how it's any more secure.
For example, if your DB settings are stored in defines within a "config.php" file outside of the main web root, they're just as secure as if they're were stored in a .ini file and there would be no per-page parsing overhead (other than having to include the config file as per normal).
Hard coding settings in PHP files is bad because those same PHP files will be sent around, copied, put into repositories, etc. The passwords should be treated with more privacy. Also, it's annoying to have to the source files overwrite your local copies.
Note that I'm referring specifically to embedding in regular PHP files in your project's codebase. If you place your config settings in a PHP file that sits external to all of that, then none of the above applies.
If you are worried about the overhead of parsing one config file, then you shouldn't be using PHP at all... However, you could limit file reads by parsing it only when a cached (e.g., memcache) copy cannot be found.
It makes sense if you have more than just db access stored in the ini file . It can act like a config for you're app so you don't have to open 10 files to change 3 hardcoded variables/constants/whatever . If you don't like reading a file each time you're app is requested then use a php file to store all you're config options ( keep them all in one place is realy good ) , and as sugested keep the ini/php config file out of you're web root .
Probably not. If its a .ini file then a browser can just visit it and download it. At least a .php has a decency of a blank screen.

Categories