0
<?php
session_start();

//create array of data to be posted
//traverse array and prepare data for posting (key1=value1)
//set POST variables
    $url = 'xxx/cart2.php';
    $fields = array(
        'ssl_merchant_id' => ('xxx'),
        'ssl_user_id' => ('xxx'),
        'ssl_pin' => ('xxx'),
        'ssl_transaction_type' => ('xxx'),
        'confirm_code' => ($_POST['confirm_code']),
        'ssl_show_form' => ('xxx'),
        'ssl_cardholder_ip' => ($_POST['ssl_cardholder_ip']),
        'ssl_amount' => ($_POST['ssl_amount'])
    );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init('xxx/cart2.php');

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
echo $result;

?>

每次加载此代码时,它都会创建一个新会话!如何防止每次加载新会话?这只是我发现并修改为我自己使用的一个脚本,因为我以前从未使用过 cURL。

4

1 回答 1

0

创建一个名为“cookie.txt”的可写文本文件。您将需要它来保存从 cURL 返回的会话数据。

$cookie_file = "cookie.txt";
if (!file_exists($cookie_file)) {
    $handle = fopen($cookie_file, 'w') or die('Cannot open file:  '.$cookie_file);
}
if (!is_writable($cookie_file) && !chmod($cookie_file, 0777)) {
    die ("chmod() failed on file $cookie_file");
}

在这里设置选项并告诉 cURL 将 cookie 数据保存在哪里。

curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file);

因此,在您使用 cURL 发布表单后,会话数据将被保存,并且每次运行脚本时都不会加载新会话。

于 2013-03-31T03:18:54.503 回答