0

我在常规网站上有一个登录表单。如果用户使用此表单登录我的网站,他们应该同时登录论坛。怎么做 ?有人说使用 SSI.php 但这个文件给了我另一种形式来验证 SMF。

4

2 回答 2

0

您可以使用 CURL Php 来实现这一点。

这是一个例子

$username="username"; 
$password="password"; 
$url="http://www.simplemachines.org/"; 
$cookie="cookie.txt"; 

$postdata = "user=".$username."&passwrd=".$password."&cookielength=-1"; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);
于 2013-02-13T13:24:16.450 回答
0

好的,所以如果您只想执行与登录表单相同的 POST 请求,但来自另一个 php 代码,您可能会对这个函数感兴趣:


大部分代码来自http://www.jonasjohn.de/snippets/php/post-request.htm

function post_request($url, $data, $referer='') {

    // Convert the data array into URL Parameters like a=b&foo=bar etc.
    $data = http_build_query($data);

    // parse the given URL
    $url = parse_url($url);

    if ($url['scheme'] != 'http') { 
        die('Error: Only HTTP request are supported !');
    }

    // extract host and path:
    $host = $url['host'];
    $path = $url['path'];

    // open a socket connection on port 80 - timeout: 30 sec
    $fp = fsockopen($host, 80, $errno, $errstr, 30);

    if ($fp){

        // send the request headers:
        fputs($fp, "POST $path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");

        if ($referer != '')
            fputs($fp, "Referer: $referer\r\n");

        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ". strlen($data) ."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $data);

        $result = ''; 
        while(!feof($fp)) {
            // receive the results of the request
            $result .= fgets($fp, 128);
        }
    }
    else { 
        return array(
            'status' => 'err', 
            'error' => "$errstr ($errno)"
        );
    }

    // close the socket connection:
    fclose($fp);

    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);

    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';

    // return as structured array:
    return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
    );
}

data 参数是一个数组,如下所示

$post_data = array(
        'login' => 'yourLogin',
        'pass' => 'yourPass',
        ...
);

然后只需使用 url 调用方法(原始表单的操作?):

$result = post_request('http://www.smf-forum1.com/login.php', $post_data);

希望这是您的预期,如果不是,我还没有理解问题:/

于 2013-02-13T13:06:23.903 回答