Instagram Login API allows the user to sign into the website using their Instagram account without sign up on that website. Here we can give an option to the user to register/ login to our system with their Instagram.
You need to generate Instagram App Client Id and Client Secret.
<?php define('INSTAGRAM_CLIENT_ID','**************'); define('INSTAGRAM_CLIENT_SECRET','**************'); define('INSTAGRAM_REDIRECT_URI','**************'); ?>
Now Add the following code to "Sign in with Instagram" button.
<a href="<?php echo $login_url ?>">Login with Instagram</a>
if(isset($_GET['code'])) { try { $instagram_ob = new InstagramApi(); // Get the access token $access_token = $instagram_ob->GetAccessToken(INSTAGRAM_CLIENT_ID, INSTAGRAM_REDIRECT_URI, INSTAGRAM_CLIENT_SECRET, $_GET['code']); // Get user information $user_info = $instagram_ob->GetUserProfileInfo($access_token); //print_r($user_info); $passcode = $user_info['id']; $fullname = $user_info['full_name']; $username = $user_info['username']; $profilepic = $user_info['profile_picture']; $loginwith = "Instagram"; /*Check if the user connected before */ $sql = db_query("select * from register_instagram where passcode='".$passcode."'"); $numrow = mysqli_num_rows($sql); if($numrow > 0){ $_SESSION['passcode'] = $passcode; header('Location:myaccount.php'); exit(); } $btnstatus = ""; } catch(Exception $e) { echo $e->getMessage(); exit; } }
/*Check if the user connected before */ $sql = db_query("select * from register_instagram where passcode='".$passcode."'"); $numrow = mysqli_num_rows($sql); if($numrow > 0){ $_SESSION['email'] = $email; $_SESSION['passcode'] = $passcode; header('Location:myaccount.php'); exit(); }
CREATE TABLE IF NOT EXISTS `register_instagram` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(50) DEFAULT NULL, `lastname` varchar(50) DEFAULT NULL, `email` varchar(60) DEFAULT NULL, `gender` varchar(10) DEFAULT NULL, `passcode` varchar(100) DEFAULT NULL, `profilepic` varchar(250) DEFAULT NULL, `loginwith` varchar(10) DEFAULT NULL, `status` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;