Types of Authentication
Chances are you have needed to log in to a website in the past, so you should be aware of how the process of authentication works from a
There are actually two ways that a website can authenticate a user, though: using basic HTTP authentication and using session-based authentication. The following sections clarify the differences between these two
Basic HTTP AuthenticationBasic HTTP authentication can be performed by web server, without having anything to do with PHP script. The example in this section assumes that you are using Apache web server; for other web servers, you should refer to your documentation. Authentication is usually done on a per-directory basis but can be set up to apply to individual files if required. By using an .htaccess file on your website, you can specify for that directory a custom configuration that instructs the web server to require a login before proceeding. A typical set of configuration directives would look like this: AuthType Basic AuthName "Protected Website" AuthUserFile /home/yourname/htpasswd require valid-user AuthUserFile points to the location of a password file that is created by using the htpasswd program. To create a new password file, you would run a command like the following:
$
htpasswd c /home/yourname/htpasswd chris
New password:
Re-type new password:
You have to enter the new password twice, after which an entry is added to the password file given. The entry consists of the username and an encrypted version of the password, separated with a
chris:XNiv7qSUTFPU6 damon:ZxxE2PTEXeVNU shelley:SVzAEtxMLEAls vanessa:cX/t1Pv2oQfrY
When you try to access a file in the protected directory, your web browser pops up a window that asks for a username and password, and the page
The require valid-user directive instructs the web server to show the page to any authenticated user. You might want to grant access to only certain users, which you can do with the require user directive: require user chris damon shelley
Basic HTTP authentication also allows you to set up user groups to give access to particular sections of the site only to certain users. You can then use the
require
The following groups file, usually named htgroups , divides the users in the password file into two groups: boys: chris damon girls: shelley vanessa To give access only to the boys group, you could use the following .htaccess file: AuthType Basic AuthName "Boys Only" AuthUserFile /home/yourname/htpasswd AuthGroupFile /home/yourname/htgroup require group boys
Although it is
Session-Based Authentication
To provide a completely customizable login process for your website, you must implement it yourself, and doing so in PHP requires using session
In a
One fairly significant difference from basic HTTP authentication is that the instruction to check the validity of a user's session appears in the script itself, not in a per-directory configuration file.
|