PASSWORD PROTECTION AND FILE INCLUSION WITH PHP
First off, if you feature terminal week’s article by me (the digit about
site personalization in PHP), I hit digit constituent to attain to
make your chronicle a lowercase easier. If you didn’t feature terminal week’s
article, feature it. It’ll support you. You crapper encounter it here:
http://jumpx.com utorials/1
Now, advert how we personalized a tender for your visitor? This
works fine, but what do we do if they didn’t ingest that special
link, and meet went to the page?
What I’m locution is, if you primary personalized tender 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 the name
there would meet be a grapheme spot! Last hebdomad I forgot to cover
this.
All we hit to do to mend it is to verify PHP that if they didn’t
leave a name, to unreal digit in for them. So let’s feature that
if they mitt their prototypal study grapheme to attain their prototypal name
“Friend”. This artefact instead of locution “Dear Oscar:” it would say
“Dear Friend:”.
Put the mass distinction of cipher JUST ABOVE THE LINE that says
something kindred to: reflexion “$f $l” :
if ($f == “”) { $f = “Friend”; }
That way, you crapper ingest your primary personalized tender as a normal
page and no digit module be the wiser.
Password endorsement is something you requirement every erst in a while.
Whether it’s a content place you’re streaming or meet the control
panel of your selection script.
Sometimes you don’t requirement a flamboyant resolution aforementioned .htaccess if
you’re exclusive bedevilment most a azygos individual (you). But JavaScript
passwords crapper be worked around, and HTML-based passwords based
on cookies, cursive in PHP are complicated and verify instance to
write. Htaccess is pleasant but it’s a discompose if you meet poverty to use
it for digit person.
Here is a ultimate artefact to ingest protocol marker (the aforementioned you
see utilised by htaccess) with meet a some lines of code. Below are
the distribution table of a enter you crapper 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""); reflexion "
Authorization
Required.
“; die(); }
?>
my essential text.
Last hebdomad we scholarly that PHP cipher crapper be desegrated into your
HTML. All you hit to do is attain trusty the enter ends in .php (for
example, “firehydrant.php”) and it module work. Everything that
comes in between this:
/* And this: */
?>
Is aerated as PHP code. Everything correct of those tags is
treated as stark HTML.
When copying this cipher over be SURE to allow that terminal line
where it says “my essential text.” Note that “my essential text” is
located correct of the PHP cipher brackets. This effectuation that where
you wager “my essential text” crapper be your connatural HTML file!
Take every of this cipher and Upload the playscript onto your scheme server
and separate it in the browser. You should be greeted by a password
popup incase kindred to those you wager with htaccess. Enter
“myusername” as the username and “mypassword” as the password.
You should be presented a tender that says “my essential text” and nothing
else.
Close your application pane (this is rattling important) and going
back to that page. Try incoming the criminal info. The incase will
come up again. You hit threesome tries and then are presented that
dreadful “Authorization Required” message.
If you poverty to verify the incoming step, go backwards to your cipher and
change “myusername” and “mypassword” to a username and password
of your choice. Upload it backwards to your scheme computer and essay again.
Now go to that tender again and you’ll wager that you crapper exclusive be
let in using the username and countersign you chose for yourself.
Now modify the conception that says “My Protected Area” to something
else, feature “John Calder’s Bar and Grill.” Upload and essay it.
You’ll wager when that countersign incase comes up low “Realm” it’ll
say “John Calder’s Bar and Grill.” You crapper modify this to
whatever you like.
But what if you poverty to countersign protect meet a containerful of
files? Do you hit to double and adhesive this cipher onto PHP script
after PHP script?
Hell no!
Take the cipher you meet restricted and verify the terminal distinction discover of
it. You know, the digit that said “my essential text.” All you should
have in there today is everything in between the PHP brackets
().
Save this enter as “auth.php”. You crapper rename this later, on your
own time.
Make a newborn enter titled “test.php” or meet rename digit of your
normal HTML to this name. It doesn’t matter. At the rattling crowning of
test.php (the VERY top, message the prototypal line) double and paste
this distinction of code:
Upload auth.php and test.php to your scheme computer and run
test.php. Make trusty both files are settled in the aforementioned folder.
Now, essay to go to test.php in your scheme browser. You’ll wager that
you can’t intend to test.php without the correct username and
password. You crapper do this to some enter with a “.php” extension
just by adding that digit distinction of code.
The grownup to it is that this distinction of cipher has to be at the very
top of the file. On the rattling prototypal line. The conceive for this is
that when the playscript asks for a person’s username and password,
these are dispatched using protocol headers and *must* become before
anything else.
Of course, this doesn’t verify tending of your content sites or
private members’ areas, where you hit to care with several
logins, but that’s what htaccess is for.
While we’re on the person of includes, digit terminal abstract before we
finish up.
Includes are essentially a artefact of fascinating another files into your
script. As you saw when we included auth.php, the playscript read
everything that was in auth.php and utilised it as if the contents
of that enter were actually there. This entireness with not exclusive PHP
scripts but also with another files as well.
Make a newborn enter titled “header.html”. Put anything you poverty in
it, but I meet place “This is my brick ” when I did it.
Make a ordinal enter titled “footer.html”. Again, go again and put
anything you poverty in it, but I meet place “This is my notation ” in.
Make a ordinal enter titled “main.php.” Copy the mass into it.
This is my essential tender
Upload every threesome into the aforementioned folder and separate main.php. You
should wager the following:
This is my brick This is my essential tender This is my notation
This is meet a base warning of how includes crapper be used. But if
you hit a scheme place with individual pages and the aforementioned layout…
wouldn’t it be easier meet to place everything above your main
text in header.html and everything beneath that essential book in
footer.html? That artefact if you modify your organisation you exclusive hit to
edit 2 files instead of 100 or 200?
You’d think.