PHP: Difference between revisions

From Cumulus Wiki
Jump to navigationJump to search
5,785 bytes added ,  13:56, 25 May 2020
m
m (Text replacement - "http://sandaysoft.com/forum/" to "https://cumulus.hosiene.co.uk/")
 
(9 intermediate revisions by the same user not shown)
Line 33: Line 33:


Many web servers support the PHP scripting language, this means that the web server will parse the contents and obey all the script instructions to generate a pure HTML page that it forwards to a user agent (e.g. browser).
Many web servers support the PHP scripting language, this means that the web server will parse the contents and obey all the script instructions to generate a pure HTML page that it forwards to a user agent (e.g. browser).
=== Advantages of PHP scripts ===
PHP is relatively easy to learn, because it can be used either in a very simple way, or when you learn more in a very sophisticated way. 
One advantage of PHP scripts is because they are processed by the web server nobody can steal your script unless you use some functionality like that [https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425&p=126065 described here].
If you use PHP for a set of web pages, you can make your web suite very much easier to maintain by not putting every instruction into one script and so avoiding repeating yourself in many places.
PHP allows you to use syntax like 'require' or 'include' to effectively bring in common code from other files. Put simply, when the PHP parser finds "require_once 'file_name';", "require 'file_name';", or the alternative include syntax, the parser reads the file referenced and treats it as if it is part of the original file from then onwards. "include" does not cause an error if it cannot find the file requested. "require" creates an error and aborts the main script if it cannot find the named file. The "_once" is used if the file you want to bring in includes setting variables that you later want to give a new value to, because your new value would be lost if the include happened again. Also a function can only be declared once so if script you select to bring in contains one or more function definitions, either that script must check if function has already been declared before it declares it, or that script must be called with the "_once" variant. However, because using "_once" creates a considerable overhead, the parser must read all files currently loaded to see if the named script  is referenced anywhere else, it is bad coding to use "require_once"/"include_once" variant if the aforementioned reasons don't make it necessary, instead just use plain "require" or "include".
There is almost no restriction on what you bring in:
*If you always include some HTML that is same on every web page, bring that in by specifying a HTML file, the normal file extension is '''.html'''.
**Remember,  a standard header (and footer) can make all your web pages look similar and more professional, doing it this way means there is only one place where you need to edit to change that look. But remember it must be exactly same for every web page as any file with a HTML file extension is normally ignored by the PHP parser, so only client scripts like JavaScript can change the content of a HTML file.
*If you have a standard header (or footer) to repeat on every web page, but want to be able to send it some PHP variables to modify some of the content (what CSS to use, you have a common navigation menu but need to identify which page is the current one, you want to declare when the current web page had its structure or content last updated in a standard way on every web page), all of these and more can be achieved by using a php script. Remember to use the extension '''.php''' in the require/include and also to use a '''<?php''' to identify the start of the PHP in the file you are including. Don't use a '''?>''' in the require/include file, unless there is HTML directly after that in that file, because you have pHP next back in calling script.
**Remember, a standard header (and footer) can make all your web pages look similar and there is only one place where you need to edit to change that look. But equally using a script a web page can look different depending on any data you choose (time of day, current season, whether it is raining, how hot it is) or it can display different content depending on particular values.
*Some other scripts might contain standard functions (like connecting to a database, reading from a database,  calculating highest or lowest, or they might contain some of your intellectual property in terms of a script that you want to place in a secure location only known when parsing within the web server, so cannot be accessed from the browser and so cannot be hacked.
*My examples quote extensions using .html or .php, but some people suggest all include scripts are stored in files with an extension '''.inc''' to make it clear that file is not intended to be used on its own, and other advice is to create a separate directory just for files used by require and include.
*Some people use this to bring in the equivalent of what the HTML "iframe" syntax does, or to include a data file that you want to show on the web page within HTML '''&lt;output&gt;''' to '''&lt;/output&gt;''' tags.
A common mistake with includes is to forget to specify the path correctly. If all files are in same directory use '''.\file_name.extension''' syntax, this is called relative paths and means the requested file is in same directory as calling file. Leaving out the prefix, and the PHP parser will load the first file it finds with that name following the defined'''include_path''' rules. A standard one might start with "./" as first path, but you cannot assume that is the first path if you are writing a script others might use. Equally if all the files to be included are in a common directory at same level as the directory where the calling files are use '''../common/file_name.extension''' syntax.  If you are writing a script that might be used with different web pages, or even in batch, so you cannot be sure of the relative path between calling script and file to be included, declare the absolute path, if necessary using whatever constant represents the root (it might be $_SERVER[DOCUMENT_ROOT]) in the path.


=== Learning More about PHP ===
=== Learning More about PHP ===
Line 46: Line 66:


One possible advantage of this type of script is that by default the script cannot be seen (unless the developer has added some code ([https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425 see this forum topic]) allowing a query-string to be added to the url asking for some or all of the PHP source to be shown to the browser). This makes for a more secure script, because it only runs on the web site server.  For instance if information is read from a database or in another way involves a password of some kind, all that code can be hidden, the end-user then has no way to modify the script to get information that should not be seen by others. A disadvantage is that it can be more complicated to update information on a page, requiring the whole page to be regenerated. (Consequently, a mix of scripts may be used with updating by AJAX or JSON, added to the PHP that generates the basic page. That is beyond the scope of this article).
One possible advantage of this type of script is that by default the script cannot be seen (unless the developer has added some code ([https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425 see this forum topic]) allowing a query-string to be added to the url asking for some or all of the PHP source to be shown to the browser). This makes for a more secure script, because it only runs on the web site server.  For instance if information is read from a database or in another way involves a password of some kind, all that code can be hidden, the end-user then has no way to modify the script to get information that should not be seen by others. A disadvantage is that it can be more complicated to update information on a page, requiring the whole page to be regenerated. (Consequently, a mix of scripts may be used with updating by AJAX or JSON, added to the PHP that generates the basic page. That is beyond the scope of this article).


=== Using PHP in Cumulus templates ===
=== Using PHP in Cumulus templates ===
Line 176: Line 194:
#[https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=8631&p=71770 compare yesterday with one year ago page]
#[https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=8631&p=71770 compare yesterday with one year ago page]
#[https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=10755 Top Ten Records page]
#[https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=10755 Top Ten Records page]
= The next article to read …… =
Learn how to use a suite of PHP scripts to populate PHP variables with values taken from web tags. See [[Php webtags|PHP Hypertext Preprocessor scripts reading web tags]].
5,838

edits

Navigation menu