First 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/1Now, remember how we personalized a page for your visitor? This works fine, but what do we do if they didn't use that special link, and just went to
page?
What I'm saying is, if you special personalized page was at http://www.your.host/sales.php/f=Oscar/l=Grouch but your visitor only went to http://www.your.host/sales.php. Instead of
name there would just be a blank spot! Last week I forgot to cover this.
All we have to do to fix it is to tell PHP that if they didn't leave a name, to substitute one in for them. So let's say that if they left their first name blank to make their first name "Friend". This way instead of saying "Dear Oscar:" it would say "Dear Friend:".
Put
following line of code JUST ABOVE THE LINE that says something similar to: echo "$f $l" :
if ($f == "") { $f = "Friend"; }
That way, you can use your special personalized page as a normal page and no one will be
wiser.
Password protection is something you need every once in a while. Whether it's a secret site you're running or just
control panel of your favorite script.
Sometimes you don't need a fancy solution like .htaccess if you're only worrying about a single user (you). But JavaScript passwords can be worked around, and HTML-based passwords based on cookies, written in PHP are complicated and take time to write. Htaccess is nice but it's a pain if you just want to use it for one person.
Here is a simple way to use HTTP authentication (the same you see used by htaccess) with just a few lines of code. Below are
sample contents of a file you can use.
$myusername = "myusername"; $mypassword = "mypassword"; $areaname = "My Protected Area";
if ($PHP_AUTH_USER == "" || $PHP_AUTH_PW == "" || $PHP_AUTH_USER != $myusername || $PHP_AUTH_PW != $mypassword) { header("HTTP/1.0 401 Unauthorized"); header("WWW-Authenticate: Basic realm="$areaname""); echo "
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
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
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
script onto your web server and run it in
browser. You should be greeted by a password popup box similar to those you see with htaccess. Enter "myusername" as
username and "mypassword" as
password. You should be given a page that says "my main text" and nothing else.