0

我的代码在本地服务器上运行良好,但在实时服务器上无法运行。

我的本地服务器是 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"];
}
4

4 回答 4

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()or empty(),但不是isset()

于 2012-08-16T14:43:39.843 回答
0

确保会话已启动并检查是否设置了原始购物车。

session_start();
if(!isset($_SESSION['cart']))
{
    $_SESSION['cart']=rand(111111111,999999999);
}
于 2012-08-16T14:40:36.170 回答
0

这个应该可以在任何地方工作:

if(empty($_SESSION["cart"])){
    $_SESSION["cart"] = rand(111111111,999999999);
    $cart = $_SESSION["cart"];
} else
    $cart = $_SESSION["cart"];
于 2012-08-16T14:41:56.120 回答
0

分配后立即尝试var_dump($cart)并发布结果。

您可能还想检查$_Session[cart]而不是分配和检查,

于 2012-08-16T14:42:21.203 回答