gpt4 book ai didi

javascript - php session 随机丢失,无法理解为什么

转载 作者:IT王子 更新时间:2023-10-28 23:54:21 24 4
gpt4 key购买 nike

我付钱请程序员制作购物篮脚本以使用 Spreadshirt API。一切都运行良好,除了篮子自己不断清空。我认为 session 在某个时候丢失了,因此脚本创建了另一个 BasketId

我试图找出它发生的具体原因,但没有成功...我无法重现该错误。它只是随机发生,没有任何原因。关闭浏览器、重置 apache 甚至整个网络服务器都不会导致 session 丢失。

我有两个不同的脚本在同一个域上使用 cookie,它们没有任何问题(一个是用于管理员登录 session 的 cookie,另一个 cookie 用于保存用户在商店中最后查看的文章)

我尝试了在谷歌上找到的所有解决方案但没有成功:编辑 php.ini ,通过 php 强制 ini 设置,尝试了 htaccess 方式,...

这是我的 phpinfo 的“ session ”部分:http://gyazo.com/168e2144ddd9ee368a05754dfd463021

shop-ajax.php(第 18 行的 session 处理)

ini_set('session.cookie_domain', '.mywebsite.com' );
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
$language = addslashes($_GET['l']);
$shopid = addslashes($_GET['shop']);


// if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
// die("no direct access allowed");
// }



if(!session_id()) {
$lifetime=60 * 60 * 24 * 365;
$domain = ".mywebsite.com";
session_set_cookie_params($lifetime,"/",$domain);
@session_start();
}





// Configuration
$config['ShopSource'] = "com";
$config['ShopId'] = $shopid;
$config['ShopKey'] = "*****";
$config['ShopSecret'] = "*****";



/*
* add an article to the basket
*/
if (isset($_POST['size']) && isset($_POST['appearance']) && isset($_POST['quantity'])) {
/*
* create an new basket if not exist
*/
if (!isset($_SESSION['basketUrl'])) {
/*
* get shop xml
*/
$stringApiUrl = 'http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $config['ShopId'];
$stringXmlShop = oldHttpRequest($stringApiUrl, null, 'GET');
if ($stringXmlShop[0]!='<') die($stringXmlShop);
$objShop = new SimpleXmlElement($stringXmlShop);
if (!is_object($objShop)) die('Basket not loaded');

/*
* create the basket
*/
$namespaces = $objShop->getNamespaces(true);
$basketUrl = createBasket('net', $objShop, $namespaces);
$_SESSION['basketUrl'] = $basketUrl;
$_SESSION['namespaces'] = $namespaces;

/*
* get the checkout url
*/
$checkoutUrl = checkout($_SESSION['basketUrl'], $_SESSION['namespaces']);

// basket language workaround
if ($language=="fr") {
if (!strstr($checkoutUrl,'/fr')) {
$checkoutUrl = str_replace("spreadshirt.com","spreadshirt.com/fr",$checkoutUrl);
}
}

$_SESSION['checkoutUrl'] = $checkoutUrl;

}



/*
Workaround for not having the appearance id :(
*/
if ($_POST['appearance']==0) {
$stringApiArticleUrl = 'http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $config['ShopId'].'/articles/'.intval($_POST['article']).'?fullData=true';
$stringXmlArticle = oldHttpRequest($stringApiArticleUrl, null, 'GET');
if ($stringXmlArticle[0]!='<') die($stringXmlArticle);
$objArticleShop = new SimpleXmlElement($stringXmlArticle);
if (!is_object($objArticleShop)) die('Article not loaded');
$_POST['appearance'] = intval($objArticleShop->product->appearance['id']);
}


/*
* article data to be sent to the basket resource
*/
$data = array(

'articleId' => intval($_POST['article']),
'size' => intval($_POST['size']),
'appearance' => intval($_POST['appearance']),
'quantity' => intval($_POST['quantity']),
'shopId' => $config['ShopId']

);

/*
* add to basket
*/
addBasketItem($_SESSION['basketUrl'] , $_SESSION['namespaces'] , $data);

$basketData = prepareBasket();


echo json_encode(array("c" => array("u" => $_SESSION['checkoutUrl'],"q" => $basketData[0],"l" => $basketData[1])));
}




// no call, just read basket if not empty
if (isset($_GET['basket'])) {
if (array_key_exists('basketUrl',$_SESSION) && !empty($_SESSION['basketUrl'])) {

$basketData = prepareBasket();

echo json_encode(array("c" => array("u" => $_SESSION['checkoutUrl'],"q" => $basketData[0],"l" => $basketData[1])));
} else {
echo json_encode(array("c" => array("u" => "","q" => 0,"l" => "")));
}
}






function prepareBasket() {

$intInBasket=0;

if (isset($_SESSION['basketUrl'])) {
$basketItems=getBasket($_SESSION['basketUrl']);

if(!empty($basketItems)) {
foreach($basketItems->basketItems->basketItem as $item) {
$intInBasket += $item->quantity;
}
}
}

$l = "";
$pQ = parse_url($_SESSION['checkoutUrl']);
if (preg_match("#^basketId\=([0-9a-f\-])*$#i", $pQ['query'])) {
$l = $pQ['query'];
}

return array($intInBasket,$l);
}







// Additional functions
function addBasketItem($basketUrl, $namespaces, $data) {
global $config;

$basketItemsUrl = $basketUrl . "/items";

$basketItem = new SimpleXmlElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<basketItem xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net">
<quantity>' . $data['quantity'] . '</quantity>
<element id="' . $data['articleId'] . '" type="sprd:article" xlink:href="http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $data['shopId'] . '/articles/' . $data['articleId'] . '">
<properties>
<property key="appearance">' . $data['appearance'] . '</property>
<property key="size">' . $data['size'] . '</property>
</properties>
</element>
<links>
<link type="edit" xlink:href="http://' . $data['shopId'] .'.spreadshirt.' .$config['ShopSource'].'/-A' . $data['articleId'] . '"/>
<link type="continueShopping" xlink:href="http://' . $data['shopId'].'.spreadshirt.'.$config['ShopSource'].'"/>
</links>
</basketItem>');

$header = array();
$header[] = createAuthHeader("POST", $basketItemsUrl);
$header[] = "Content-Type: application/xml";
$result = oldHttpRequest($basketItemsUrl, $header, 'POST', $basketItem->asXML());
}



function createBasket($platform, $shop, $namespaces) {

$basket = new SimpleXmlElement('<basket xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net">
<shop id="' . $shop['id'] . '"/>
</basket>');

$attributes = $shop->baskets->attributes($namespaces['xlink']);
$basketsUrl = $attributes->href;
$header = array();
$header[] = createAuthHeader("POST", $basketsUrl);
$header[] = "Content-Type: application/xml";
$result = oldHttpRequest($basketsUrl, $header, 'POST', $basket->asXML());
$basketUrl = parseHttpHeaders($result, "Location");

return $basketUrl;

}






function checkout($basketUrl, $namespaces) {

$basketCheckoutUrl = $basketUrl . "/checkout";
$header = array();
$header[] = createAuthHeader("GET", $basketCheckoutUrl);
$header[] = "Content-Type: application/xml";
$result = oldHttpRequest($basketCheckoutUrl, $header, 'GET');
$checkoutRef = new SimpleXMLElement($result);
$refAttributes = $checkoutRef->attributes($namespaces['xlink']);
$checkoutUrl = (string)$refAttributes->href;

return $checkoutUrl;

}

/*
* functions to build headers
*/
function createAuthHeader($method, $url) {
global $config;

$time = time() *1000;
$data = "$method $url $time";
$sig = sha1("$data ".$config['ShopSecret']);

return "Authorization: SprdAuth apiKey=\"".$config['ShopKey']."\", data=\"$data\", sig=\"$sig\"";

}


function parseHttpHeaders($header, $headername) {

$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));

foreach($fields as $field) {

if (preg_match('/(' . $headername . '): (.+)/m', $field, $match)) {
return $match[2];
}

}

return $retVal;

}

function getBasket($basketUrl) {

$header = array();
$basket = "";

if (!empty($basketUrl)) {
$header[] = createAuthHeader("GET", $basketUrl);
$header[] = "Content-Type: application/xml";
$result = oldHttpRequest($basketUrl, $header, 'GET');
$basket = new SimpleXMLElement($result);
}

return $basket;

}




function oldHttpRequest($url, $header = null, $method = 'GET', $data = null, $len = null) {

switch ($method) {

case 'GET':

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

if (!is_null($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

break;

case 'POST':

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true); //not createBasket but addBasketItem
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

break;

}

$result = curl_exec($ch);
curl_close($ch);

return $result;

}
?>

还有脚本的其他两个部分:一个用于将 sample T 恤添加到购物篮的表单 (example.php) 和一个调用 ajax 的脚本 (shop-controller.js )。如果需要可以发布,但没有 session 处理内容。

更新 - 也许问题与 session 无关。 BasketId 丢失,但 PHPSESSID 在浏览器 cookie 中保持不变。

我在过去 3 天做了以下测试(使用不同的计算机和浏览器进行了测试):

  • 清空浏览器 cookie,然后在下午开始新的 session

  • 将 1 件商品添加到购物篮,我记下 BasketId 并检查浏览器 cookie 以记下 PHPSESSID

  • 通常总是在午夜左右,篮子自己空了

  • PHPSESSID 在我的浏览器 cookie 中保持不变,即使在购物篮自行清空之后也是如此

  • 但是BASKETID不一样,下午用的丢了重新生成一个

服务器是 CentOS 5.9 - PHP 版本 5.2.9(来自 OVH)。专用 IP 上的专用服务器。

最佳答案

首先您需要确定问题是出在 session 的垃圾回收中还是代码中的逻辑错误。为此,您可以:

// Add this right after session_start()
if (!isset($_SESSION['mySessionCheck'])) {
$_SESSION['mySessionCheck'] = "This session (" . session_id() . ") started " . date("Y-m-d H:i:s");
}

// For HTML pages, add this:
echo '<!-- ' . $_SESSION['mySessionCheck'] . ' -->';

// For AJAX pages, add "mySessionCheck" to the JSON response:
echo json_encode(
array(
"c" => array(
"u" => $_SESSION['checkoutUrl'],
"q" => $basketData[0],
"l" => $basketData[1]
),
"mySessionCheck" => $_SESSION['mySessionCheck']
)
);

如果此消息在篮子清空的同时发生变化,那么您肯定会知道这是 PHP session 的问题。

在这种情况下,您可以尝试以下几种方法:

1)你在做什么

$lifetime=60 * 60 * 24 * 365;
$domain = ".mywebsite.com";
session_set_cookie_params($lifetime,"/",$domain);
@session_start();

但是根据user contributed note来自 PHP.net 文档:

PHP's Session Control does not handle session lifetimes correctly when using session_set_cookie_params().

因此您可以尝试使用 setcookie() 代替:

$lifetime=60 * 60 * 24 * 365;
session_start();
setcookie(session_name(),session_id(),time()+$lifetime);

尽管评论中指出这是一个 4 年前的记录,但我对其进行了测试并且它仍然发生(我在 PHP 5.5.7、Windows Server 2008、IIS/7.5 上)。只有 setcookie() 生成 HTTP header 以更改到期日期(例如将 $lifetime 设置为 600):

Set-Cookie: PHPSESSID=(the id); expires=Mon, 22-Jun-2015 15:03:17 GMT; Max-Age=600

2) 如果您使用的是 Debian 服务器或某些衍生产品,它们 use a cron job to clear out PHP sessions ,所以你可以尝试:

3) 要查看是否有进程清除您的 session ,您可以查看 session 文件存储的目录(实际路径因服务器而异,使用session_save_path 查找位置你的)。我不是服务器管理员,但我读过你可以使用 auditctl为此,请确保您登录 who made the changes to your files .

4) 如果您无权访问服务器配置,或者不想依赖服务器配置(如果您切换主机则很好),您可以实现自己的 session 处理程序。看看这个 example by Pedro Gimeno .

关于javascript - php session 随机丢失,无法理解为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com