function asynHttp($mod = '', $postData = array(), $cookieData = array(), $isShowReturn = false)
{
    if (!$mod) {
        showError('缺少操作模块');
    }
    $html = '';
    $nonce = getRandInt(8);
    $timestamp = TIME;
    $tmpArr = array(VCODE, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $signature = sha1(implode($tmpArr));
    $res = array('header' => '', 'body' => '');
    $urlArr = parse_url(BASE_URL);
    $hostname = $urlArr['host'];
    $port = 80;
    $errno = 0;
    $errstr = null;
    $requestPath = WEB_PATH . 'open/asyn/index?mod=' . $mod . '&nonce=' . $nonce . '&timestamp=' . $timestamp . '&signature=' . $signature;
    $fp = fsockopen($hostname, $port, $errno, $errstr, 10);
    if (!$fp) {
        showError("{$errstr} ({$errno})");
    }
    $method = 'GET';
    if (!empty($postData)) {
        $method = 'POST';
    }
    $header = "{$method} {$requestPath} HTTP/1.1\r\n";
    $header .= "Host: {$hostname}\r\n";
    if (!empty($cookieData)) {
        $_cookie = strval(NULL);
        foreach ($cookieData as $k => $v) {
            $_cookie .= $k . '=' . $v . '; ';
        }
        $cookie_str = 'Cookie: ' . $_cookie . " \r\n";
        //传递Cookie
        $header .= $cookie_str;
    }
    if (!empty($postData)) {
        $_post = strval(NULL);
        //        foreach ($postData as $k => $v) {
        //            $_post[] = $k . '=' . urlencode($v); //必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
        //        }
        //        $_post = implode('&', $_post);
        $_post = http_build_query($postData, '', '&');
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        //POST数据
        $header .= "Content-Length: " . strlen($_post) . "\r\n";
        //POST数据的长度
        $header .= "Connection: Close\r\n\r\n";
        //长连接关闭
        $header .= $_post;
        //传递POST数据
    } else {
        $header .= "Connection: Close\r\n\r\n";
        //长连接关闭
    }
    fwrite($fp, $header);
    if ($isShowReturn) {
        while (!feof($fp)) {
            $html .= fgets($fp);
        }
        list($res['header'], $res['body']) = preg_split('/\\r\\n\\r\\n|\\n\\n|\\r\\r/', $html, 2);
    }
    fclose($fp);
    return $isShowReturn ? $res : true;
}
 public function setUserLogin($userInfo = array(), $remember = 0, $saveLogin = true, $loginFrom = 'wx')
 {
     $saltkey = getRandInt(8);
     $auth = setEnocde($userInfo['uid'] . "\t" . $userInfo['aid'] . "\t" . $loginFrom, user::getAuthKey($saltkey));
     myCookie('saltkey', $saltkey, $remember);
     myCookie('auth', $auth, $remember);
     //修改登录数据
     if ($saveLogin) {
         $this->update(array('last_login_ip' => ip2long(getUserIp()), 'last_login_time' => TIME), array('uid' => $userInfo['uid']));
     }
     return true;
 }
Пример #3
0
function getRandChance($percent)
{
    return getRandInt(0, 99) < $percent;
}
 public function getCouponCode()
 {
     $code = getRandInt();
     if ($this->field('id')->where(array('coupon_code' => $code))->find()) {
         $code = $this->getCouponCode();
     }
     return $code;
 }