1

I've read all the other articles on in_array, and still don't understand why mine is givng odd results. I'm inheriting this code from someone else, and don't fully understand why they did certain things. When a user logs in, data is grabbed from a db, one of the fields being their "level". 1 is an admin, 2 a regular user, etc. Once the data is grabbed from the db, we put the user level (stored as a:1:{i:0;s:1:"2") into an array:

$user_level = unserialize($this->result['user_level']); 
$_SESSION['unt']['user_level'] = $user_level;

Later we check to see if this is an admin:

error_log(print_r($_SESSION['abc']['user_level']));  //this is always "1"
if (in_array('1', $_SESSION['abc']['user_level'])) {   //should be yes, correct?

Yet somehow the if statement never evaluates as true, even though the SESSION variable is 1. What am I missing?

1
  • Your print_r should have true as the second parameter if you are passing the result instead of printing it e.g. error_log(print_r($_SESSION['abc']['user_level'], true)); Commented Oct 16, 2013 at 17:54

2 Answers 2

2

$_SESSION['abc']['user_level'] doesn't appear to be an array. Looks like you want one of the following.

If gettype($_SESSION['abc']['user_level']) is 'integer':

if ($_SESSION['abc']['user_level']) === 1) {

If gettype($_SESSION['abc']['user_level']) is 'string':

if ($_SESSION['abc']['user_level']) === '1') {

If gettype($_SESSION['abc']['user_level']) is 'string' and its value actually contains the quotes:

if ($_SESSION['abc']['user_level']) === '"1"') {

If it was an array the output would have this structure, not just "1":

Array
(
    [0] => 1
)
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting - when I use gettype, it comes back as an array, so none of the above work!
0

Even though I noticed closing bracket } missing from your input string I just assumed that you probably might have missed while copy-pasting .

a:1:{i:0;s:1:"2"

So in_array is not the problem but your input string is the problem .
With display_errors setting Off you would not see any error when you try to unserialize it .

You could use the below function to check if the input is a valid string to unserialize :

//  Copied from http://www.php.net/manual/en/function.unserialize.php
function is_serialized( $str )
{
    return( $str == serialize( false ) || @unserialize( $str ) !== false );
}

Then something along these lines :

    $inputString = 'a:1:{i:0;s:1:"2";}';   //  Is valid and is holding one array of info
//  $inputString = 'a:1:{i:0;s:1:"2"';   //  Invalid as is missing a closing baracket }

if( ! is_serialized( $inputString ) )
{
    echo 'Is not serialized';
}
else
{
    $user_level = unserialize( $inputString );

    $_SESSION['unt']['user_level'] = $user_level;   //  Is an array

//  Note the second argument as was already pointed by @AsksAnyway
    error_log( print_r( $_SESSION['unt']['user_level'] , true ) );

    var_dump( in_array( '1' ,$_SESSION['unt']['user_level']) );

    var_dump( in_array( '2' ,$_SESSION['unt']['user_level']) );
}

3 Comments

Interesting. So even though gettype says it's an array, print_r prints only the number 1. Both var_dumps come back as NULL.
Your serialized string is missing ending curly bracket } : a:1:{i:0;s:1:"2" and it seems that you turned Off display_errors . In that scenario , I too got the same result : 1 , null , null . If the display_errors is On PHP would have failed while unserializeing . I have updated my answer accordingly .
I missed the ending curly bracket when I cut and pasted, and turned on display_errors. The solution from Uours worked like a charm - thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.