我的代码在我的本地服务器上运行良好,但在实时服务器上不起作用。
我的本地服务器是WAMP,而实时服务器是带有LiteSpeed,PHP和MySQL的Unix。
问题是我希望只有在没有现有会话的情况下才能创建新会话。请任何人都可以帮忙吗?
具体代码如下:
$cart = $_SESSION["cart"];
if (isset($cart))// this checks if session has been created already.
$cart = $cart; // if session is already set, it uses the random value already created.
else {
$_SESSION["cart"] = rand(111111111,999999999);// if session has not be created before a new randome number is picked.
$cart = $_SESSION["cart"];
}
答案 0 :(得分:3)
从isset()
开始检查是否设置了变量,这里显而易见:
$cart = $_SESSION["cart"]; // setting the variable $cart and assigning it some value
if (isset($cart)) // this checks if session has been created already
// and it will return TRUE anyway because `$cart` is already defined above regardless value it was assigned
这部分代码不会检查是否设置了$_SESSION['key']
,而是检查$cart
变量。实际上已经设定了。可以检查其is_null()
或empty()
,而不是isset()
。
答案 1 :(得分:0)
确保会话已启动并检查是否已设置原始购物车。
session_start();
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=rand(111111111,999999999);
}
答案 2 :(得分:0)
这个应该适用于任何地方:
if(empty($_SESSION["cart"])){
$_SESSION["cart"] = rand(111111111,999999999);
$cart = $_SESSION["cart"];
} else
$cart = $_SESSION["cart"];
答案 3 :(得分:0)
在签名后立即尝试var_dump($cart)
并发布结果。
您可能还想检查$_Session[cart]
,而不是分配和检查