Wednesday, 25 September 2013

Login or Register With Yahoo Open Id in PHP

This article will guide you to crate login or register with Yahoo.

The process and steps are same as login or register through Gmail. You only need to change the path from https://www.yahoo.com/accounts/o8/id to https://me.yahoo.com.

Step1:

Include your database connection file and openid.php to you login page.
Here you can download openid.php  file.

<?php
 include('connection.php');
 require_once 'yahoo/openid.php';
 $yahooid = new LightOpenID("localhost");//your damain name
 $yahooid->identity = 'https://www.yahoo.com/accounts/o8/id';
 $yahooid->required = array(
   'namePerson/first',
   'namePerson/last',
   'contact/email',);
$yahooid->returnUrl = 'http://xxx.com/login-yahoo.php';
?>

Now Add the following code to "Login with Yahoo" button.

<a href="<?php echo $yahooid->authUrl() ?>">Login with Yahoo</a>

If the user enters the correct info then Yahoo page will redirect back to or login page with required information's.

if ($yahooid->mode == 'cancel') {
echo "User has canceled authentication !";
} elseif($yahooid->validate()) {
$data = $yahooid->getAttributes();
$email = $data['contact/email'];
$firstname = $data['namePerson/first'];
$lastname = $data['namePerson/last'];

$Identity = explode("=",$yahooid->identity);
$userid = $Identity[1];
$loginwith = 'Yahoo';

$sql = mysql_query("select userid from register where passcode='".$userid."'");
$numrow = mysql_num_rows($sql);
if($numrow > 0){
header('Location:myaccount.php');
exit();
}
}

If the user registered before then our system will redirect the user to my account page otherwise it will ask the user to register for the first time.

if($_POST['register']){
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['email']);
$passcode = mysql_real_escape_string($_POST['passcode']);
$loginwith = mysql_real_escape_string($_POST['loginwith']);
$status = 'active';
$mysql = mysql_query("insert into register set fastname = '".$fname."',
lastname = '".$lname."',
email = '".$email."',
passcode = '".$passcode."',
loginwith = '".$loginwith."',
status = '".$status."'");
header('Location:myaccount.php');
}

The MySql table used in this project

CREATE TABLE IF NOT EXISTS `register` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`fastname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`passcode` varchar(100) NOT NULL,
`loginwith` varchar(20) NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

No comments:

Post a Comment