0

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"];
}
Ehis Asibor
  • 37
  • 3
  • 11
  • 1
    Can you be a little more specific about it not working - do you mean that `$_SESSION["cart"]` _is_ set, but isset isn't picking it up; or it's _not_ set, and isset tells you it is? – andrewsi Aug 16 '12 at 14:39
  • what I mean is that every time i reload the page, the value of $cart changes. it shouldnt. – Ehis Asibor Aug 16 '12 at 14:40
  • @EhisAsibor Did you call `session_start()`? Your local server may have `session.auto_start` enabled. – DaveRandom Aug 16 '12 at 14:41
  • yes i called session_start();.... at the very top of the page – Ehis Asibor Aug 16 '12 at 14:43

4 Answers4

3

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().

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
0

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);
}
Waygood
  • 2,657
  • 2
  • 15
  • 16
0

This one should work anywhere:

if(empty($_SESSION["cart"])){
    $_SESSION["cart"] = rand(111111111,999999999);
    $cart = $_SESSION["cart"];
} else
    $cart = $_SESSION["cart"];
PLuS
  • 642
  • 5
  • 12
  • This should work. it must be something else. please show more code and test results – Waygood Aug 16 '12 at 14:49
  • I guess it is impossible for this not to work! It just checks and if it is empty, creates a new one, can you just paste output of `print_r($_SESSION);` ? – PLuS Aug 16 '12 at 14:55
  • PLuS , I agree with you, it should work,but unfortunately its not. I am printing the value of $cart during the check and each time i load the page, I see the value changing. Can u think of any configuration in the server setup that may be causing this? – Ehis Asibor Aug 16 '12 at 15:10
  • The possible reasons for this not to work, is these: 1. php installed on server is very old (older than 4.1), you can check this with phpversion() function. 2. you have forgotten to session_start() 3. the server you are working on, has cookies disabled 4. your browser does not allow cookies from server you can check 3 and 4 with using firebug on firefox or similar tools – PLuS Aug 16 '12 at 15:37
  • pls how do I check if cookies are disabled on server? – Ehis Asibor Aug 16 '12 at 17:08
  • Finally found the solution after a lot of research. The problem was that my server had no folder to store session variables... I had to create a new folder then place this code on each page above the session_start().........session_save_path('path to my session variable folder'); – Ehis Asibor Aug 16 '12 at 18:22
0

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,

sloth
  • 99,095
  • 21
  • 171
  • 219
Dvid Silva
  • 1,110
  • 13
  • 25