Answer the question
In order to leave comments, you need to log in
PHP: How to determine the value of a found key in an array?
Hello!
There is an array:
$arr = array (
"k1" => "v1",
"k2" => "v2"
);
$var1 = array_key_exists($login, $arr);
$login = trim($_POST["login"]);
$var2 = in_array($password, $arr); // $password - по аналогии с логином
Answer the question
In order to leave comments, you need to log in
The essence of the question is not clear at all, there are few details ...
The array_key_exists and in_array functions check the presence of the desired key or value in the array.
What exactly do you need?
Do you have an array where the key is the username and the value is the password?
$arr = [
'user1' => '123',
'user2' => '124'
];
$login = trim($_POST['login']);
if (isset($users[$login ]) {
$key = $login;
$value = $users[$login];
} else {
echo 'Нет нужного ключа в массиве';
}
$users = [
'user1' => '123456'
];
// post request ...
$post = array_map('trim', $_POST);
if (isset($users[$post['login']]) && isset($post['password']) && $users[$post['login']] === $post['password'])
{
//login successful
$login = $post['login'];
$password = $users[$post['login']];
}
else
{
// paste error to session and redirect to ths page. use pattern PRG
header('Location: http://example.com/redirect_to_this_page');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question