PDA

View Full Version : Validating a username and password from external pages


sknight
01-24-2011, 10:00 PM
Seems like rocket science to get the proper informatiion but here is some code I whipped up this afternoon.

I needed a way for users of my site to log into my main site using their forum username and password and I thought this would help others trying to do the same.


<?
$server=ENTER VBULLETIN SERVER IP HERE;
$databaseusername=ENTER VBULLETIN DATABASE USERNAME HERE;
$databasepassword=ENTER VBULLETIN DATABASE PASSWORD HERE;
$databasename=ENTER VBULLETIN DATABASE NAME HERE
$chandle =mysql_connect ($server, $databaseusername", $databasepassword);
if (!mysql_select_db($datababasename, $chandle)) die("can't select database");
$username=mysql_real_escape_string($_REQUEST['username']);
$password=mysql_real_escape_string($_REQUEST['password']);
$pass=false;
$passworderr="please enter a password";
$usernameerr="please enter a username";
if (($username<>"") && ($password<>""))
{
$sql="SELECT username,password,salt FROM `user` WHERE username='".$username."' LIMIT 0 , 1";
$sresult = mysql_query($sql);
if (!$sresult) die ("bad sql in username lookup ".$sql." ".mysql_error());
$row=mysql_fetch_array($sresult);
if (mysql_affected_rows()==1)
{
if (md5(md5($password) . $row['salt']) == $row['password'])
$pass=true;
else
$passworderr="<b>invalid password</b>";
}
else
$usernameerr="<b>username not found</b>";
}

if ($pass==true)
{?>
Thank you for logging in
<? }
else
{?>
<form type="post">
<table>
<tr>
<td align=right><? echo $usernameerr ?></td>
<td><input type=text name=username value="<?echo $username ?>" width=20></td>
</tr>
<td>
<tr>
<td align=right><? echo $passworderr ?></td>
<td><input type=password name=password value="<?echo $password ?>" width=20>
<input type=submit value="log in">
</td>
</tr>
</table>
<? }?>

tom001
01-30-2011, 06:31 PM
What do you do with this?

We need to know.

Fuhrmann
03-01-2011, 01:52 PM
Nice one, thanks!!

Gursimran
04-03-2011, 06:59 PM
I have wordpress installed in the root. I don't show its login box to my users, neither ask them to register in wordpress. I wonder if I can use this integration in wordpress pages?

bigtime
07-18-2011, 04:52 PM
Thanks for the code. There's a typo.

$row['password])

Should be:

$row['password'])

Thanks,

Tim
www.SuppYo.com