Advertisement
Guest User

Untitled

a guest
May 10th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4.  
  5. //Login in with Facebook
  6. require ("XXXXXXXXXXXXXXX");
  7.  
  8. if(isset($_GET['state'])) {
  9. $_SESSION['FBRLH_state'] = $_GET['state'];
  10. }
  11. /*Step 1: Enter Credentials*/
  12. $fb = new \Facebook\Facebook([
  13. 'app_id' => 'XXXXXXXXXXXX',
  14. 'app_secret' => 'XXXXXXXXXXXXXXXXXXX',
  15. 'default_graph_version' => 'v3.3',
  16. //'default_access_token' => '{access-token}', // optional
  17. ]);
  18.  
  19. /*Step 3 : Get Access Token*/
  20. $access_token = $fb->getRedirectLoginHelper()->getAccessToken();
  21. /*Step 4: Get the graph user*/
  22. if(isset($access_token)) {
  23. try {
  24. $response = $fb->get('/me',$access_token);
  25. $fb_user = $response->getGraphUser();
  26. echo $fb_user->getName();
  27. // var_dump($fb_user);
  28. } catch (\Facebook\Exceptions\FacebookResponseException $e) {
  29. echo 'Graph returned an error: ' . $e->getMessage();
  30. } catch (\Facebook\Exceptions\FacebookSDKException $e) {
  31. // When validation fails or other local issues
  32. echo 'Facebook SDK returned an error: ' . $e->getMessage();
  33. }
  34. }
  35.  
  36. // Check if the user is already logged in, if yes then redirect him to welcome page
  37. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  38. header("location: myaccount.php");
  39. exit;
  40. }
  41.  
  42. // Include config file
  43. require_once "config.php";
  44.  
  45. // Define variables and initialize with empty values
  46. $login = $username = $email = $password = "";
  47. $login_err = $username_err = $email_err = $password_err = "";
  48.  
  49. // Processing form data when form is submitted
  50. if($_SERVER["REQUEST_METHOD"] == "POST"){
  51.  
  52. // Check if username/email is empty
  53. if(empty(trim($_POST["login"]))){
  54. $login_err = "Please enter username or email";
  55. } else{
  56. $login = trim($_POST["login"]);
  57. }
  58.  
  59. // Check if password is empty
  60. if(empty(trim($_POST["password"]))){
  61. $password_err = "Please enter your password.";
  62. } else{
  63. $password = trim($_POST["password"]);
  64. }
  65.  
  66. // Validate credentials
  67. if(empty($username_err) && empty($email_err) && empty($password_err)){
  68. // Prepare a select statement
  69. $sql = "SELECT id, username, email, password FROM users WHERE username = '$login' OR email = '$login'";
  70.  
  71. if($stmt = mysqli_prepare($link, $sql)){
  72. // Bind variables to the prepared statement as parameters
  73. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_email);
  74.  
  75. // Set parameters
  76. $param_username = $username;
  77. $param_email = $email;
  78.  
  79. // Attempt to execute the prepared statement
  80. if(mysqli_stmt_execute($stmt)){
  81. // Store result
  82. mysqli_stmt_store_result($stmt);
  83.  
  84. // Check if username exists, if yes then verify password
  85. if(mysqli_stmt_num_rows($stmt) == 1){
  86. // Bind result variables
  87. mysqli_stmt_bind_result($stmt, $id, $username, $email, $hashed_password);
  88. if(mysqli_stmt_fetch($stmt)){
  89. if(password_verify($password, $hashed_password)){
  90. // Password is correct, so start a new session
  91. session_start();
  92.  
  93. // Store data in session variables
  94. $_SESSION["loggedin"] = true;
  95. $_SESSION["id"] = $id;
  96. $_SESSION["username"] = $username;
  97. $_SESSION["email"] = $email;
  98.  
  99. // Redirect user to welcome page
  100. header("location: myaccount.php");
  101. } else{
  102. // Display an error message if password is not valid
  103. $password_err = "The password you entered was not valid.";
  104. }
  105. }
  106. } else{
  107. // Display an error message if username doesn't exist
  108. $login_err = $username_err = $email_err = "No account found with that username/email";
  109. }
  110. } else{
  111. echo "Oops! Something went wrong. Please try again later.";
  112. }
  113. }
  114.  
  115. // Close statement
  116. mysqli_stmt_close($stmt);
  117. }
  118.  
  119. // Close connection
  120. mysqli_close($link);
  121. }
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement