1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

curl remote login and redirect

Discussion in 'PHP' started by bumbar, Mar 11, 2014.

  1. #1
    Hello,

    Here is my code to login

     $tmp_fname = tempnam("/tmp", "COOKIE");
    $post = array(
        'username=user',
        'password=xxxx',
    );
    
    $post = implode('&', $post);
    $curl_handle = curl_init ("http://example.com/login.php");
    curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmp_fname);
    curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
    $output = curl_exec ($curl_handle);
    $curl_handle = curl_init ("http://example.com/ads.php");
    curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmp_fname);
    curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($curl_handle);
    echo $output
    PHP:
    This code works.
    The problem is that I want to redirect to the remote site. But not saved session .. or do not know how to use cookies.
    Can anyone tell how after I made a successful remote authorization, I can navigate through the remote site.
    Lost session mybe..

    Thanks.
     
    bumbar, Mar 11, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    What have you tried to use to navigate the site? The second part of the code above should work for multiple URLs unless the website checks for valid user agents.
     
    ThePHPMaster, Mar 12, 2014 IP
  3. Usama Aziz

    Usama Aziz Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    123
    #3
    Well I think it is easy to redirect page after authentication.
    Simply the with you $output variable.


    <?php
    
    $tmp_fname = tempnam("/tmp", "COOKIE");
    $post = array(
        'username=user',
        'password=xxxx',
    );
    $post = implode('&', $post);
    $curl_handle = curl_init ("http://example.com/login.php");
    curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmp_fname);
    curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
    $output = curl_exec ($curl_handle);
    $curl_handle = curl_init ("http://example.com/ads.php");
    curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmp_fname);
    curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($curl_handle);
    echo $output
    
    //redirection
    if($output == "authorized"){
    header("location:desired.url");
    
    //redirection end
    }
    
    ?>
    PHP:
     
    Usama Aziz, Mar 15, 2014 IP