0

我正在尝试使用 curl 登录到外部网站。我希望用户在我的索引页面上输入用户名和密码,然后当用户单击提交按钮时,登录表单会将用户名和密码都发布到 logincheck.php。它在那里检查用户名是否已在数据库中注册。如果这是真的,那么它应该将用户名和密码转发到外部站点并尝试在那里登录。如果没有,您应该会看到回声“失败”。

<?php
error_reporting(-1);

ob_start();
$host="XXXXXXX";
$username="XXXXXXX";
$password="XXXXXXX";
$db_name="XXXXXXX";
$tbl_name="XXXXXXX";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['loginid'];
$mypassword=$_POST['password'];
$loginUrl1 = 'http://externalwebsite.com/';

$myusername = stripslashes($myusername);
$myusername = mysql_real_escape_string($myusername);

$sql="SELECT * FROM $tbl_name WHERE login='$myusername'";
$result=mysql_query($sql);
if (false === $result) {
    echo mysql_error(); exit;
}

$count=mysql_num_rows($result);


$cookieFile = "test/myCookiefile.txt";
if(!file_exists($cookieFile)) {
    $fh = fopen($cookieFile, "w");
    fwrite($fh, "");
    fclose($fh);
}

if($count==1){
session_start();
$_SESSION['myusername'] = $myusername;

$curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $loginUrl1);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'gebruikersnaam='.$myusername.'&wachtwoord='.$mypassword);
    curl_setopt($curl, CURLOPT_FAILONERROR, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
    curl_exec($curl);
    $result = curl_exec($curl);
    header('Location: http://externalwebsite.com/');
    curl_close();
}
else {
    echo "failed";
}
ob_end_flush();
?>

现在,它可以很好地重定向到外部网站,但无论我尝试什么,它都不会尝试登录该网站。我究竟做错了什么?我错过了什么吗?

4

0 回答 0