0

'Permissions' is a comma separated list of integers, ex: 1,10,70,1000 The permissions string is shoved into $_SESSION, and then returned as $type after exploding

$permissions = $_SESSION['user']['permissions'];
$type = explode(",", trim($permissions));

if(in_array(1337, $type)){
echo '<li><a href="protectedpage.php">Page Name</a></li>';}

For some reason, nothing is echoed. I've echoed $_SESSION['user']['permissions'] and gotten 1337

I've done print_r($type) and gotten Array ( [0] => 1337 )

So why isn't in_array returning true?

3
  • 4
    Use var_dump($type[0]) to see if the value stored is integer, which I really doubt. Try if(in_array("1337",$type) Commented Nov 15, 2012 at 19:30
  • 1
    I can't reproduce. It works fine for me. What does it say when you set $permissions explicitly to 1337? Commented Nov 15, 2012 at 19:30
  • var_dump will also show you if there is something like a space (which I suspect it is) in the string like 1,2, 3,4 if you compare string length to what you actually see in the dump. Commented Nov 15, 2012 at 19:35

2 Answers 2

2

I executed the following code, and it worked fine.

$permissions = '1,10,70,1000,1337';
$type = explode(",", trim($permissions));

if(in_array(1337, $type))
{
    echo 'found';
}

I recommend that you check to make sure that $permissions is what you expect it to be in the cases where it is failing. Try echoing it if the condition doesn't work, so you can check. You may also want to remove any spaces just in case that is fouling things up.

$permissions = $_SESSION['user']['permissions'];
$type = explode(",", str_replace(' ', '', $permissions));

if(in_array(1337, $type))
{
    echo 'found';
}
else
{
    echo $permissions;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try putting 1337 into quotes, '1337'. I believe that will fix your problem.

1 Comment

-1 This should only matter in strict mode (i.e. third parameter is true).

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.