cURL PHP code redirect after success

481 views Asked by At

I created a Facebook login type code using cURL, and I was wondering how to redirect back to my website after a successful login to Facebook. Code is shown below:

<?PHP
    $login_email = '****';
    $login_pass = '****';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&  pass='.urlencode($login_pass).'&login=Login');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $page = curl_exec($ch) or die(curl_error($ch));

    echo $page;
?>
1

There are 1 answers

0
nonamorando On

First, you don't have to use cURL to login to Facebook. Facebook provides its own API.

You can check if the $session variable in that API succeeds and then call the header method. Also see: FaceBookRedirectLoginHelper

And if you're looking to redirect, use header() function:

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>