3

For a small project I'm working on I want a user to be able to register an account using a username however I don't want to configure a database i.e I just want to compare the username the user inputs with a PHP array.

<?php
$a=array("user1","user2");
for($a =0; $x<$arrlength; $a++){
if($username == $a){ //i want to say if $username is in array alert this and do nothing
$echo print a new username, already taken;
return false;
else(
array_push($a,username);?>

I'm working with the w3 schools PHP examples and I have something like this (the user inputs a 'username' in a form. I was wondering how you would actually implement this functionality properly.

1
  • 1
    You would still need to store the user list somewhere if you want to permanently store the user. A database is the best way. But if you really don't want to use a database, then you would have to store it in a flat text file. You could do something like file_put_contents('users.txt', serialize($a)) at the end of the file to store the contents of $a in users.txt. Then in the beginning of the file you would do $a=unserialize(file_get_contents('users.txt')); Commented May 23, 2016 at 23:08

5 Answers 5

4

You could simply use the in_array() function:

<?php
$usernames = array("user1", "user2", "user3", "user4");

if (in_array("username_from_user_input", $usernames)) {
    echo "Got Username";
}else{
    echo "Username not found"
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks George that's what i was looking for and it makes sense. It's only a small little test i'm doing (i'm new to PHP) for myself thanks for the help!
Sometimes the simplest answer is the best answer.
2

If you want to save the registered accounts across requests, you have to store them in a file or database (or something else..), because the script is run from start on every requests.

Instead of looping over the array, you could also use the in_array function. ( http://php.net/manual/de/function.in-array.php )

To store the usernames, take a look at the file functions. (e.g. http://php.net/manual/en/function.file-put-contents.php to write to a file, and file_get_contents or file(..) to read a file). (For real projects, make sure to lock the file to prevent race conditions.)

1 Comment

S/he should also consider blocking access to the file on the server.
1

Late answer, but because I'm on a ternary mood, here it goes:

echo (in_array("someuser", array("user1","user2"))) ? "Got Username" : "Username not found";

1 Comment

Got my vote as it's a valid alternative (not to mention I love ternary too ;-))
0

You can use in_array:

if (in_array($username, $a){
  ...
}

Comments

0

Try the solution of the other users but you could combine with Traits.

You could create a Trait called VerifyUser with the next content:

trait VerifyUser {
    public function verifyUser($user){
        if (in_array($username, $usersArray)){
            echo $username . "is already taken";
        }else {
            array_push($usersArray, $username);
        }
    }
}

Don't forget add your custom array to your class if you are working in a OOP environment. Add this trait with the use VerifyUser instruction.

class UsersCollection{
    use VerifyUsers

    $usersArray= ["user1", "user2"];

    $this->verifyUser("user1"); //It echo the custom message you could return a boolean value and then specify a custom behaviour.
}

2 Comments

I think this is way too advanced for someone new to PHP.
Correct, but I only answered this need and may be useful for other users newbies or advanced. The answer is use the in_array function but I only exposed a way to improve this, facilitating the code reusability.

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.