| | |||||
| ||||||||
| Webmaster Tutorials Instructional webmaster-related how-to's and tutorials. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Aug 2002
Posts: 1,255
![]() ![]() | Tutorial: Simple site management with the use of PHP and the query string(?page=1)Code: //
//
//Tutorial by deadserious - © http://www.webdesigntalk.net
//© 2003 http://www.webdesigntalk.net
//REPUBLICATION OF THE TUTORIAL REQUIRES OUR PERMISSION.
//webmaster@webdesigntalk.net
//
//
// easier with the use of PHP and the query string. Including Files A simple example of how using includes can make site management easier. For example a file called header.inc contatins: <html> <head> <title>Title of your page</title> </head> <body> <h7>Header Text</h7> And your main page named index.php contains: All of your usual html content here. And a file called footer.inc contains: Coypright yoursite, All Rights Reserved. and what ever else you want in your ????: NamePros.com http://www.namepros.com/webmaster-tutorials/14664-tutorial-simple-site-management-use-php.html footer here. Okay so now we've got three files: header.inc, index.php, and footer.inc Putting it all together. In your index.php you include the header.inc and footer.inc files like this: File index.php PHP Code: up in your browser: <html> <head> <title>Title of your page</title> </head> <body> <h7>Header Text</h7> All of your usual html content here. Coypright yoursite, All Rights Reserved. and what ever else you want in your footer here. </body> </html> Now what makes this nice is if you want to edit your footer or header file all you have to do is make the change to one file and it will be changed throughout your entire site. It's alot easier editing only page, especially when you have hundreds or even thousands of pages. That is just a basic example, but it gives you the basic idea. You can take it alot further and divide your page up into as many sections as you want. There's alot more to including files (security) and more which you'll learn eventually with time and experience, however if you don't mind people having the ability to view the files your including and ????: NamePros.com http://www.namepros.com/showthread.php?t=14664 you're not storing any sensitive information in them this way should be safe. Some ways to get around people having the ability to view your included files is name them something noone would ever guess. Surrounding them with the start <?php and ?> end tags, and alot of others you'll eventually learn. Note:You don't have to use the .inc extension for your included files, you can use .txt, .php, .htm, .html or anything you want. We'll begin with a simple switch statement to get started and show you how to make use of the Query String. File: switch.php PHP Code: http://www.yourdomain.com/switch.php?page=1 You'll get: This is page 1 If you change the end of the url to ?page=2 you'll get: This is page 2 And for any request that doesn't end with ?page=1, ?page=2, or ?page=3 you'll get the default. So if you called up http://www.domain.com/switch.php?page=4 you'll get: Sorry Page 4 doesn't exist Now you can change the $page variable to anything you want. So for example if you changed it to $cmd then you would call up the url's like: http://www.domain.com/switch.php?cmd=1 You can also change each case to what ever you want. So for example you could change the 1st case to something like case 'design': Then you could call up http://www.domain.com/switch.php?page=design Everything that follows the question mark in a url is the query string. The query string works like variable=name and each pair of variable=name is separated by the & sign. The query string can be very useful for creating dynamic web pages. PHP makes it easy to to take advantage of the query string because all the hard stuff has already been done for you. Note: Depending on how PHP is set up on your server you may need to change the first line of the switch statement from: switch ($page) to: switch ($_GET['page']) or: switch ($HTTP_GET_VARS['page']) You'll still be able to change 'page' to what ever you want. We'll use the ($_GET['page']) method in the rest of the examples. Easy enough right? ![]() You can also include files and do various other things based on the query string rather than just echoing output to the browser. In this example we have five files. 1. header.inc with the following content for an example: <title>Fun with PHP</title> </head> <body bgcolor="#ffffff"> 2. main.inc with: <h7 align="center">This is the main page</h7> 3. design.inc with: This is the design page 4. services.inc with This is the services page 5. footer.inc <center>Copyright mysite.com</center> </body> </html> Now we have one more file, we'll call it switch.php with the following content: PHP Code: http://www.yourdomain.com/switch.php in your browser you'll see the default: <html> <head> <title>Fun with PHP</title> </head> <body bgcolor="#ffffff"> <h7 align="center">This is the main page</h7> <center>Copyright mysite.com</center> </body> </html> If you change the url to http://www.yourdomain.com/switch.php?page=home you'll get the same thing as the default. If you change the url to http://www.yourdomain.com/switch.php?page=design you'll get: <html> <head> <title>Fun with PHP</title> </head> <body bgcolor="#ffffff"> This is the design page <center>Copyright mysite.com</center> </body> </html> Now you see how the design.inc file was included in the main part of the page replacing the main.inc page? ![]() The same will work with http://www.yourdomain.com/switch.php?page=services etc. You can also do the exact same thing using if elseif statements. PHP Code: single file. There's alot of different ways to go about it which you'll learn eventually if you stick with PHP, which of course you will. Well that should give you a basic idea of how you can make site management simple with the use of PHP. Feel free to correct any mistakes I may have made, post your comments, suggestions, and ask any questions you may have. Hint: Want to get rid of the page variable /?page=design and have it called as /?design ...... try using ($_SERVER['QUERY_STRING']) |
| |
| | #2 (permalink) |
| New Member Join Date: Nov 2003
Posts: 21
![]() | Im pretty new to php and was looking and thinking of ways to make management easier, thanks for the help. Although i plan on taking it a small step further, I saw a guy use skins on his site. skin grey, turned everything grey and changed the head image. He had 2 more skins, i thought that it was a great idea. Using that switch function will make my life easier. thanks alot
__________________ whats green with wheels? grass, i lied about the wheels, dumbass. |
| |
| | #6 (permalink) |
| NP Maniacâ„¢ Join Date: Apr 2004 Location: Nottingham, UK
Posts: 820
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Brilliant Posts, glad its been made a sticky.
__________________ PHP/MySQL Development offered: PM me for a quote. 47ideas.com - my freelancing portfolio. |
| |
| | #8 (permalink) |
| NamePros Member Join Date: Jun 2004
Posts: 92
![]() | excelent tutorial! extremely informative and easy to follow. thanks your time and effort.
__________________ Bais Menachem |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |