Continued from page 1
If you want to take 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 username and password you chose for yourself.
Now change 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?
Take code you just modified and take last line out of it. You know, one that said "my main text." All you should have in there now is everything in between 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 very top of test.php (the VERY top, meaning 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 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 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 very top of file. On very first line. The reason for this is that when 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 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, script read everything that was in auth.php and used it as if 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 following into it.
This is my main page
Upload all three into same folder and run main.php. You should see 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 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?
Article by Robert Plank
Free PHP articles and advice. http://www.jumpx.com/newsletter