Results 1 to 10 of 10

Thread: Session problem

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Session problem

    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.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    without seeing the rest of your code, here are some suggestions to clean that up:
    PHP 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:'];

    ?>
    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:
    PHP Code:
    $_SESSION['hat:'] = isset($_SESSION['hat:']) ? $_SESSION['hat:']++ : 1

  3. The Following User Says Thank You to traq For This Useful Post:

    ntin0s (02-11-2011)

  4. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    ntin0s (02-11-2011)

  6. #4
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    ntin0s (02-11-2011)

  9. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

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

  10. The Following User Says Thank You to Schmoopy For This Useful Post:

    ntin0s (02-11-2011)

  11. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.
    Last edited by traq; 02-06-2011 at 03:32 AM.

  12. The Following User Says Thank You to traq For This Useful Post:

    ntin0s (02-11-2011)

  13. #8
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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 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
    Last edited by ntin0s; 02-07-2011 at 01:53 AM. Reason: just spelling editing

  14. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 Code:
    <?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.
    Last edited by traq; 02-07-2011 at 03:53 AM.

  15. The Following User Says Thank You to traq For This Useful Post:

    ntin0s (02-11-2011)

  16. #10
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •