Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
HELP!! How to compare a value to multiple lines in a text file??
Old 03-12-2009, 04:52 PM HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Hey,

I created this multi-user PHP login script but I'm not sure what PHP to use to compare the POST(ed) password to a text file that has one password on each line.

For example, the text file would store the passwords like so:

password1
password2
password3
password4
etc...

When a user types in a password and then submits it, I want the script to check if the user typed in ANY of the password in the text file.

How would I go about this?

Thanks a lot,
Moatist
moatist is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-12-2009, 04:55 PM Re: HELP!! How to compare a value to multiple lines in a text file??
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
The best way to do this, would be to store the passwords in an array in a file. For example:
Password File (passwords.php):
PHP Code:
<?php
$passwords
['username'] = "password";// with a md5 encryption
$passwords['username2'] = "password2";
?>
Check file:
PHP Code:
<?php
include('passwords.php');
if(
md5($_POST['password']) !== $passwords[$_POST['username']]){
// It's ok
}
?>
However, this is very insecure (any flat file bases can be easily stolen and reversed). I suggest you look into a MySQL & PHP based solution.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 03-12-2009 at 05:00 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 03-12-2009, 05:47 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
rogem002,

With what my purposes for this script are, I'm not really worried about security. Can you or someone else tell me how to accomplish what I described above?

Thanks anyway rogem002, I'll keep what you said in mind for future PHP endeavors.



Moatist
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 05:55 PM Re: HELP!! How to compare a value to multiple lines in a text file??
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP Code:
$filePath 'path to your file';
$password $_POST['password'];

$data file_get_contents($filePath);
$dataArray explode('\n'$data);

if(
in_array($password$dataArray))
{
     
//found the password
}
else
{
     
//didn't find the password

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 07:00 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
NullPointer,

I'm not sure why but your code is not working. Could you check to see if it works for you and then let me know if you fix any bugs?

Thanks a ton!

Moatist
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 07:50 PM Re: HELP!! How to compare a value to multiple lines in a text file??
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Run the following code and post the output:
PHP Code:
$filePath 'path to your file'
echo 
'File: ' $filePath '<br />';
$password $_POST['password'];
echo 
'Password: ' $_POST['password'] . '<br />';

$data file_get_contents($filePath); 
echo 
'Data: ' $data '<br />';
$dataArray explode('\n'$data);
echo 
'Data Array: ';
print_r($dataArray);

if(
in_array($password$dataArray)) 

     
//found the password 

else 

     
//didn't find the password 

From there it shouldn't be too hard to figure out what is wrong.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 08:46 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I'm getting the following parse error message while attempting to run the code you posted above:

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in path_to_folder on line 11

Line 11 code: [PHPecho 'File: ' $filePath . '<br />';][/php]

Thanks a lot for helping!

Last edited by moatist; 03-12-2009 at 10:53 PM..
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 09:32 PM Re: HELP!! How to compare a value to multiple lines in a text file??
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
My mistake. I left out a .
PHP Code:
$filePath 'path to your file';  
echo 
'File: ' $filePath '<br />'
$password $_POST['password']; 
echo 
'Password: ' $_POST['password'] . '<br />'

$data file_get_contents($filePath);  
echo 
'Data: ' $data '<br />'
$dataArray explode('\n'$data); 
echo 
'Data Array: '
print_r($dataArray); 

if(
in_array($password$dataArray))  
{  
     
//found the password  
}  
else  
{  
     
//didn't find the password  

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 10:33 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Here is the output from the code that you supplied:

File: ../test.txt
Password:
Data: password1 password2 password3 password4
Data Array: Array ( [0] => password1 password2 password3 password4 )
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 10:37 PM Re: HELP!! How to compare a value to multiple lines in a text file??
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Was your text file line seperated or space seperated?
ie
Code:
password1 password2 password 3
OR
Code:
password1
password2
password3
If it is space seperated you'll have to change the delimiter from '\n' to ' '.
Otherwise try replacing '\n' with '\r\n'
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 10:51 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
The code is still not working.
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 10:58 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
When I store the passwords on the same line seperated by a space evrything works fine (if I change the delimiter to ' ')

Thanks a lot NullPointer. You totaly rock!
moatist is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 11:03 PM Re: HELP!! How to compare a value to multiple lines in a text file??
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Sorry my mistake once again. I forgot that PHP has to parse the contents of a string to check for newlines. '\n' literally means \n "\n" means new line. This is not the case in other languages. Working version:
PHP Code:
$filePath 'path to your file'
$password $_POST['password']; 

$data file_get_contents($filePath); 
$dataArray explode("\n"$data); 

if(
in_array($password$dataArray)) 

     
//found the password 

else 

     
//didn't find the password 

EDIT:
I didn't see your last response before I posted. Glad you got it working. In my defence I had a final today and didn't sleep last night
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-12-2009 at 11:08 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-13-2009, 04:20 PM Re: HELP!! How to compare a value to multiple lines in a text file??
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You know that the file() function will read the file into an array
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-14-2009, 07:23 AM Re: HELP!! How to compare a value to multiple lines in a text file??
Skilled Talker

Posts: 78
Name: andrei
Trades: 0
this is a great post , i thinks i could use this too
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


deyu is offline
Reply With Quote
View Public Profile Visit deyu's homepage!
 
Old 03-14-2009, 11:36 AM Re: HELP!! How to compare a value to multiple lines in a text file??
Skilled Talker

Posts: 85
Name: Ilyes Rhouma
Trades: 0
Hi,
I think with this script, the file with passwords is not encrypted, this mean that all passwords are readable, so make sur that any one will just download it !
for example someone type 'youraddress.com/passwords.txt' and then can see everything.
You may add a apache config file to not allow extern reading.

I hope this will save you and your data
__________________
Want a cheap and custom programmer or writer ?
Please login or register to view this content. Registration is FREE

Are you
Please login or register to view this content. Registration is FREE
? Start
Please login or register to view this content. Registration is FREE
free and earning money !
Please login or register to view this content. Registration is FREE
Ilyes is offline
Reply With Quote
View Public Profile Visit Ilyes's homepage!
 
Old 03-15-2009, 09:18 PM Re: HELP!! How to compare a value to multiple lines in a text file??
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Hey guys, I'm not a complete noobie (even though profile may call me a novice ).

Yes, in some situations its a completely terrible thing to use text files to store passwords. My case is an exception because I really don't care whether or not people know the passwords that aren't supposed to. Go figure.
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to HELP!! How to compare a value to multiple lines in a text file??
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 4.05368 seconds with 12 queries