<?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。
答案 0 :(得分: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发布表单后,会话数据将被保存,并且每次运行脚本时都不会加载新会话。