Hey…
I think every PHP developer knows the PEAR Package, Its give excellent, flexibility and secured solutions.
One of the is PEAR::Auth.
The PEAR::Auth package provides methods for creating an authenticationsystem using PHP code.
More info about PEAR::Auth you can find here…
Fist of all create mysql db & table:
The Let load the PEAR::Auth
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| require_once "Auth.php";
// Setup the DNS and Fields name in table 'auth'
$options = array(
'dsn' => "mysql://root@localhost/dbname",
'table' => "auth" ,
'usernamecol' => "email" ,
'passwordcol' => "password",
'db_fields' => array('id','email')
);
// Create instance of PEAR::Auth with the option and the login form function
$a = new Auth ("MDB", $options, "loginFunction");
// The function that show the login form
function loginFunction ($username = null, $status = null, & ;$auth)
{
/*
* Change the HTML output so that it fits to your
* application.
*/
echo "<form action=\"".$_SERVER['PHP_SELF']."?login=1\" method=\"post\">";
echo "<input name=\"email\" type=\"text\" />";
echo "<input name=\"password\" type=\"password\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}
// Start the PEAR::Auth Instance
$a->start(); |
Now when we want to check if the user is connected to the system we do:
1 2 3 4 5
| // if "True" than user is Logedin
if ($a->checkAuth()) {
// Getting the fields from the user `auth` table
$_SESSION['userid']=$a->getAuthData('id');
} |
For Logout use:
Add users:
1
| $a->addUser("username","password"); |

PEAR
Have Fun,
Garry Lachman
Share on Facebook
Using Pear Auth Package for user loging – PHP
Hey…
I think every PHP developer knows the PEAR Package, Its give excellent, flexibility and secured solutions.
One of the is PEAR::Auth.
The PEAR::Auth package provides methods for creating an authenticationsystem using PHP code.
More info about PEAR::Auth you can find here…
Fist of all create mysql db & table:
2
3
4
5
6
7
`id` mediumint(4) NOT NULL auto_increment,
`email` char(100) NOT NULL default '',
`password` char(32) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `email` (`email`)
) TYPE=MyISAM
The Let load the PEAR::Auth
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Setup the DNS and Fields name in table 'auth'
$options = array(
'dsn' => "mysql://root@localhost/dbname",
'table' => "auth" ,
'usernamecol' => "email" ,
'passwordcol' => "password",
'db_fields' => array('id','email')
);
// Create instance of PEAR::Auth with the option and the login form function
$a = new Auth("MDB", $options, "loginFunction");
// The function that show the login form
function loginFunction($username = null, $status = null, &$auth)
{
/*
* Change the HTML output so that it fits to your
* application.
*/
echo "<form action=\"".$_SERVER['PHP_SELF']."?login=1\" method=\"post\">";
echo "<input name=\"email\" type=\"text\" />";
echo "<input name=\"password\" type=\"password\" />";
echo "<input type=\"submit\" />";
echo "</form>";
}
// Start the PEAR::Auth Instance
$a->start();
Now when we want to check if the user is connected to the system we do:
2
3
4
5
if ($a->checkAuth()) {
// Getting the fields from the user `auth` table
$_SESSION['userid']=$a->getAuthData('id');
}
For Logout use:
Add users:
PEAR
Have Fun,
Garry Lachman