Reputation: 749
so im messing around with some php and for some reason in_array() won't pickup if something is within the array, correctly?
index.php:
<form action="login.php" method="get">
<input name="username" type="text" />
<input name="password" type="password" />
<input id="submit" type="submit" />
</form>
login.php:
<?php
$username = $_GET["username"];
$password = $_GET["password"];
include('data/user_data.php');
if(in_array($username, $users)):
echo "in array";
else: echo "not in array";
endif;
?>
user_data.php:
<?php $users = array(
dextermb => array("dextermb", "password"),
tonymb => array("tonymb", "password2")
)
?>
When inputting "dextermb" or "tonymb" into username and submitting I get the result "not in array" even though it is in the array?
Thoughts on what might be the issue?
Upvotes: 1
Views: 269
Reputation: 3763
Simply You can use a php built in function
if(array_key_exists($username, $users)){
echo "in array";
}else{
echo "not in array";
}
Upvotes: 0
Reputation: 28911
If this is your users array:
$users = array(
'dextermb' => array("dextermb", "password"),
'tonymb' => array("tonymb", "password2")
);
Then you want to simply do:
if(isset($users[$username])) {
Or alternatively:
if(array_key_exists($username, $users)) {
The username is the key in your $users
array, the in_array
method looks for values (and not nested values).
Upvotes: 4