Page 1 of 1

searching a txt file for a string.

Posted: Tue Nov 25, 2003 10:39 pm
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.

Posted: Wed Nov 26, 2003 5:45 am
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;
?

Posted: Wed Nov 26, 2003 8:40 am
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";
    }

?>

Posted: Wed Nov 26, 2003 9:15 am
by Weirdan
The problem is here

Code: Select all

if ($login_ok = true)
= is assignment operator. To compare values use == operator.

Posted: Wed Nov 26, 2003 9:59 am
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

Posted: Wed Nov 26, 2003 10:02 am
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

Posted: Wed Nov 26, 2003 10:54 am
by dull1554
BUMP!

Posted: Wed Nov 26, 2003 10:58 am
by Weirdan
oops... forgot to remove \ns.

Code: Select all

foreach ($passwd_file as $line) 
  if ($registerednameandpass == trim($line)) 
     $login_ok = true;

Posted: Wed Nov 26, 2003 10:59 am
by JayBird
try changing this

Code: Select all

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

Code: Select all

if ($registerednameandpass == chop($line))
Mark

Posted: Wed Nov 26, 2003 11:10 am
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";
    }

?>

Posted: Wed Nov 26, 2003 11:13 am
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

Posted: Wed Nov 26, 2003 11:20 am
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!