Password Protection and File Inclusion With PHP

Written by Robert Plank


Continued from page 1

If you want to takerepparttar 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 usingrepparttar 105085 username and password you chose for yourself.

Now changerepparttar 105086 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!

Takerepparttar 105087 code you just modified and takerepparttar 105088 last line out of it. You know,repparttar 105089 one that said "my main text." All you should have in there now is everything in betweenrepparttar 105090 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. Atrepparttar 105091 very top of test.php (the VERY top, meaningrepparttar 105092 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 inrepparttar 105093 same folder. Now, try to go to test.php in your web browser. You'll see that you can't get to test.php withoutrepparttar 105094 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 atrepparttar 105095 very top ofrepparttar 105096 file. Onrepparttar 105097 very first line. The reason for this is that whenrepparttar 105098 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 onrepparttar 105099 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,repparttar 105100 script read everything that was in auth.php and used it as ifrepparttar 105101 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." Copyrepparttar 105102 following into it.

This is my main page

Upload all three intorepparttar 105103 same folder and run main.php. You should seerepparttar 105104 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 andrepparttar 105105 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

Free PHP articles and advice. http://www.jumpx.com/newsletter


Track your visitors, using PHP

Written by Dennis Pallett


Continued from page 1

Let's use PHP to generate useful overviews for is. The first thing that needs to be done is getrepparttar contents fromrepparttar 105083 log file in a variable, like so:

 // Open log file $logfile = "G:projectsphpitcontentrack your visitors using phplog.txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r"); $log = fread($handle, filesize($logfile)); fclose($handle); } else { die ("The log file doesn't exist!"); }

Now thatrepparttar 105084 log file is in a variable, it's best if each logline is in a separate variable. We can do this usingrepparttar 105085 explode() function (http://www.php.net/explode), like so:
 // Seperate each logline $log = explode(" ", trim($log));  
After that it may be useful to get each part of each logline in a separate variable. This can be done by looping through each logline, and using explode again:
 // Seperate each part in each logline for ($i = 0; $i < count($log); $i++) { $log[$i] = trim($log[$i]); $log[$i] = explode('|', $log[$i]); } 

Nowrepparttar 105086 complete log file has been parsed, and we're ready to start generating some interesting stuff.

The first thing that is very easy to do is gettingrepparttar 105087 number of pageviews. Simply use count() (http://www.phpit.net/count) onrepparttar 105088 $log array, and there you have it;

 echo count($log) . " people have visited this website."; 
You can also generate a complete overview of your log file, using a simple foreach loop and tables. For example:
 // Show a table ofrepparttar 105089 logfile echo '<table>'; echo '<th>IP Address</th>'; echo '<th>Referrer</th>'; echo '<th>Date</th>'; echo '<th>Useragent</th>'; echo '<th>Remote Host</th>';

foreach ($log as $logline) { echo '<tr>';

echo '<td>' . $logline['0'] . '</td>'; echo '<td>' . urldecode($logline['1']) . '</td>'; echo '<td>' . date('d/m/Y', $logline['2']) . '</td>'; echo '<td>' . $logline['3'] . '</td>'; echo '<td>' . $logline['4'] . '</td>';

echo '</tr>';

}

echo '</table>';

You can also use custom functions to filter out search engines and crawlers. Or create graphs using PHP/SWF Charts (http://www.maani.us/charts/index.php). The possibilities are endless, and you can do all kinds of things!

In Conclusion... In this article I have shown you have to create a logging module for your own PHP website, using nothing more than PHP and its built-in functions. To viewrepparttar 105090 log file you need to parse it using PHP, and then display it in whatever way you like. It is up to you to create a kick-ass traffic analyzer.

If you still prefer to use a pre-built traffic analyzer, have a look at http://www.hotscripts.com.

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites: http://www.phpit.net, http://www.aspit.net and http://www.ezfaqs.com


    <Back to Page 1
 
ImproveHomeLife.com © 2005
Terms of Use