In PHP, adding, setting up, or managing user management can be quite tedious and can quickly overwhelm a webmaster. Having a nice class that can manage your authentication, user setup, and permissions will really speed up the process. This is not easy, but this class will take a lot of the workload out it for you.
This class utilizes Apache .htaccess and .htgroup files to define users, groups and passwords. Additionally, you can use the class to manage extra information for users, stored in a third file.
Test (this will ensure the user management class is working properly:
-
<?php
-
/*****************************************************************************
-
| @script_type – PHP Example-Script
-
| ————————————————————————-
-
| http://thedevnet.com/php/classes/php-user-management-script-using-htaccess/
-
| $Id: setup_test.php,v 1.1 2009/10/18 19:58:58 horst Exp $
-
****************************************************************************/
-
-
error_reporting(E_ALL ^E_NOTICE);
-
-
# embedd classfile
-
-
require_once(dirname(__FILE__).'/../_libs/example_class.inc.php');
-
-
-
# to define HTPASSWDPATH and HTUSERFILE is mandatory,
-
# to define (and use) HTGROUPFILE or HTINFOFILE both is optional
-
-
define('HTPASSWDPATH', dirname(__FILE__).'/../_htfiles/');
-
define('HTUSERFILE', '.htuser');
-
define('HTINFOFILE', '.htinfo'); // if you use infos and also want the user let it edit too, enabled it, otherwise it should be disabled
-
define('HTGROUPFILE', '.htgroup'); // isn't needed for the ChangePassword-Form, and can be disabled for performance reason (reading and parsing one file more or less, _every_time_!)
-
-
-
# initialize the class
-
# param 1: URL of this script, needed for Redirections and Formactions
-
# param 2: (optional) array with admin-userid/s
-
# param 3: (optional) name of the admin group
-
-
$ht = new hn_htusers($_SERVER['PHP_SELF'], array('XyZ'), 'admins');
-
$ht->setup_test();
-
-
?>
Example PHP HTTP Authentication:
-
<?php
-
if(!isset($_SERVER['PHP_AUTH_USER']))
-
{
-
header('WWW-Authenticate: Basic realm="hn_htusers"');
-
header('HTTP/1.0 401 Unauthorized');
-
echo '401 Unauthorized! Accessing this page needs a valid useraccount, sorry!';
-
exit();
-
}
-
?>
An example form for Administrators:
-
<?php
-
/*******************************************************************************\
-
* @script_type – PHP-Script
-
* —————————————————————————
-
* $Source: http://thedevnet.com/php/classes/php-user-management-script-using-htaccess/
-
* $Id: example.woh4.php,v 1.1 2009/10/16 17:05:42 horst Exp $
-
\*******************************************************************************/
-
-
include('htaccess.inc.php');
-
$hn_let_only_admins_pass = TRUE;
-
-
// sets the used htfiles to users, groups and infos
-
$htfiles = 3;
-
include('../example_manager.inc.php');
-
-
?>
With this PHP User management class your authentication, and permissions is streamlined.
| Author: |
Horst Nogajski |
| Platforms: |
Web Based |
| Category: |
classes |
| License: |
GNU General Public License |
| Date: |
November 9, 2009 |
GD Star Rating
loading...
Originally posted 2009-11-09 21:26:27.
Popularity: 40%
Posted by natster @ 12 February 2010