Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:cURL使用散列旁路登录_Php_Oop_Curl - Fatal编程技术网

PHP:cURL使用散列旁路登录

PHP:cURL使用散列旁路登录,php,oop,curl,Php,Oop,Curl,我正在尝试登录到一个具有隐藏哈希字段的表单。问题是,当我卷曲页面以获取散列时,当我在下一次卷曲调用(对同一页面)中将其作为post值包含时,散列不再有效,因为后续的卷曲cal类似于已经刷新的页面,并且它重新生成了一个新的散列 那么,如何在不模拟刷新页面的情况下获取散列呢 以下是我的示例代码: <?php $la = new LoginAuth('http://site.tld/auth.php', 'username', 'password'); $result = $la->aut

我正在尝试登录到一个具有隐藏哈希字段的表单。问题是,当我卷曲页面以获取散列时,当我在下一次卷曲调用(对同一页面)中将其作为post值包含时,散列不再有效,因为后续的卷曲cal类似于已经刷新的页面,并且它重新生成了一个新的散列

那么,如何在不模拟刷新页面的情况下获取散列呢

以下是我的示例代码:

<?php
$la = new LoginAuth('http://site.tld/auth.php', 'username', 'password');
$result = $la->auth(0);
echo $result;

class LoginAuth
{
  public $url;
  public $usr;
  public $pwd;
  public $status;
  private $last_url;

  public function __construct($url, $usr, $pwd)
  {
    $this->url = $url;
    $this->usr= $usr;
    $this->pwd= $pwd;
  }

  public function get_hash()
  {
    $output = $this->curl($this->url, $this->last_url);
    $hash = $this->match('!<input.*?name="hash".*?value="(.*?)"!ms', $output, 1);
    return $hash;
  }

  public function auth($server)
  {
    $hash = $this->get_hash();
    $auth_data = 'username=' . $this->usr . '&password=' . $this->pwd . '&server=' . $server . '&hash=' . $hash;
    $output = $this->curl($this->url, $this->last_url, $auth_data);
    $this->status = $output;
    return $output;
  }

  private function curl($url, $referer = null, $post_param = null)
  {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20");

    if($referer)
      curl_setopt($ch, CURLOPT_REFERER, $referer);

    if(!is_null($post_param))
    {
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_param);
    }

    $html = curl_exec($ch);
    $this->last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    return $html;
  }

  private function match($regex, $str, $out_ary = 0)
  {
    return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false;
  }
}
/* End of file auth.php */
/* Location: ./auth.php */

服务器可能正在向您发送会话id的设置Cookie头。它会将哈希存储在本地某个位置,然后将您提交的哈希与该哈希进行比较(如果您将会话Cookie提供给它)

您需要从
get\u hash()
响应中读取会话cookie,然后在
auth()
调用中提交回会话cookie


我会启动firebug并检查来回发送的标题,当您手动执行此操作时,可能还有其他一些重要的标题。

您可能会希望使用CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE来保存和加载cookie。是的,我看到标题中发送了“PHPSESSID=S44tft54uaihgmabrlneoakM7”。但对于复杂的curl调用,我真的是一个业余爱好者,所以如果你介意告诉我具体怎么做吗?正如Anomie所说,请阅读CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE。IIRC他们会自动为你处理。