searching a txt file for a string.

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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

searching a txt file for a string.

Post by dull1554 »

i'm working on a login script.

Code: Select all

<?php

$loginusername = $_POST['user'];
$loginpassword = md5($_POST['password']);
$registerednameandpass = $loginusername.$loginpassword;


//I want to search the user text file for a string($registerednameandpass), i'm not sure if i have to define the entire file as a str and search that or what
//I'm just not sure how to do it

if (if search returns true(the user and pass are found) there will be a cookie set)

else{
    echo "Sorry you were not found in the database, please register";
    }

?>
when a user registers his/her username and password are stuck together and written to a txt file

Code: Select all

($_POST['username']).(md5($_POST['password']))
in the end i want to be able to take the login info, make a string in a similar way to which i did in my register script, and then take that string and search the txt file for it so I can verify if the given person is a resistered user or not.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$passwd_file=file($filename);
$loginusername = $_POST['user']; 
$loginpassword = md5($_POST['password']); 
$registerednameandpass = ($loginusername).(md5($loginpassword));
$login_ok=false;
foreach($passwd_file as $line)
  if($registerednameandpass==$line)
     $login_ok=true;
?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

I tried that and no matter what the user and passwords (even if the given user and pass are not yet registered) it always returns login ok

Code: Select all

<?php

$passwd_file=file("include/userandpass.txt");
$loginusername = $_POST['user'];
$loginpassword = md5($_POST['password']);
$registerednameandpass = ($loginusername).(md5($loginpassword));
$login_ok = false;
foreach ($passwd_file as $line)
  if ($registerednameandpass == $line)
     $login_ok = true;



if ($login_ok = true)
   echo "Login ok!";

else{
    echo "Sorry you were not found in the database, please register";
    }

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

The problem is here

Code: Select all

if ($login_ok = true)
= is assignment operator. To compare values use == operator.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

ok, the compare statement works now, but it can't find my username and password in the txt file, even though i know it's in the txt file and is correct
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

heres my code now

login.php

Code: Select all

<?php

$passwd_file=file("include/userandpass.txt");
$loginusername = $_POST['user'];
$loginpassword = md5($_POST['password']);
$registerednameandpass = ($loginusername).(md5($loginpassword));
$login_ok = false;
foreach ($passwd_file as $line)
  if ($registerednameandpass == $line)
     $login_ok = true;



if ($login_ok == true){
   echo "Login ok!";
   }

else{
    echo "Sorry you were not found in the database, please register";
    }

?>
userandpass.txt

Code: Select all

dull1554d914e3ecf6cc481114a3f534a5faf90b
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

BUMP!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

oops... forgot to remove \ns.

Code: Select all

foreach ($passwd_file as $line) 
  if ($registerednameandpass == trim($line)) 
     $login_ok = true;
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try changing this

Code: Select all

if ($registerednameandpass == $line)
to...

Code: Select all

if ($registerednameandpass == chop($line))
Mark
Last edited by JayBird on Wed Nov 26, 2003 11:14 am, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

neither helped, it still returns
Sorry you were not found in the database, please register

heres my code again

Code: Select all

<?php

$passwd_file=file("include/userandpass.txt");
$loginusername = $_POST['user'];
$loginpassword = md5($_POST['password']);
$registerednameandpass = ($loginusername).(md5($loginpassword));
$login_ok = false;
foreach ($passwd_file as $line)
  if (chop($registerednameandpass) == $line)
     $login_ok = true;



if ($login_ok == true){
    echo "Login ok!";
    }

else{
    echo "Sorry you were not found in the database, please register";
    }

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

actually

i got mine the wrong way roung shoudl have been

Code: Select all

if ($registerednameandpass == chop($line))
chop is just an alias of rtrim (trim), so if weirdans didn't work, neither will chop

Mark
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

maybe i'll just scrap the whole script and start over, i just wanted to beable to make my usere login before they were able to download something, but i dont have a database at my disposal otherwise it would be a piece of cake. I really appreciate all the help!
Post Reply