PHP: Difference between revisions

216 bytes added ,  13:56, 25 May 2020
m
Line 50: Line 50:
*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.
*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.
*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 '''<output$gt;''' to '''</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.
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.
5,838

edits