Page 1 of 1

Finding content in arrays

Posted: Thu Jun 08, 2006 5:20 am
by MrMaveric
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?

Posted: Thu Jun 08, 2006 5:29 am
by s.dot
hmm... probably an easier way
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);
that should give you the password for that user

Posted: Thu Jun 08, 2006 6:26 am
by MrMaveric
Thanx.

I'll give it a try

Posted: Thu Jun 08, 2006 6:32 am
by Li0rE
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.

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.");
     }
}
?>
So this will verify that the username exists in the database, AND if it does it will also verify if the password is there.

Posted: Thu Jun 08, 2006 6:40 am
by Li0rE
woah. Im really surprised I've never thought of just making it into an array to split username:password. I have many scripts that do that like shopping carts that display id.quantity, etc. but never thought of using an array to parse them.

Posted: Thu Jun 08, 2006 6:44 am
by MrMaveric
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

Posted: Thu Jun 08, 2006 6:48 am
by MrMaveric
Li0rE:

once i wrap my head around that code i will try it out.

Thanx

Posted: Thu Jun 08, 2006 6:58 am
by s.dot
echo array_search('user1',$user_search);

put an s on users in $user_search... should be $users_search ;) typo, sorry

Posted: Thu Jun 08, 2006 7:02 am
by sweatje
You have to choose just one name for the variable, it is either $users_search or $user_search. ;)

The error points out just where you chose to switch names.

Posted: Thu Jun 08, 2006 7:08 am
by MrMaveric
scottayy wrote:echo array_search('user1',$user_search);

put an s on users in $user_search... should be $users_search ;) typo, sorry
I am blind. Thank you (it worked)

i am still trying to figure out everything in that large one. 8O I will get it soon

Posted: Thu Jun 08, 2006 7:18 am
by MrMaveric
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 :D

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!