仔细看下手册你就明白了. session_id() is used to get or set the session id for the current session. If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose. session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists). 拿它做键值就相当于玩火.呵呵
用一个不确定的函数前,一定要去看手册。 The session_unset() function frees all session variables currently registered. session_unset($_SESSION[session_id()]); 改成 unset($_SESSION[session_id()]);试试
session_id() is used to get or set the session id for the current session.
If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose.
session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists). 拿它做键值就相当于玩火.呵呵
我刚尝试使用userid作为key,结果。。
第2次执行的时候,头一次存入的值的丢失了。郁闷了
第一次,第二次执行正常,但是第三次数据又丢失了
<?
$goodsid=1;
if(isset($goodsid)){
session_start();
/* if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views']."<br>"; echo session_id()."<br>";*/
/* 这里输出的居然也是第一次Views =1 ,第二次Views = 2,第三次Views =1, */
if(isset($_SESSION[session_id()])){
//非第一次载入,还需要对是否已存在做判断,有则数量+1;
echo "非第一次载入.<br>";
$shopObj = array();
$shopObj['goodsid'] = $goodsid;
$shopObj['goodsname'] = $goodsname;
$shopObj['goodsurl'] = $goodsurl;
$shopObj['unit'] = $unit;
$shopObj['price'] = $price;
$shopObj['count'] = 1; $shopList = $_SESSION[session_id()];
$shopList[count($shopList)] = $shopObj;
session_unset($_SESSION[session_id()]);
$_SESSION[session_id()] = $shopList;
}else{
//未存在,第一次载入
echo "第一次载入.<br>";
$shopObj = array();
$shopList = array();
$shopObj['goodsid'] = $goodsid;
$shopObj['goodsname'] = $goodsname;
$shopObj['goodsurl'] = $goodsurl;
$shopObj['unit'] = $unit;
$shopObj['price'] = $price;
$shopObj['count'] = 1; $shopList[0] = $shopObj; //置入session;
$_SESSION[session_id()] = $shopList;
//include template($mod, 'myshop');
}
}
$shopList = $_SESSION[session_id()];
echo count($shopList);
for( $i=0;$i<count($shopList);$i++)
{
echo var_dump($shopList[$i])."<br>";
}?>
The session_unset() function frees all session variables currently registered. session_unset($_SESSION[session_id()]);
改成
unset($_SESSION[session_id()]);试试
每次另外 echo $goodsid; 看看。。
session_unset
(PHP 4, PHP 5)session_unset -- Free all session variables
Description
void session_unset ( void )
The session_unset() function frees all session variables currently registered.
你不能用session_unset("key")去unset这个key,session_unset不是这么用的,手册表明session_unset( void ),即这个函数调用是不带参数的,将把对应的session文件的所有变量unset掉,而不是单单一个session变量。
谢谢,我是做java开发的,这是这个项目正好需要用php,只能硬着头上马了。目前检查下来问题可能出现在
if($_REQUEST)
{
if(MAGIC_QUOTES_GPC)
{
$_REQUEST = new_stripslashes($_REQUEST);
if($_COOKIE) $_COOKIE = new_stripslashes($_COOKIE);
}
else
{
$_POST = new_addslashes($_POST);
$_GET = new_addslashes($_GET);
$_COOKIE = new_addslashes($_COOKIE);
@extract($_POST);
@extract($_GET);
@extract($_COOKIE);
}
if(!defined('IN_ADMIN')) $_REQUEST = filter_xss($_REQUEST, ALLOWED_HTMLTAGS);
extract($db->escape($_REQUEST), EXTR_SKIP);
if($_COOKIE) $db->escape($_COOKIE);
}
if(QUERY_STRING && strpos(QUERY_STRING, '=') === false && preg_match("/^(.*)\.(htm|html|shtm|shtml)$/", QUERY_STRING, $urlvar))
{
parse_str(str_replace(array('/', '-', ' '), array('&', '=', ''), $urlvar[1]));
}
建议你把session相关代码改了之后,把浏览器的cookies清掉,再来测试你的页面。
session_start();
}
// 接收的商品信息
$goodsid = 3;
$goodsname = "test2";
$goodsurl = "http://www.test.com";
$unit = "test";
$price = "1";
$count = 1;session_start();
if(isset($goodsid))
{
// 输出view
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
} echo "Views=" . $_SESSION['views'] . "<br>"; // 购物车内商品个数
$size = isset($_SESSION["shopList"]) ? count($_SESSION["shopList"]) : 0; // 判断是否商品已存在,存在则只更新数量
if($size > 0 && $_SESSION["shopList"][$size-1]['goodsid'] == $goodsid)
{
$_SESSION["shopList"][$size-1]['count'] += $count;
}
else
{
$shopList = array();
$shopList['goodsid'] = $goodsid;
$shopList['goodsname'] = $goodsname;
$shopList['goodsurl'] = $goodsurl;
$shopList['unit'] = $unit;
$shopList['price'] = $price;
$shopList['count'] = $count;
$_SESSION["shopList"][] = $shopList;
}
}// 判断购物是否为空
if(isset($_SESSION["shopList"]))
{
foreach ( $_SESSION["shopList"] as $key => $value )
{
echo $value['goodsname'] . "<br>";
}
}
这样就把全部 session 数据都删除了
我看他就是因为这行把之前所有的session毁掉了, 所以下面没效果,
$_SESSION[session_id()] = $shopList; //关键就在这行