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
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).
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.
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)
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:
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:
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:
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
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). And it's okay, mokimofiki. I'm using a ternary operator, which confuses a lot of people at the beginning.
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 )
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?
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:
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:
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.
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
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