[php] How do I show a message in the foreach loop?

A user goes to the URL:

http://mywebsite.com/index.php?company=walmart

Once the page loads i want to check if the registered user can access this people. To do that i check with an array save in session for the user.

Array ( [0] => ebgames [1] => walmart ) 

Using an if/else statement inside a for each loop i want to show a message or redirect.

$_SESSION['accessto'] holds the array.

I tried this but no luck.

$companyname = $_GET['company']; $accessto = $_SESSION['member_accessto']; foreach ($accessto as $key => $val) {     if ($val == $companyname) {         echo 'You have access to this company page of "'.$companyname.'"';     } else {         header('Location:/login');     } } 

print_r($_SESSION['member_accessto']); gives me the following

Array ( [0] => ebgames [1] => walmart )  

This question is related to php arrays

The answer is


You are looking to see if a single value is in an array. Use in_array.

However note that case is important, as are any leading or trailing spaces. Use var_dump to find out the length of the strings too, and see if they fit.