如何在不使用SMF登录表的情况下从其他表单登录SMF论坛

时间:2013-02-13 12:35:00

标签: php authentication smf-forum

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

2 个答案:

答案 0 :(得分: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);

希望这是你所期望的,如果没有,我还没有理解这个问题:/

答案 1 :(得分: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);