Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $object = new TestLogin('https://www.balatarin.com/sessions', 'i886318', 'i886318@mvrht.net');
- $object->login();
- $object->getContent('https://www.balatarin.com/users/i886318');
- function __autoload($class)
- {
- if(file_exists($class.'.php'))
- {
- require_once $class.'.php';
- }
- else
- {
- die($class. ' class does not exists.');
- }
- }
- die;
- <?php
- class TestLogin extends General implements ICurl
- {
- public function __construct($loginUrl, $username, $password)
- {
- $this->loginUrl = $loginUrl;
- $this->username = $username;
- $this->password = $password;
- parent::__construct();
- }
- public function login()
- {
- if(!$this->isLogin())
- {
- $response = curl_exec($this->curl);
- if(curl_errno($this->curl)) die(curl_error($this->curl));
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
- curl_setopt($this->curl, CURLOPT_POST, true);
- /* params */
- $params = array(
- 'session[login]' => $this->username,
- 'session[password]' => $this->password,
- 'session[remember_me]' => '1',
- 'commit' => 'ورود',
- 'utf8' => '✓',
- 'authenticity_token' => '',
- );
- /* create http query */
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
- $login = curl_exec($this->curl);
- $_SESSION['loggedIn'] = ($login === true ? true : false);
- return $login;
- }
- else
- {
- return true;
- }
- }
- public function getContent($url)
- {
- if($this->isLogin())
- {
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_BINARYTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_URL, $url);
- $content = curl_exec($this->curl);
- if (curl_errno($this->curl)) print curl_error($this->curl);
- curl_close($this->curl);
- echo 'start<br />';
- echo($content);
- echo '<br />end<br />';
- }
- else
- {
- return null;
- }
- }
- }
- <?php
- class General
- {
- protected $curl;
- protected $loginUrl;
- protected $username;
- protected $password;
- private $cookiePath = __DIR__;
- private $subClassName;
- public function __construct()
- {
- session_start();
- $this->subClassName = get_called_class();
- $this->curl = curl_init();
- //Set the useragent
- curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
- /* set login url */
- curl_setopt($this->curl, CURLOPT_URL, $this->loginUrl);
- /* set cookie */
- curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiePath."\".$this->subClassName.'_cookie.txt');
- curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiePath."\".$this->subClassName.'_cookie.txt');
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- // for ssl
- //curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- }
- protected function isLogin()
- {
- return (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] === true);
- }
- public function logout()
- {
- $cookiePath = $this->cookiePath.'\'.$this->subClassName.'_cookie.txt';
- unset($_SESSION['loggedIn']);
- if(file_exists($cookiePath)) unlink($cookiePath);
- }
- public function __destruct()
- {
- curl_close($this->curl);
- }
- }
- <?php
- interface ICurl
- {
- public function login();
- public function getContent($url);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement