X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 6D453D1BB16 for ; Wed, 7 Apr 2004 11:16:58 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 17294-07 for ; Wed, 7 Apr 2004 11:16:58 -0300 (ADT) Received: from hotmail.com (bay14-f10.bay14.hotmail.com [64.4.49.10]) by svr1.postgresql.org (Postfix) with ESMTP id 895E2D1BA2E for ; Wed, 7 Apr 2004 11:16:53 -0300 (ADT) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Wed, 7 Apr 2004 07:16:57 -0700 Received: from 195.92.67.209 by by14fd.bay14.hotmail.msn.com with HTTP; Wed, 07 Apr 2004 14:16:57 GMT X-Originating-IP: [195.92.67.209] X-Originating-Email: [yazkedoo@hotmail.com] X-Sender: yazkedoo@hotmail.com From: "Yasmine Kedoo" To: pgsql-php@postgresql.org Subject: Re: PHP Username & Password Detection From PSQL Database Date: Wed, 07 Apr 2004 14:16:57 +0000 Mime-Version: 1.0 Content-Type: text/plain; format=flowed Message-ID: X-OriginalArrivalTime: 07 Apr 2004 14:16:57.0816 (UTC) FILETIME=[FC6BD580:01C41CAA] X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/9 X-Sequence-Number: 2666 Hi again. After a recommendation, i have changed my approach and i'm now using a html form to accept the username and password. Please view the following link: http://www.cyber.brad.ac.uk/~yamkedoo/Tests/brandnew.html The username 'yamkedoo' and password 'yasmine' will give Successful Login. This works for all usernames and passwords in the database. Though if a different password is used, Access Denied is printed as well as an error, Warning: Unable to jump to row 0 on PostgreSQL result index 2 in /home/webpages/yamkedoo/Tests/brandnew.php on line 16, that I am unable to solve. This applies for all incorrect passwords. Please view my code: "; } else { #assign formusername from html form to $auth_user #assign formpassword from html form to $auth_pass $auth_user = trim($formusername); $auth_pass = trim($formpassword); $query = "SELECT * FROM PatPerInfo WHERE trim(username) = '$auth_user' AND trim(password) = '$auth_pass'"; $result = pg_exec($database, $query); $row = pg_fetch_object($result, $rw); if($row) { print "Successful Login\n"; } else { print "Access Denied\n"; } } pg_close($database); ?> If anyone can spot any mistakes, i will welcome suggestions ;-) Thanx >From: Andrew McMillan >To: Yasmine Kedoo >CC: pgsql-php@postgresql.org >Subject: Re: [PHP] PHP Username & Password Detection From PSQL Database >Date: Wed, 07 Apr 2004 21:35:22 +1200 > >On Wed, 2004-04-07 at 20:59, Yasmine Kedoo wrote: > > Hi. > > > > I am just beginning to work with PHP & PSQL so forgive me if i make >simple > > mistakes. :-) > > > > I created my PSQL database via telnet on my university's database >server. I > > have no problems retrieving and displaying certain data using PHP, but i >am > > unable to recognise a username and password entered via a predefined > > authentication variable, $PHP_AUTH_USER. > > > > The script must recognise the username: 'yamkedoo', and password: >'yasmine'. > > In the database, the username & password columns are spelt exactly as: > > 'username' & 'password'. The database name is 'yamkedoo', and the table >name > > is 'PatPerInfo', as can be seen from the following code: > >The example in the PHP manual is: > > if (!isset($_SERVER['PHP_AUTH_USER'])) { > header('WWW-Authenticate: Basic realm="My Realm"'); > header('HTTP/1.0 401 Unauthorized'); > echo 'Text to send if user hits Cancel button'; > exit; > } else { > echo "

Hello {$_SERVER['PHP_AUTH_USER']}.

"; > echo "

You entered {$_SERVER['PHP_AUTH_PW']} as your >password.

"; > } >?> > >A couple of notes: > >1) You have starts (like in the example above). Lowercase is also a lot more normal >(although probably uppercase still works). > >2) The example above shows the syntax for more recent PHP versions, with >some security features enabled (i.e. use of $_SERVER['PHP_AUTH_USER'] >rather than $PHP_AUTH_USER) whether the older syntax you have used below >will work will depend on how the installation was configured, to some >extent, as well as the version you are using. > > > > > > if(!isset($PHP_AUTH_USER)) > > { > > Header("WWW-Authenticate: Basic realm=\"Authentication\""); > > Header( "HTTP/1.0 401 Unauthorized"); > > > > echo "No Login\n"; > > exit; > > } > > else > > { > > echo "User: $PHP_AUTH_USER
"; > > echo "Password: $PHP_AUTH_PW
"; > > } > > > $database = pg_connect("host=pgdbs.inf.brad.ac.uk dbname=yamkedoo > > user=yamkedoo password=yamkedoo"); > > > > if(!$database) > > { > > print "Connection to database failed."; > > } > > > > else > > { > > $selectquery = "SELECT * FROM PatPerInfo"; > > $result = pg_exec($database, $selectquery); > > > > $maxrows = pg_numrows($result); > > $maxfields = pg_numfields($result); > > > > for ($rw = 0; $rw < $maxrows; $rw++) > > { > >Just as a suggestion you might want to consider: > >$row = pg_fetch_object($result, $rw); >if ( trim($_SERVER['PHP_AUTH_USER']) == trim($row->username) > trim($_SERVER['PHP_AUTH_PW']) == trim($row->password) ) >{ > ... > >Actually, though, you can get the database to do it: > >$auth_user = pg_escape_string(trim($_SERVER['PHP_AUTH_USER'])); >$auth_pass = pg_escape_string(trim($_SERVER['PHP_AUTH_PW'])); >$selectquery = "SELECT * FROM PatPerInfo > WHERE trim(username) = '$auth_user' > AND trim(password) = '$auth_pass'"; > >$result = pg_exec( ... > > >Doing it this way you can simply see if you got back exactly one row, >and if you did then that should be the correct user record - no need for >PHP to inefficiently loop through all of the table looking. > > > > $username = pg_Result($result,$rw,0); > > $password = pg_Result($result,$rw,1); > > > >Aren't you missing a comparison on the line below? > > > if( trim($PHP_AUTH_USER) == trim($username) && (trim($PHP_AUTH_PW)) > > { > > $auth = 1; > > } > > } > > > > echo $auth; > > } > > > > if($auth==0) > > { > > print "Access Denied
\n"; > > exit; > > } > > > > > > ?> > > > > After the username and password, i get the following error: Parse error: > > parse error in /home/webpages/yamkedoo/Tests/referrals2.php on line 44. > > > > Please view te following link: > > http://www.cyber.brad.ac.uk/~yamkedoo/Tests/referrals2.php to see what >is > > happening. > > Only once has the authentication window appeared, and has not done so >since. > > It only gives the error as seen at the link. > >Once you have provided the correct credentials to basic auth, your web >browser will repeatedly provide them each time until you exit the >browser or cancel them. > >Most sites don't use Basic Authentication like the above - generally >some form of session is maintained through URL rewriting or cookies >since that allows a lot more control (and graphical design) fitting the >login process more smoothly into the web page. > >Regards, > Andrew. > >------------------------------------------------------------------------- >Andrew @ Catalyst .Net .NZ Ltd, PO Box 11-053, Manners St, Wellington >WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St >DDI: +64(4)916-7201 MOB: +64(21)635-694 OFFICE: +64(4)499-2267 > http://survey.net.nz/ - any more questions? >------------------------------------------------------------------------- > > >---------------------------(end of broadcast)--------------------------- >TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) _________________________________________________________________ It's fast, it's easy and it's free. Get MSN Messenger today! http://www.msn.co.uk/messenger