herghost Posted November 2, 2010 Share Posted November 2, 2010 Good evening everyone, I am having some problems with the correct method of writing a piece of code, basically what I have is this: if(!empty($_SESSION['reg'])) || (!empty($_SESSION['fail']))|| (!empty($_SESSION['logged'])) || (!empty($_SESSION['failedlog'])) { echo '<div id="alert">' . $_SESSION['reg'] . '</div>'; echo '<div id="alertr">' . $_SESSION['fail'] . '</div>'; echo '<div id="alert">' . $_SESSION['logged'] . '</div>'; echo '<div id="alertr">' . $_SESSION['failedlog'] . '</div>'; unset($_SESSION['reg']); unset($_SESSION['fail']); unset($_SESSION['logged']); unset($_SESSION['failedlog']); } Which should basically fire off the div alert or alertr on page load if one of the sessions exists. Only one of these sessions will ever exist at anyone time. However this gives me an parse error on the 1st line. Session_start(); is set early in the script and all sessions echo out if i printr them before the unset. Can anyone point me in the right direction of how something like this should be formatted? Many Thanks Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 Your parentheses are borked. You can use this: if(!empty($_SESSION['reg']) || !empty($_SESSION['fail']) || !empty($_SESSION['logged']) || !empty($_SESSION['failedlog'])) { Or if you are actually checking for isset, then this: if(isset($_SESSION['reg'], $_SESSION['fail'], $_SESSION['logged'], $_SESSION['failedlog'])) { Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 2, 2010 Share Posted November 2, 2010 Or if you are actually checking for isset, then this: if(isset($_SESSION['reg'], $_SESSION['fail'], $_SESSION['logged'], $_SESSION['failedlog'])) { I agree that isset() is probably the best way to check this, but adding all the vaues into one isset() will not give the intended results. When adding multiple values in isset, it will return TRUE only when all the values are true. In this case the OP wants to return true if one of the values isset. So, instead you could use: if(isset($_SESSION['reg']) || isset($_SESSION['fail']) || isset($_SESSION['logged']) || isset($_SESSION['failedlog'])) { However, if empty() is the correct test in this situation, then you could simplify the condition by concatenating the values as follows: if(!empty($_SESSION['reg'].$_SESSION['fail'].$_SESSION['logged'].$_SESSION['failedlog'])) { Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 Yep, my bad. I never use this but thought it would be shorter. Quote Link to comment Share on other sites More sharing options...
herghost Posted November 2, 2010 Author Share Posted November 2, 2010 Thanks to both of you, success! I used this in total: if(isset($_SESSION['reg']) || isset($_SESSION['fail']) || isset($_SESSION['logged']) || isset($_SESSION['failedlog']) || isset($_SESSION['logout'])) { if(isset($_SESSION['reg'])){ echo '<div id="alert">' . $_SESSION['reg'] . '</div>'; unset($_SESSION['reg']); } if(isset($_SESSION['fail'])){ echo '<div id="alertr">' . $_SESSION['fail'] . '</div>'; unset($_SESSION['fail']); } if(isset($_SESSION['logged'])){ echo '<div id="alert">' . $_SESSION['logged'] . '</div>'; unset($_SESSION['logged']); } if(isset($_SESSION['failedlogged'])){ echo '<div id="alertr">' . $_SESSION['failedlog'] . '</div>'; unset($_SESSION['failedlog']); } if(isset($_SESSION['logout'])){ echo '<div id="alertr">' . $_SESSION['logout'] . '</div>'; unset($_SESSION['logout']); session_destroy(); } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 2, 2010 Share Posted November 2, 2010 Then why do you need the first IF statement? You could get the same results with the following: $sessionIndexes = array( 'reg' => 'alert', 'fail' => 'alertr', 'logged' => 'alert', 'failedlog' => 'alertr', 'logout' => 'alertr' ); foreach($sessionIndexes as $index => $divID) { if(isset($_SESSION[$index])) { echo "<div id=\"{$divID}\">{$_SESSION[$index]}</div>"; unset($_SESSION[$index]); session_destroy(); } } Quote Link to comment 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.