SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logging into another site via php to gather information?

    I wanted to find out how to login to another site via PHP... I don't know the proper term for it, but basically, I want a user to be able to enter their login information for another website on mine, and interact with it through mine.

    Best example I can think of is twollow.com. Once logged in to twollow.com, you can follow members on twitter through the site, without leaving twollow. I want to know how this type of thing is done.

  2. #2
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It's pretty simple. You should go to the site and look at the login form, you need:
    1. form action
    2. fields for the login/password
    3. any other significant fields, for example hidden fields with the names: "action", "mode" etc

    Then you should submit the same information by script and receive the cookies:
    Code:
    # "submit" form
    $fp = @fsockopen($domain_name, $port, $errno, $errstr, 30);
    fputs($fp, "POST http://$host$post_url HTTP/1.0\r\n");
    fputs($fp, "Host: $host\r\n");
    # it's used if you have got a cookie
    # for example you are log on and try to get a page in background
    if (!empty($cook))
       fputs($fp, "Cookie: ".$cook."\r\n");
    fputs($fp, "User-Agent: Mozilla/4.5 [en]\r\n");
    fputs($fp, "Content-Type: $content_type\r\n");
    fputs($fp, "Content-Length: ".strlen($post_str)."\r\n");
    fputs($fp, "\r\n");
    fputs($fp, $post_str."\r\n\r\n");
    
    # get the response/cookie
            while (!feof($fp)) {
                $line = fgets($fp,4096);
    
                if ($header_passed == false && ($line == "\n" || $line == "\r\n")) {
                    $header_passed = true;
                    continue;
                }
    
                if ($header_passed == false) {
                    $header_line = explode(": ", $line, 2);
                    $header_line[0] = strtoupper($header_line[0]);
                    $http_header[$header_line[0]] = chop($header_line[1]);
    
    # here you are receiving the required cookies for the next requests
                    if ($header_line[0] == 'SET-COOKIE')
                        array_push($cookies, chop($header_line[1]));
                    continue;
                }
                $result .= $line;
            }
            fclose ($fp);
    where post_str is something like (according the required params)
    Code:
    login=abcd&password=tmp
    As the result you will receive a cookies array, which can be used for the next requests (to receive a page as a logged user). This way any info from any site can be grabbed, parsed and so on.

  3. #3
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Example using Zend_Http_Client:

    PHP Code:
    <?php

    $client 
    = new Zend_Http_Client();
    $client->setCookieJar();
    $client->setUri('http://example.com/login.php');
    $client->setParameterPost(array(
        
    'username' => USERNAME,
        
    'password' => PASSWORD,
    ));
    $response $client->request('POST');

    // $response contains everything you need

    echo $response->getBody();




    // Now we're logged in, get private area!
    $client->setUri('http://example.com/members_only.php');
    $response $client->request('GET');
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  4. #4
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, BTW look at the completed solution in the manual - with cookie and so on:
    23.2.2. Adding Cookies and Using Cookie Persistence
    http://framework.zend.com/manual/en/....advanced.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •