Hormold

PHP VK.com oAuth Login Demo

Jan 28th, 2012
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include("vkapi.class.php");
  4. if($_GET["logout"]){$_SESSION[user_id]=null;}
  5. $appid="2773137"; $settings="photos"; $secret="<....>";
  6. $api=new vkapi($appid,$secret,$_SESSION["access_token"]);
  7. if($_GET["callback"]){
  8.     $req=$_REQUEST["code"];
  9.     $data=$api->token($req);   
  10.     if($data['access_token']){
  11.             $_SESSION['access_token'] = $data['access_token'];
  12.             $_SESSION['user_id'] = $data['user_id'];
  13.             echo "<html><body onunload='window.opener.location.reload(true);'><script>window.opener.location.reload(true);window.close();</script></body></html>";
  14.             exit();
  15.         }
  16. }else{
  17.     if($_SESSION["user_id"]!==null){$login=1;}else{$login=0;}
  18. }
  19. ?>
  20. <!DOCTYPE HTML5>
  21. <html>
  22.  <head>
  23.   <title> LogIn </title>
  24.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  25.   <script>
  26.   var appID = "<?=$appid;?>";
  27.   var settings  = "<?=$settings;?>";
  28.   var redirect  = "http://local.lc/vk.php?callback=1";
  29.   window.onload=function(){
  30.         url='http://oauth.vkontakte.ru/authorize?client_id='+appID+'&scope='+settings+'&redirect_uri='+redirect+'& display=popup&response_type=code';      
  31.   }
  32.   </script>
  33.  </head>
  34.  <body>
  35.  <?php if($login==0){?>
  36.   <button id="login" onClick='window.open(url,"vkauth", "location,width=620,height=320,top=0");'>Log in via VK.com</button>
  37.   <?php }else{ ?>
  38.   Hello, id<?=$_SESSION["user_id"];?>.<br />
  39.   <?php } ?>
  40.  </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment