View Full Version : Session problem
ntin0s
02-05-2011, 09:23 PM
Hello,
I have a problem with this :
case 'hat:':
session_start();
$_SESSION['hat:'] = 1;
if(isset($_SESSION['hat:']))
$_SESSION['hat:'] = $_SESSION['hat:'] + 1;
else
$_SESSION['hat:'] = 1;
echo "Used lives = ". $_SESSION['hat:'];
But I always get 1 as an answer.
without seeing the rest of your code, here are some suggestions to clean that up:
<?php
session_start();
// [ ...code... ]
switch($whatever){
case 'hat:':
if(isset($_SESSION['hat:'])){
$_SESSION['hat:'] = $_SESSION['hat:'] + 1;
}else{
$_SESSION['hat:'] = 1;
}
break;
}
echo "Used lives = ".$_SESSION['hat:'];
?>important parts:
1)always use full syntax, and don't leave out the { brackets }, etc. If there's trouble, that's the first place to look.
2)you were missing the switch statement for your hat: case. (I used $whatever as the variable, but use whatever you intended to use.)
3)functions like session_start() need to be called first, way up at the top.
incidentally, your if...else above could be rewritten in several quicker ways. this would be my personal preference:
$_SESSION['hat:'] = isset($_SESSION['hat:']) ? $_SESSION['hat:']++ : 1;
Schmoopy
02-06-2011, 01:36 AM
Although I agree with traq's comments on how your code is laid out, I don't see how you are getting the result you are.
The code (presuming it works, and that you just missed out the switch statement) should always echo out "Used lives = 2" because you're setting the "hat:" session variable to 1 every time and then incrementing it afterwards.
Try traq's code and see what happens.
ntin0s
02-06-2011, 02:33 AM
Thank you for replying,
traq I used your code but it stills gives me the result 1 every time. If it matters I have more cases in the code. I would prefer not to post all my code that's why I wrote just the problematic code. Schmoopy what you would suggest? I thinks each time I use that "case" its not saving the session so I get each time 1. I could sent it to you by private msg if you are interested more in helping me.
Thanks
djr33
02-06-2011, 02:47 AM
Why are you using a colon in the name? Maybe just "hat" would help. I'm not sure about this, but that's an obvious odd aspect of this code.
Schmoopy
02-06-2011, 03:07 AM
Try and isolate some code to test if your session variables are saving correctly.
So just make a random file somewhere and try this code:
<?php
session_start();
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = 1;
} else {
$_SESSION['test']++;
}
echo $_SESSION['test'];
?>
Then try refreshing the page and see what results you get. If you're always getting "1" then there's an issue with your session variables not saving properly. You should see the number increase relative to how many times you refresh the page.
my thought was that it might be a syntax issue : because your code is written in a very "shorthand" way, something might not be being parsed as you expected it to.
for example: if you have other cases in your code, and one or more of them don't have breaks (and your example didn't), then you might be getting the correct result but having it overwritten by code elsewhere - or, this might not even be the code that's running at all (Schmoopy's observation that you should be getting "2" every time could support this conclusion as well).
Try Schmoopy's example on its own and let us know if it works. otherwise, we may need to see the rest of the code.
ntin0s
02-07-2011, 01:51 AM
djr33 I tried without the colon and the same problem persisted. Ok guys i should gave you more information from the beginning, this is a chatbot and it uses cases.The case "hat:" randomly generates a letter but before that i want it to echo the session number and if i type that case again it will echo 2 and the next time 3 etc. Other cases are "echo" and echoes what you typed etc. Maybe when the case "hat:" stops then when i type again that case it refreshes or something? I can't find the problem. I think its not saving the session and its always set to what i put in the else so in this code:
<?php
session_start();
// [ ...code... ]
switch($whatever){
case 'hat:':
if(isset($_SESSION['hat:'])){
$_SESSION['hat:'] = $_SESSION['hat:'] + 1;
}else{
$_SESSION['hat:'] = 1;
}
break;
}
echo "Used lives = ".$_SESSION['hat:'];
?>
it is always 1, and if i change it to 2 will be 2 etc.
also i have breaks after each case. I have a different html file and a php file that has all the functions coding.
Thanks for helping me
It's sounding more like missing breaks and/or session_start()ing at the wrong place/time to me. post your whole code, if you don't mind, or at least the entire switch statement, and we'll see what we can find.
also, if you're going to use my example for testing, you'll need to define $whatever
<?php
session_start();
$whatever = 'hat:';
switch($whatever){
case 'hat:':
if(isset($_SESSION['hat:'])){
$_SESSION['hat:'] = $_SESSION['hat:'] + 1;
}else{
$_SESSION['hat:'] = 1;
}
break;
}
echo "Used lives = ".$_SESSION['hat:'];
?>
tested, works for me. prints "Used lives = 1" the first time, and increments each time thereafter.
If you get different results, then it's some problem on your server. Out of curiosity, try running the code <?php phpinfo(); ?> in a blank page. you can see your php version and configuration.
ntin0s
02-11-2011, 02:09 PM
finally i got what was the problem. sessions are not working because there is somewhere another php code involved that i cant see because I am using some other services like imified and phono so i will just find another way to save that.
Thank you all for your help:)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.