Tuesday, March 8, 2016

PHP session changes unexpectedly in if statement and ignores echo command

Leave a Comment

I'm setting $_SESSION['showroom'] to 'active' when a particular page in Wordpress is displayed:

if(get_the_ID()==6470||get_the_ID()==252){     $_SESSION['showroom']='active'; } 

I then set 2 arrays of pages to check against. If the next page displayed is NOT in one of these arrays, $_SESSION['showroom'] gets changed to 'inactive'.

$allowed_templates = array('template-A.php',                            'template-B.php',                            'template-C.php',                            'template-E.php',                            'template-G.php'); $allowed_ids = array(6470,252);  $template_name = get_page_template_slug(); $page_id = get_the_ID();  if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){     $_SESSION['showroom']='inactive'; } 

The if statement works most of the time, but sometimes my $_SESSION['showroom'] changes to inactive EVEN though one of the arrays is returning true! After several hours of testing I am unable to locate where the problem is. Echoing out the two parts of the if statement ALWAYS gives me 2 trues or 1 true + 1 false, but never 2 falses:

if(in_array($template_name,$allowed_templates)==false){echo 'TFALSE';} if(in_array($template_name,$allowed_templates)){echo 'TTRUE';} if(in_array($page_id,$allowed_ids)==false){echo 'IFALSE';} if(in_array($page_id,$allowed_ids)){echo 'ITRUE';} 

What am I missing here?

Thanks in advance for any help!

EDIT: Have continued testing and found the following anomaly:

    if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){     $_SESSION['showroom']='inactive';     echo 'SET TO INACTIVE'; } 

The if statement changes $_SESSION['showroom'] to 'inactive' but DOES NOT echo out 'SET TO INACTIVE'!

There's something strange going on here!

3 Answers

Answers 1

Problem solved. My code was fine. Two missing images files were causing WordPress to crash my sessions. Took 10 hours to find out but happy I found it. Thanks to everyone for their help.

Answers 2

You can try the following;

 if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){      $_SESSION['showroom']='inactive';  } 

Edit: lets try and break it down further... similar to your examples

 if(!in_array($template_name,$allowed_templates){      echo "not in templates,";  }  if(!in_array($page_id,$allowed_ids)){      echo "not in ids,";  }  if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){      echo "not in both\n";  } 

then see if we get a result with not in templates,not in ids, but no trailing not in both

Answers 3

The problem is pure logical. Lets look at this statement:

if (in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false) 

Which translates to "If the template is not valid AND page is not valid"

This means that both statements needs to be fulfilled in order to mark session as inactive. What if the template is fine, but the page is not valid? That definitely should be marked as inactive as well.

By changing the statement to read "If the template is not valid OR page is not valid", we cover up the invalid cases. Because either of them counts as an invalid state, and thus, only one of them needs to be false in order for everything to be false. (the OR-statement)

So code-wise it would be

if (in_array($template_name,$allowed_templates)==false || in_array($page_id,$allowed_ids)==false) 

And you are set.

As and addition. I would structure the code as you noted works. Which is more logical. That is, mark it as inactive whenever it's should be treated as inactive, in all other cases mark it as 'active'. Or vice-versa.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment