Jump to content

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/
Share on other sites

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'])) {

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129610
Share on other sites

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'])) {

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129628
Share on other sites

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();
		}
	}


?>

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129635
Share on other sites

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();
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129649
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.