merylvingien Posted November 29, 2010 Share Posted November 29, 2010 Hi girls and boys I am trying to set a variable if a session OR a cookie has been set, but am unsure on how to write the statement... if (isset($_SESSION['name'])||isset($_COOKIE['name'])) {$variable = $_SESSION['name']||$_COOKIE['name'];} Obviously not working there, but just need a pointer here. any help is appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/220166-setting-a-variable-if-a-session-or-cookie-isset/ Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 This would probably make more sense. if ( isset($_SESSION['name']) ) { $variable = $_SESSION['name']; } elseif( isset($_COOKIE['name']) ) { $variable = $_COOKIE['name']; } else { // default action if neither is set $variable = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/220166-setting-a-variable-if-a-session-or-cookie-isset/#findComment-1141061 Share on other sites More sharing options...
merylvingien Posted November 29, 2010 Author Share Posted November 29, 2010 Thanks for the reply, but i tried this but wont work unless the cookie is set. I have only ever worked with either cookies or sessions, never both! Seems impossible to get both to work together smoothly. Also for some reason, when i add this code to the top of the image upload file, it breaks the code, everything stops working correctly.... Headache!!!! Quote Link to comment https://forums.phpfreaks.com/topic/220166-setting-a-variable-if-a-session-or-cookie-isset/#findComment-1141073 Share on other sites More sharing options...
ManiacDan Posted November 29, 2010 Share Posted November 29, 2010 Thanks for the reply, but i tried this but wont work unless the cookie is set. I have only ever worked with either cookies or sessions, never both! Seems impossible to get both to work together smoothly. Also for some reason, when i add this code to the top of the image upload file, it breaks the code, everything stops working correctly.... Headache!!!! What Pikachu gave you will work exactly right, you may not be printing the right variables. Right before this IF check, do: var_dump($_COOKIE);var_dump($_SESSION); See what comes out. I bet the variables aren't set the way you think they are. To be clear, what we think you're saying is: "I want $variable to be the value of EITHER $_COOKIE['name'] OR $_SESSION['name'}, whichever is set. If both are set, use the $_COOKIE variable." If that's not what you mean, rephrase the question. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/220166-setting-a-variable-if-a-session-or-cookie-isset/#findComment-1141074 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 I just tested it locally, and it works fine. What do you mean it won't work unless the cookie is set? Quote Link to comment https://forums.phpfreaks.com/topic/220166-setting-a-variable-if-a-session-or-cookie-isset/#findComment-1141076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.