Finding content in arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Finding content in arrays

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
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.
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Post by MrMaveric »

Thanx.

I'll give it a try
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post 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.
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post 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.
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Post 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
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Post by MrMaveric »

Li0rE:

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

Thanx
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

echo array_search('user1',$user_search);

put an s on users in $user_search... should be $users_search ;) typo, sorry
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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Post 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
MrMaveric
Forum Newbie
Posts: 6
Joined: Thu Jun 08, 2006 5:11 am

Post 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!
Post Reply