Reputation: 37
I have a code that is working perfectly on my local server, but does not work on live server.
My local server is WAMP, while the live server is Unix with LiteSpeed, PHP and MySQL.
Problem is I want a new session to be created only if there is none existing. Please can anyone help with this?
The exact code is as follows:
$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"];
}
Upvotes: 0
Views: 1535
Reputation: 1162
try var_dump($cart)
right after you asign it and post the result.
You might also want to check $_Session[cart]
instead of asigning and checking,
Upvotes: 0
Reputation: 4114
As of isset()
is checking if variable is set or not, here is obvious:
$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
And this part of code doesn't check if $_SESSION['key']
is set, it check $cart
variable instead. Which is actually already set. Here is possible to check if its is_null()
or empty()
, but not isset()
.
Upvotes: 3
Reputation: 642
This one should work anywhere:
if(empty($_SESSION["cart"])){
$_SESSION["cart"] = rand(111111111,999999999);
$cart = $_SESSION["cart"];
} else
$cart = $_SESSION["cart"];
Upvotes: 0
Reputation: 2683
make sure the session is started and check if the original cart is set.
session_start();
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=rand(111111111,999999999);
}
Upvotes: 0