Logging into to a site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cgi bin laden
Forum Newbie
Posts: 2
Joined: Tue Jul 20, 2004 4:39 pm

Logging into to a site

Post by cgi bin laden »

OK - the basic premise here is this.

file_get_contents() is the best function ever :) In the world.

However - if you want to grab a webpage using this function (yes, I have my fopen wrappers correctly set) where the page is within a password protected area, I obviously run into difficulty.

So the application is this - I have a home-page type set built where a script goes out checks my mail accounts (IMAP and POP3), and returns the information (new messages etc) and displays it for me to do a quick check if there's anything I need to look at.

What I want to be able to do is log in to the 4 or 5 different forums I visit and check for private messages/new posts in watched topics etc.

So my question is - is there a way to log into a website from a script? I'm thinking no because the session data would not be stored, but I'm also thinking yes if I open a socket-based connection which I have never done in PHP....

Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check into [php_man]cURL[/php_man]
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

or if you're really brave you can open a socket to the servers and post your info, then retrieve the response =)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

im asking the exact same question here - viewtopic.php?t=24132

Mark
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

hrm... this is actually an interesting question... first... you would have to log into the site... then caputer the session cookie.. then perhaps you could get at the other pages that are protected if you had your session id... an interesting project =)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

As fyed has already pointed out, have a look at cURL. cURL is by far the easiest approach to achieve this. cURL has many options but amongst them are the ability to handle cookies, follow redirects etc.. these make using cURL for this kind of thing relatively simple.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

awe what fun is that?
cgi bin laden
Forum Newbie
Posts: 2
Joined: Tue Jul 20, 2004 4:39 pm

Post by cgi bin laden »

Bech100 wrote:im asking the exact same question here - viewtopic.php?t=24132

Mark
Thanks - I did search teh forums looking for similar posts I just couldn;t find one!!

I'll play with cURL and fingure out how to make it do what I need it to. I had seen cURL before, just wondered if anyone had built any niffy little classes to tidy it up a bit. once I've built my auto-login script, I'll post the class back here....

Thanks to all who responded.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

A little while ago I started to write a curl class but never finished it as I quickly came to the conclusion that in many cases (or at least my cases) it was quicker just to call the cURL functions.

Anyway, here is my initial workings, it is by no means complete and I certainly would not class it as 'a niffty little class', it also has no error checking but it is functional for general connections. It may be of help to you or others....

Code: Select all

<?php

class EzCurl
{
  var $ch;
  var $url;
  var $fullurl;
  var $post;
  var $get;
  var $cookie;
  var $browser;
  var $buffer;

  function EzCurl()
  {
    $this->ch      = FALSE;
    $this->url     = FALSE;
    $this->fullurl = FALSE;
    $this->post    = array();
    $this->get     = array();
    $this->cookie  = FALSE;
    $this->browser = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
    $this->buffer  = FALSE;
    $this->ch      = curl_init();
  }

  function set_url($_url)
  {
    $this->url     = $_url;
    $this->fullurl = $this->url;
  }

  function set_post_val($_key, $_value)
  {
    $this->post[$_key] = rawurlencode($_value);
  }

  function set_get_val($_key, $_value)
  {
    $this->get[$_key] = rawurlencode($_value);
  }

  function cookie_file($file)
  {
    $this->cookie = $file;
  }

  function set_browser($_browser)
  {
    $this->browser = $_browser;
  }

  function get_page($_display = FALSE)
  {
    $this->_init_curl();

    if ($_display)
    {
      echo $this->_exec_curl();
    }
    else
    {
      return $this->_exec_curl();
    }
  }

  function reintialize($_keepcookie = TRUE)
  {
    $this->ch      = FALSE;
    $this->url     = FALSE;
    $this->fullurl = FALSE;
    $this->post    = array();
    $this->get     = array();
    $this->buffer  = FALSE;
    $this->ch      = FALSE;

    if (!$_keepcookie)
    {
      $this->cookie  = FALSE;
    }
  }

  function _setopt_postvars()
  {
    curl_setopt($this->ch, CURLOPT_POST, TRUE);
    $postvals = '';
    foreach ($this->post as $key => $value)
    {
      $postvals .= $key . '=' . $value .'&';
    }

    $postvals = substr($postvals, 0, -1);
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postvals);
  }

  function _setopt_getvars()
  {
    $this->fullurl .= '?';
    foreach ($this->get as $key => $value)
    {
      $this->fullurl .= $key . '=' . $value .'&';
    }

    $this->fullurl = substr($this->fullurl, 0, -1);
  }

  function _setopt_cookies()
  {
    curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($this->ch, CURLOPT_COOKIEJAR,  $this->cookie);
  }

  function _init_curl()
  {
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($this->ch, CURLOPT_USERAGENT,      $this->browser);

    if ($this->cookie)
    {
      $this->_setopt_cookies();
    }

    if (count($this->post) > 0)
    {
      $this->_setopt_postvars();
    }

    if (count($this->get) > 0)
    {
      $this->_setopt_getvars();
    }

    curl_setopt($this->ch, CURLOPT_URL,       $this->fullurl);
  }

  function _exec_curl()
  {
    $this->buffer = curl_exec($this->ch);
    return $this->buffer;
  }
}
?>
Example usage......
Logging on to this forum....

Code: Select all

<?php

$CH = new EzCurl();

$CH->set_url('http://forums.devnetwork.net/login.php');
$CH->set_post_val('username', 'YourUsername');
$CH->set_post_val('password', 'YourPassword');
$CH->set_post_val('login',    'Login');
$page = $CH->get_page();
// if you want to output the page immediately you can use $CH->get_page(true);
echo $page;
?>
Logging in directly to your inbox....

Code: Select all

<?php

$CH = new EzCurl();

$CH->set_url('http://forums.devnetwork.net/login.php');
$CH->set_post_val('username', 'YourUsername');
$CH->set_post_val('password', 'YourPassword');
$CH->set_post_val('login',    'Login');
$CH->set_post_val('redirect', 'privmsg.php?folder=inbox');
$page = $CH->get_page();
?>
Getting some results from Google for the search term 'php'

Code: Select all

<?php

$CH = new EzCurl();

$CH->set_url('http://www.google.com/search');
$CH->set_get_val('hl', 'en');
$CH->set_get_val('ie', 'UTF-8');
$CH->set_get_val('q', 'php');
$CH->set_get_val('btnG', 'Google+Search');
$page = $CH->get_page();
?>
Post Reply