Mega Code Archive

 
Categories / Php / Authentication
 

Using Postgres and PHP3 Authentication from a Web application

Login to remove this ad. // // This little PHP script shows you how to connect to a Postgres // database via a browser with User/Password database // authentication using PHP3 authenticate function. // // 1) You have to modify the $PGDATA/pg_hba.conf file: // local all trust // -> trusting all connection for everyone on local machine // host all 127.0.0.1 255.255.255.255 password passwd // -> using password like file (passwd made using pg_passwd tool // to create user password account) to authenticate users // from everywhere // 2) Just use the pg_Connect (or pg_pConnect) function with the // connection string as follows. // <?PHP // // Authentication function - see previous archives for comments // function authenticate() { Header("WWW-authenticate: basic realm=\"Intranet MBDS\""); Header("HTTP/1.0 401 Unauthorized"); $title="Login invalide"; ?> In order to proceed you will need a valid username/password. <? exit; } // // Postgres Connection // function connect($user,$passwd) { // You simply have to change the following variables to match // your site configuration // HOSTNAME = name of your postgres base host // DBNAME = name of your postgres base $conn = pg_Connect("host=HOSTNAME dbname=DBNAME port=5432 user=$user password=$passwd"); // Db connection problem if (!$conn) { echo "Database connection error.\n"; exit; } // SQL order $query_parent="SELECT * FROM xxxx WHERE xxxx"; $result = pg_Exec($conn,$query_parent); if (!$result) { echo "SQL order problem.\n"; exit; } // ... // End of SQL statment pg_FreeResult($result); pg_Close($conn); } // // Main part of your PHP script // if (!isset($PHP_AUTH_USER)) { authenticate(); } else { connect($PHP_AUTH_USER,$PHP_AUTH_PW); } ?>