我在常规网站上有一个登录表单。如果用户使用此表单登录我的网站,他们应该同时登录论坛。如何做到这一点?有些人说使用SSI.php,但这个文件给了我另一个向SMF进行身份验证的表单。
好的,所以如果你只想从另一个php代码执行与登录表单相同的POST请求,这个函数可能会让你感兴趣:
大部分代码来自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.1rn");
fputs($fp, "Host: $hostrn");
if ($referer != '')
fputs($fp, "Referer: $refererrn");
fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
fputs($fp, "Content-length: ". strlen($data) ."rn");
fputs($fp, "Connection: closernrn");
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("rnrn", $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
);
}
数据参数是一个数组,如以下
$post_data = array(
'login' => 'yourLogin',
'pass' => 'yourPass',
...
);
然后用url调用方法(原始表单的操作?):
$result = post_request('http://www.smf-forum1.com/login.php', $post_data);
希望这是你所期望的,如果不是,我还没有理解问题:/
您可以使用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);