P
P
pagemaster2017-07-27 21:34:34
PHP
pagemaster, 2017-07-27 21:34:34

PHP script is not authorized, what parameters to pass?

I am writing a small parser that will go through a ready-made list of links to products and take current prices from there, but the problem is that the catalog is closed and available only to authorized users. Here is a piece of code with authorization functions:

<?php
// функция авторизации и сохранения куков
function post_content ($url,$postdata) {
  $uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";

  $ch = curl_init( $url );
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  curl_setopt($ch, CURLOPT_USERAGENT, $uagent);  // useragent
  curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
  curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie.txt");

  $content = curl_exec( $ch );
  $err     = curl_errno( $ch );
  $errmsg  = curl_error( $ch );
  $header  = curl_getinfo( $ch );
  curl_close( $ch );

  $header['errno']   = $err;
  $header['errmsg']  = $errmsg;
  $header['content'] = $content;
  return $header;
}


// функция обращения к страницам после авторизации, используя сохраненные куки
function get_web_page( $url )
{
  $uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";

  $ch = curl_init( $url );
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   // возвращает веб-страницу
  curl_setopt($ch, CURLOPT_HEADER, 0);           // не возвращает заголовки
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);   // переходит по редиректам
  curl_setopt($ch, CURLOPT_ENCODING, "");        // обрабатывает все кодировки
  curl_setopt($ch, CURLOPT_USERAGENT, $uagent);  // useragent
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // таймаут соединения
  curl_setopt($ch, CURLOPT_TIMEOUT, 120);        // таймаут ответа
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);       // останавливаться после 10-ого редиректа
  curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie.txt");

  $content = curl_exec( $ch );
  $err     = curl_errno( $ch );
  $errmsg  = curl_error( $ch );
  $header  = curl_getinfo( $ch );
  curl_close( $ch );

  $header['errno']   = $err;
  $header['errmsg']  = $errmsg;
  $header['content'] = $content;
  return $header;
}

  
// так выглядит тело запроса:  
// username=tronmarina&password=tronmarina&remember=yes&Submit=%D0%92%D0%BE%D0%B9%D1%82%D0%B8&option=com_users&task=user.login&return=aW5kZXgucGhwP0l0ZW1pZD00MzU%3D&23b0f12487d33d530f63635857f2dd1e=1: undefined
 
$url = 'http://zelart.com.ua/';
$result = post_content( $url, array( // вызываем функцию авторизации с параметрами
'username'=>'tronmarina',
'password'=> 'tronmarina',
'remember' => 'yes',
'Submit' => '%D0%92%D0%BE%D0%B9%D1%82%D0%B8',
'option' => 'com_users',
'task' => 'user.login'));

$html = $result['content'];
echo $html;
?>

Of course, I looked at what parameters are transmitted by the authorization form and where, but besides the login and password, "remember me", some other parameters are transmitted ...
Tell me where to look? I tried to send only the login and password - it does not authorize, I tried different combinations with those other parameters - it does not help. I tried to open the page first, take cookies, then log in again with these cookies and log in - it doesn’t work (or I did something wrong ...).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2017-07-27
@pagemaster

In order to carry out authorization, you must do the following:
1) Download the page.
1.1) You need to get COOKIEs that will return with the page
1.2) Get ALL fields from the form (2 more additional parameters are passed there that you don’t have)
2) Send a request with COOKIE, parameters from p.1.2 to the authorization url (to the same download page)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question