Abstract
PEAR offers now a session based authentication using a database container. The sample below should help you to start up with it.
Script
01 <?php 02 /** 03 * Authentication using PEAR Auth and MySQL 04 * 05 * ———————————————————————– 06 * # Add this table to a database called \’auth\’: 15 * INSERT INTO auth VALUES (\’test\’, \’098f6bcd4621d373cade4e832627b4f6\’); 23 * $Id$ 27 require_once(\”DB.php\”); 28 require_once(\”DB/mysql.php\”); 29 require_once( \”Auth/Auth.php\”);30 31 // Initialization of database related information 32 $params = array( 33 \”dsn\” => \”mysql://root:@localhost/auth\”, 34 \”table\” => \”auth\”,35 \”usernamecol\” => \”username\”, 36 \”passwordcol\” => \”password\” 37 );38 39 // A database is used as storage container in this example 40 $a = &new Auth(\”DB\”, $params ); 41 42 // Detection, if user is logged in. Otherwise the login form43 // is being displayed. 44 $a->start(); 45 46 if ($a->getAuth()) 47 { 49 { 50 // Log user out 51 $a->logout(); 52 echo \”You have been logged out!<br />\\n\”;53 echo \”<a href=\\\”$_SERVER[\’PHP_SELF\’]?act=login\\\”>Login</a><br />\\n\”; 54 } 58 // for users which have been authenticated successfully. 59 printf(\”Welcome user <b>%s</b>!<br />\\n\”, $a->getUsername() ); 60 echo \”<a href= \\\”$_SERVER[\’PHP_SELF\’]?act=logout\\\”>Logout</a><br />\\n\”;61 } 66 echo \”<center>You are not authenticated.<br />\\n\”; 67 echo \”[Try: demo,demo or test,test]</center>\\n\”;68 } 69 ?> |