PDA

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


sknight
25 Jan 2011, 22:32
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
30 Jan 2011, 20:31
What do you do with this?

We need to know.

Fuhrmann
01 Mar 2011, 15:52
Nice one, thanks!!

Gursimran
03 Apr 2011, 20:59
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
18 Jul 2011, 18:52
Thanks for the code. There's a typo.

$row['password])

Should be:

$row['password'])

Thanks,

Tim
www.SuppYo.com