Finding content in arrays
Moderator: General Moderators
Finding content in arrays
Ok so i'm realy new to php and have not been using this site for long so if someone has already asked this question
or It's realy stupid, be kind.
So i have a file called users.inc which contains:
user1:password
user2:password
now in php i creat an array called $users:
$users = file('users.inc');
which looks like:
$users[0] -> "user1:password";
$users[1] -> "user2:password";
now i found out how to check weather or not that array contains a word but all i get is a true or false answer.
say i search user1 how can i get a responce user1:password so i can then go and split it and use it?
or It's realy stupid, be kind.
So i have a file called users.inc which contains:
user1:password
user2:password
now in php i creat an array called $users:
$users = file('users.inc');
which looks like:
$users[0] -> "user1:password";
$users[1] -> "user2:password";
now i found out how to check weather or not that array contains a word but all i get is a true or false answer.
say i search user1 how can i get a responce user1:password so i can then go and split it and use it?
hmm... probably an easier way
but this is what comes to mind
that should give you the password for that user
but this is what comes to mind
Code: Select all
$users_search = array();
foreach($users AS $user){
$info = explode(':',$user);
$name = $info[0];
$password = $info[1];
$users_search[$password] = $name;
}
echo array_search('user1',$user_search);Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
also, you could do this (by the way I know there is a MUCH easier way to do this with a few str functions but I forgot, so here's how I would do it.
So this will verify that the username exists in the database, AND if it does it will also verify if the password is there.
Code: Select all
<?
$userdb = file('userdb.txt'); //Specify Database File
for($i=0; $i<count($userdb); $i++) //Start a loop to show all entries
{
$pos = strrpos($userdb[$i], ":");
$dbpass = substr($userdb[$i], $pos +1); //Make a string with just the password
$dbusr = str_replace(":$password", "", $userdb[$i]); //Make a string with just username
if($posted_username==$dbusr) //If the name from the form matches the name in this loop
{
$exists = True; //Set the variable exists to true
$checkPass = $dbpass; //Store the password in dbpass for later verification
}
}
if($exists==NULL) //If the variable exists isnt set, that means that the username entered is not in databse.
{
echo("The username you entered could not be found in our database."); //so tell them.
}
else //else (which means, basically; if it is true)
{
if($posted_password==$checkPass) //if the password you posted from form is equal to the checkPass variable we made.
{
//Do whatever you want, the persons login name and password has been verified at this point.
}
else
{
echo("The password you entered was incorrect.");
}
}
?>ARGH it had a cry:
<?php
$users = file('users.inc');
$users_search = array();
foreach($users AS $user){
$info = explode(':',$user);
$name = $info[0];
$password = $info[1];
$users_search[$password] = $name;
}
echo array_search('user1',$user_search);
?>
result:
Notice: Undefined variable: user_search in D:\web\users\users.php on line 13
Warning: array_search() [function.array-search]: Wrong datatype for second argument in D:\web\users\users.php on line 13
<?php
$users = file('users.inc');
$users_search = array();
foreach($users AS $user){
$info = explode(':',$user);
$name = $info[0];
$password = $info[1];
$users_search[$password] = $name;
}
echo array_search('user1',$user_search);
?>
result:
Notice: Undefined variable: user_search in D:\web\users\users.php on line 13
Warning: array_search() [function.array-search]: Wrong datatype for second argument in D:\web\users\users.php on line 13
echo array_search('user1',$user_search);
put an s on users in $user_search... should be $users_search
typo, sorry
put an s on users in $user_search... should be $users_search
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
That's all working now.
but...
the password resulting is not password. it's password with a return key so how do i remove that last charicter (the return)
[edit] I just added a return key to the end of the password typed
http://jamesbull.org/users
This is my demo of it. Rego yourself and login, the login goes nowhere but IT WORKS.
P.s Don't use your fav password cause this is just saved in plane text so i can read it!
but...
the password resulting is not password. it's password with a return key so how do i remove that last charicter (the return)
[edit] I just added a return key to the end of the password typed
http://jamesbull.org/users
This is my demo of it. Rego yourself and login, the login goes nowhere but IT WORKS.
P.s Don't use your fav password cause this is just saved in plane text so i can read it!