У меня есть проблема входа на этот сайт.
Я тестирую свой код (с небольшим изменением, чтобы получить код маркера) на веб-сайте Yii2 и очень хороший логин и использование cookie.
но у меня есть проблема для входа в новый целевой сайт.
проверьте мой код на своем локальном сайте и проверьте целевой сайт, форму для входа.
index.php:
<?php // i find action for login in this link: https://www.balatarin.com/login $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;
TestLogin.php:
<?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; } } }
General.php:
<?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 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); } }
ICurl.php:
<?php interface ICurl { public function login(); public function getContent($url); }