jeudi 25 février 2016

PHP session changes unexpectedly in if statement

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';}

Strangely, when I change my if statement to the following, everything works but I don't understand why.

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

What am I missing here?

Thanks in advance for any help!

Aucun commentaire:

Enregistrer un commentaire