Information: Adding | Updating | Retrieving | Removing | PHP and MySQL
Chapter 10 Using Include Statements - Page 281-284
- 281 Using Include Statements
- 281 Put all or most of your HTML into include files
- 281 Store the information needed to access the database in a file separate from your program
- 282 Put your functions in include files
- 282 Store statements that all the files on your Web site have in common
- 284 Using functions
- 284 Make frequent use of functions
- 284 Look for opportunities to use functions
- 284 Give descriptive names to your functions
- 281Using Include Statements
- 281 Using Include Statements
- PHP allows you to put statements into an external file
- that is a separate file where the rest of the program or HTML page is stored
- you can insert this external file whenever or where ever you need it
- by using the include statement
- include statements are useful for storing a block of statements that are repeated
- you add the include statement where ever you want the blocks of code instead of the actual block of code
- this makes your main program shorter and easier to read
- the format for the include statement is as follows
- include("filename")
- the file can have any name
- some folks use the .inc file extention
- statements in an include file are considered HTML code, not PHP code
- if you want to include PHP code you will need the php tags <?php ... ?>
- to surround your PHP code.
- 281 Put all or most of your HTML into include files
- If you use forms in your pages, place the code for the forms in an include sttm so you can use that code in multiple places.
- 281 Store the information needed to access the database in a file separate from your program
- store the vaiable names in the file as follows
- <?php
$host="localhost";
$user="phpuser";
$password="secret";
?>
- advantages to doing this is there is now one file to update if you have to change the hostname, username, or password
- include this file near the top of the program that needs to connect to a database
- do not call the include file password.inc, that makes it easier for a hacker to find
- use a misleading filename to make it harder for hackers to find
- 282 Put your functions in include files
- it is suggested that you place functions in a separate include file
- if you have a lot of functions,
- organize related functions into several include files such as data_functions.inc and form_functions.inc
- use include statements near the top of your programs, reading in the functions that are used for that program.
- 282 Store statements that all the files on your Web site have in common
- If all your pages have the same header, sidebar navigation, (the same look and feel)
- place all that HTML code into an external file that you can include.
- If all your pages include the same file, and you need to make a change to that look and feel
- you only have to make that change once to effect the entire web site
- PHP provides a related statement the include_once statement
- if the specified file has already been included in a previous statement, the file is not included again
- the format is as follows
- include_once ("$filename");
- This sttm prevents include files with similar variables from overwriting each other.
- Use include_once when you include your functions.
- You can use a variable name for the filename as follows
- include ("$filename");
- pages 282-283 has an example where there are 7 different files for the days of the week
- you can use the following sttms to display the correct message for the current day
- <?php
$today = date("D");
include("$today"."inc");
?>
- After the first statement, $today contains the day of the week, in abbreviation form.
- The second statement includes the correct file, using the day stored in $today.
- If $today contains Sun, the statement includes a file called Sun.inc
- 283 Protecting your include files is important
- The best way to protect them is to store them in a directory outside your Web space so they cannot be accessed by visitors to your Web site.
- You can set up an include directory where PHP looks for any files specified in an include statement.
- If you are the Administrator, you can set up an include directory in the php.ini file.
- Find the setting for include_path and change it to the path to your preferred directory.
- If a semicolon appears at the beginning of the line, before include_path, remove it.
- The following are examples of the include_path settings in the php.ini file:
- include_path = ".;d:\include"; # for Windows
- include_path = ".:/user/include"; # for Unix/Linux/Mac
- Both sttms specify two directories where PHP looks for include files.
- the first directory is .(dot) meaning the current directory, followed by the second directory path
- You can specify as many include directories as you want, PHP will search them all for your include file.
- The directory paths are separated by semicolons for Windows and a colon for Unix
- If you do not have access to the php.ini file, you can set the path in the individual script by using the following statement
- ini_set ("include_path", "d:\hidden");
- This sttm sets the include_path to the specified directory only while the program is running
- It does not set the directory for the entire Web site
- To access a file from an include directory, just use the filename, you do not need the full path name
- example: include("secreatpasswords.inc");
- 284 if your include file is not in an include directory, you may need to use the entire path name in the include sttm.
- if the file is in the same directory as your program, the file name alone is ok
- if the file is not in the same directory, use the include sttm as follows:
- include("d:/hidden/secreatpasswords.inc");
- 284 Using functions
- 284 Make frequent use of functions
- 284 Look for opportunities to use functions
- Functions from older projects can be used in newer projects
- 284 Give descriptive names to your functions
This site is for the private use of my students. (2008)
Top |
Chapter 7 Using Functions |
PHP and MySQL