simple authentication code doesn't work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
surban99
Forum Newbie
Posts: 14
Joined: Sat Jan 08, 2005 5:41 pm

simple authentication code doesn't work

Post by surban99 »

I'm trying to use code i found for simple validation, it consists of 3 itty bitty scripts

the first is vars.php

Code: Select all

<?
$dbHost = "127.0.0.1";
$dbUser = "root"; 
$dbPass = "root"; 
$dbName = "portal";
$userTable = "Users"; 
$userField = "Username";
$passField = "Password";
?>
the second is login.php

Code: Select all

<?include"vars.php";?>
<?php
function authenticate() {
Header("WWW-Authenticate: Basic realm=\"secure login\"");
echo "Authentication Failed!";
exit();
}
if(!isset($PHP_AUTH_USER)) {h 
authenticate();
echo "Authorization Failed\n";
exit;
} else {
$checkLogin = "SELECT ID FROM $userTable WHERE $userField='$PHP_AUTH_USER' AND 
$passField='$PHP_AUTH_PW'";
$db = mysql_pconnect($dbHost, $dbUser, $dbPass);
mysql_select_db($dbName, $db);
$result = mysql_query($checkLogin, $db);
$numrows = mysql_num_rows($result);
$myrow = mysql_fetch_array($result);
if ($numrows == 0) {
authenticate(); 
} else {
setcookie("UserID", $myrow["ID"]);
$UserID = $myrow["ID"];
}
}
?>
and the third is index.php

Code: Select all

<?include"login.php";?>
<html>
<center>Hello there Welcome</center>
</html>
the error i get when i try to go to login.php is
Parse error: parse error in c:\program files\easyphp1-8\www\portal\login.php on line 9

and i have no idea at all how to fix it
User avatar
xinnex
Forum Commoner
Posts: 33
Joined: Sun Jan 07, 2007 7:38 pm
Location: Copenhagen, Denmark

Post by xinnex »

well.. for starters.. you seem to have an extra "h" at line 8

Code: Select all

if(!isset($PHP_AUTH_USER)) {h //<--- see?
surban99
Forum Newbie
Posts: 14
Joined: Sat Jan 08, 2005 5:41 pm

Post by surban99 »

lol, well so much for learning code from here

http://www.weberdev.com/get_example-3172.html

but now i cant login

it doesnt like my username and password

any ideas?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It might have something to do with the var $PHP_AUTH_USER and the expectation that register_globals is on, which is probably isn't if you're smart.
Post Reply