|
|||
| Top > Cgi > password protection and file inclusion with php by robert plank |
Looking for Credit Cards? Choose from over 160 different offers.
Credit Card Offers and online credit card applications..
|
Tuesday, October 7, 2008
Password Protection And File Inclusion With PHP By Robert PlankFirst off, if you read last week`s article by me (the one about site personalization in PHP), I have one addition to make to make your life a little easier. If you didn`t read last week`s article, read it. It`ll help you. You can find it here: http://jumpx.com utorials/1 Authorization Required.`;die(); } ?> my main text. Last week we learned that PHP code can be integrated into your HTML. All you have to do is make sure the file ends in .php (for example, `firehydrant.php`) and it will work. Everything that comes in between this: /* And this: */ ?> Is treated as PHP code. Everything outside of those tags is treated as plain HTML. When copying this code over be SURE to include that last line where it says `my main text.` Note that `my main text` is located outside of the PHP code brackets. This means that where you see `my main text` can be your normal HTML file! Take all of this code and Upload the script onto your web server and run it in the browser. You should be greeted by a password popup box similar to those you see with htaccess. Enter `myusername` as the username and `mypassword` as the password. You should be given a page that says `my main text` and nothing else. Close your browser window (this is very important) and going back to that page. Try entering the wrong info. The box will come up again. You have three tries and then are given that dreadful `Authorization Required` message. If you want to take the next step, go back to your code and change `myusername` and `mypassword` to a username and password of your choice. Upload it back to your web server and try again. Now go to that page again and you`ll see that you can only be let in using the username and password you chose for yourself. Now change the part that says `My Protected Area` to something else, say `John Calder`s Bar and Grill.` Upload and try it. You`ll see when that password box comes up under `Realm` it`ll say `John Calder`s Bar and Grill.` You can change this to whatever you like. But what if you want to password protect just a handful of files? Do you have to copy and paste this code onto PHP script after PHP script? Hell no! Take the code you just modified and take the last line out of it. You know, the one that said `my main text.` All you should have in there now is everything in between the PHP brackets (). Save this file as `auth.php`. You can rename this later, on your own time. Make a new file called `test.php` or just rename one of your normal HTML to this name. It doesn`t matter. At the very top of test.php (the VERY top, meaning the first line) copy and paste this line of code: Upload auth.php and test.php to your web server and run test.php. Make sure both files are placed in the same folder. Now, try to go to test.php in your web browser. You`ll see that you can`t get to test.php without the right username and password. You can do this to any file with a `.php` extension just by adding that one line of code. The catch to it is that this line of code has to be at the very top of the file. On the very first line. The reason for this is that when the script asks for a person`s username and password, these are sent using HTTP headers and *must* come before anything else. Of course, this doesn`t take care of your secret sites or private members` areas, where you have to deal with several logins, but that`s what htaccess is for. While we`re on the subject of includes, one last thing before we finish up. Includes are basically a way of absorbing other files into your script. As you saw when we included auth.php, the script read everything that was in auth.php and used it as if the contents of that file were actually there. This works with not only PHP scripts but also with other files as well. Make a new file called `header.html`. Put anything you want in it, but I just put `This is my header ` when I did it. Make a second file called `footer.html`. Again, go again and put anything you want in it, but I just put `This is my footer ` in. Make a third file called `main.php.` Copy the following into it. This is my main page Upload all three into the same folder and run main.php. You should see the following: This is my header This is my main page This is my footer This is just a basic example of how includes can be used. But if you have a web site with several pages and the same layout... wouldn`t it be easier just to put everything above your main text in header.html and everything below that main text in footer.html? That way if you change your design you only have to edit 2 files instead of 100 or 200? You`d think.
Article by Robert Plank |
|||||||||||||||||||||||
Script executed in
0.3
seconds. |
||||||||||||||||||||||||
|
|
All content Copyright © 2007 ArticlesOnline.org [ Credit Card Offers ] Acupuncture Baldness - The Vertical Project - Furniture - Property In New Zealand - U.s. Virgin Islands |