Local login validation

Asked

Viewed 31 times

-1

I need to do a login validation without database, that is, with the arrays I pass, but I don’t know how to check if there is an item inside an array.

NOTE: I have tried using the in_array.

PHP

<?php
    //Pegando usuário e senha do formulário (index.php)
    $user = $_POST['user'];
    $pass = $_POST['pass'];

    //Usúarios e senhas que podem ter acesso
    $users = array('admin', 'admin2', 'admin3', 'admin4', 'admin5');
    $senhas = array('senha', 'senha2', 'senha3', 'senha4', 'senha5');

    //Convertendo usuário e senha recebidos em apenas uma array
    $login = array($user, $pass);

    if (!empty($user) and !empty($pass)) {
        
    } else {
        echo "<script>alert('Preencha todos os campos.'); window.location.href='index.php';</script>";
        exit();
    }

1 answer

0


PHP has the function array_search which can be used for your case check in the documentation and heed her return.

Example:

<?php

$user = 'admin';
$pass = 'senha';

//Usúarios e senhas que podem ter acesso
$users = array('admin', 'admin2', 'admin3', 'admin4', 'admin5');
$senhas = array('senha', 'senha2', 'senha3', 'senha4', 'senha5');

$index = array_search($user, $users);

if ($index !== false && isset($senhas[$index]) && $senhas[$index] === $pass) {
    echo 'Login valido!';
}
else {
    echo 'Login incorreto!';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.