SESSION question?

Discussion in 'PHP' started by mokimofiki, Jul 16, 2008.

  1. #1
    I'm trying to learn how to use sessions to store my variables so they can be used in different areas or a website as long as the person doesn't leave the site. If someone could take a look at the code below and tell me what i'm doing would appriciate it greatly.

    I have a form that stores a color to the variable siding so that based on their color choice they will see an image of that color anywhere that has a picture of siding.

    <?php
    
    session_start();
    
    $_SESSION['siding']="$_POST[siding]";
    
    if ($_SESSION['siding'] == "gray")
    $_SESSION['sidingcolor']="808.jpg";
    
    if ($_SESSION['siding'] == "white")
    $_SESSION['sidingcolor']="sidingwhite.gif";
    
    if ($_SESSION['siding'] == "psycho")
    $_SESSION['sidingcolor']="sidingpsycho.gif";
    
    include("shutters.php")
    ?>
    Code (markup):
    Thank you in advance :)
     
    mokimofiki, Jul 16, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    So do you actually have a question, or do you just want us to explain your own code to you? Or does your code not work?

    Anyway, suggestions:
    Use "else if"s, to speed it up (avoid unnecessary comparisons).
    Use a default image. (Don't ever rely on the user input).
     
    nico_swd, Jul 16, 2008 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    lol ... I know what the code that I wrote should do (its not working).

    There is a default image in place but it will change if the user chooses one of these images in the form before hand. It calls for the session variable later when it needs it.
     
    mokimofiki, Jul 16, 2008 IP
  4. wattie

    wattie Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    make sure that every PHP file that needs to load from the $_SESSION have "session_start();" at the beginning... I can't tell you why it's not working - maybe you can post a small example from two files (one setting the session and other one using it... that way we can look for coding issues)
     
    wattie, Jul 16, 2008 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Here's another way of how to write your code to be more clear.


    session_start();
    
    $_SESSION['siding'] = $_POST['siding'];
    
    switch($_SESSION['siding']){
    
       case 'gray': 
          $_SESSION['sidingcolor']="808.jpg";
          break;
       case 'white':
          $_SESSION['sidingcolor']="sidingwhite.gif";
          break;
       case 'psycho':
          $_SESSION['sidingcolor']="sidingpsycho.gif";
          break;
    
    }//end switch
    
    include("shutters.php")
    PHP:
     
    php-lover, Jul 16, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Or even shorter:

    
    <?php
    
    session_start();
    
    $images = array(
    	'gray'   => '808.jpg',
    	'white'  => 'slidingwhite.gif',
    	'psycho' => 'slidingpsycho.gif'
    );
    
    $_SESSION['sidingcolor'] = isset($images[$_POST['sliding']])
    	? $images[$_POST['sliding']]
    	: 'default.jpg';
    
    include("shutters.php");
    
    ?>
    
    PHP:
     
    nico_swd, Jul 16, 2008 IP
  7. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Or even shortest...lol:

    session_start();
    
    $images = array(
        'gray'   => '808.jpg',
        'white'  => 'slidingwhite.gif',
        'psycho' => 'slidingpsycho.gif'
    );
    
    $_SESSION['sidingcolor'] = array_search($_POST['siding'],array_flip($images));
    
    include("shutters.php");
    
    PHP:
     
    php-lover, Jul 16, 2008 IP
  8. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #8
    Thank you for all of your responses :)

    Okay this is what i have so far I used php-lover's because since i'm still learning i understand his. Nico I did save yours to pick apart later but right now it just looks like jibberish to me.

    One of the form buttons:
    <form method="post" action="startshutters.php">
    <input type="hidden" name="siding" id="siding" value="gray">
    <input type="submit" value="Next">
    </form>
    Code (markup):
    startshutters.php :
    <?php
    session_start();
    
    $_SESSION['siding'] = $_POST['siding'];
    switch($_SESSION['siding']){   
        case 'gray':       
            $_SESSION['sidingcolor']="808.jpg";      
            break;   
        case 'white':      
            $_SESSION['sidingcolor']="sidingwhite.gif";      
            break;   
        case 'psycho':      
            $_SESSION['sidingcolor']="sidingpsycho.gif";      
            break;}
    //end switch
    
    include("shutters.php")
    ?>
    Code (markup):
    shutters.php :
    <?php session_start(); ?>
    
    <table width="448" height="301" border="0" cellpadding="0" cellspacing="0" background="<?php echo "$_SESSION[sidingcolor]"; ?>">
    <tr>
    <td>
    </td>
    </tr>
    </table>
    Code (markup):
    These are just the portions in question the actual pages are much larger I just figure its easier to look at like this.

    Thank you again for looking at this everyone :)
     
    mokimofiki, Jul 16, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Lol, well technically I could put my code like this, and it'd be equally "long":
    
    $_SESSION['sidingcolor'] = isset($images[$_POST['sliding']]) ? $images[$_POST['sliding']] : 'default.jpg';
    
    PHP:
    ... but regardless, mine is slightly faster 'cause it doesn't require any function calls! (isset() is a language construct, and therefore faster than functions). :p


    And it's okay, mokimofiki. I'm using a ternary operator, which confuses a lot of people at the beginning.
     
    nico_swd, Jul 16, 2008 IP
  10. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #10
    It still doesn't work and I can't find any reason that it wouldn't ... any more ideas?

    Once this is figured out I can complete what i'm doing and i'll post a link in a couple days when i'm allowed to with the final working script and demo. I'm sure you guys can be plenty critical of the unorganized code (which will help me learn it :) )
     
    mokimofiki, Jul 16, 2008 IP
  11. wattie

    wattie Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I didn't test but just a quick look shows me that you do:

    "<?php echo "$_SESSION[sidingcolor]";
    Code (markup):
    while on the other places you type:

    $_SESSION['sidingcolor']
    Code (markup):
    Maybe the single quotes are doing the damage?
     
    wattie, Jul 16, 2008 IP
  12. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Smaller and smaller...lol

    session_start();
    
    $images = array(
        '808.jpg' => 'gray',
        'slidingwhite.gif' => 'white',
        'slidingpsycho.gif' => 'psycho' 
    );
    
    $_SESSION['sidingcolor'] = array_search($_POST['siding'],$images);
    
    include("shutters.php");
    PHP:
     
    php-lover, Jul 16, 2008 IP
  13. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #13
    Lol, not bad. But there's no default image, and the user could "hijack" the code to use no image at all. And it'd throw an E_NOTICE if $_POST['siding'] wasn't set. (Don't ever trust the user!)

    EDIT:
    There's actually a small mistake in my code. It should be:
    
    $_SESSION['sidingcolor'] = isset($_POST['siding'], $images[$_POST['siding']])
        ? $images[$_POST['siding']]
        : 'default.jpg';
    
    PHP:
     
    nico_swd, Jul 17, 2008 IP
  14. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #14
    Okay I've narrowed it down to where there has to be a problem.

    page2:
    <?php
    session_start();
    
    $_SESSION["siding"] = $_POST["siding"];
    switch($_SESSION['siding']){   
        case 'gray':       
            $_SESSION['sidingcolor']="808.jpg";      
            break;   
        case 'white':      
            $_SESSION['sidingcolor']="sidingwhite.gif";      
            break;   
        case 'psycho':      
            $_SESSION['sidingcolor']="sidingpsycho.gif";      
            break;}
    //end switch
    
    include("shutters.php")
    ?>
    Code (markup):
    page3:
    <?php 
    session_start();
    $sidingcolor = $_SESSION['sidingcolor'];
    echo "$sidingcolor";
     ?>
    Code (markup):
    No matter which color that is chosen of siding it always echo's 808.jpg for gray :(
    Again thank you all for your responses.
     
    mokimofiki, Jul 17, 2008 IP
  15. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #15
    I really need to get this working if anyone can look at my last post and let me know what i'm doing wrong. Thank you :)
     
    mokimofiki, Jul 17, 2008 IP
  16. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #16
    Echo $_POST["siding"], and see if it contains the expected value.
     
    nico_swd, Jul 17, 2008 IP
  17. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #17
    Thank you when you wrote that and I looked back at the form and i forgot the ; at the end of a line and it was skipping it and going directly to the default image.

    Thank you so much for all of your help, I had given you +rep yesterday :)
     
    mokimofiki, Jul 17, 2008 IP