user3350885
user3350885

Reputation: 749

Handling if condition, session and in_array in php

I have to check the session value, and check it with the constant/defined value and perform action.

The below code is not working. What I'm tyring to do is.

if the session value is A, then check it against the list array(i.e $get_abc), and perform some task if the session value is E, then check it against the list array(i.e $get_ef), and perform some task

    <?php
    $_SESSION['get_value'] = "A";

    define ("ABC", serialize (array ("A", "B", "C")));
    define ("D", serialize (array ("D")));
    define ("EF", serialize (array ("E","F")));

    $get_abc = unserialize(ABC);
    $get_d = unserialize(D);
    $get_ef = unserialize(EF);

    if (in_array($_SESSION['get_value'], $get_abc)) {
        .. do abc stuff..
    }else if(in_array($_SESSION['get_value'], $get_d)) {
        .. do d stuff..
    }else if(in_array($_SESSION['get_value'], $get_ef)) {
        .. do ef stuff..
    }else{
        .. do simple query..
    }
    ?>

Any help

Upvotes: 0

Views: 305

Answers (1)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try to add session_start()

<?php session_start();
    $_SESSION['get_value'] = "A";
    define ("ABC", serialize (array ("A", "B", "C")));
    define ("D", serialize (array ("D")));
    define ("EF", serialize (array ("E","F")));

    $get_abc = unserialize(ABC);
    $get_d = unserialize(D);
    $get_ef = unserialize(EF);

    if (in_array($_SESSION['get_value'], $get_abc)){
      echo 'do abc stuff..';
    }else if(in_array($_SESSION['get_value'], $get_d)) {
        echo 'do d stuff..';
    }else if(in_array($_SESSION['get_value'], $get_ef)) {
        echo ' .. do ef stuff..';
    }else{
         echo '.. do simple query..';
    }
    ?>

output :- do abc stuff..

Upvotes: 2

Related Questions