Forum Moderators: coopster

Message Too Old, No Replies

compare

comparing strings

         

mhbweb

8:04 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



im trying to compare in an if statement a username passed to the .php file from an html file like this.

$getuser = fgets($file);
if ($getuser==$_POST["username"]) {
echo "YES!";
} else {
echo "NOO!";
}

the problem i think is coming from the username that im drawing from a text file, for some reason it doesnt think it the same even though when you echo $getuser, its exactly the same.

can anyone help?

StupidScript

8:19 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a carriage return at the end of the username in the text file?

username\n (or username\r\n in DOS) might not match username

mhbweb

8:25 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



yes there is.. how would i get around that? would adding "\r" to the $_POST["username"] field sort it?

thanks for you help btw. :) its been driving me nuts

kumarsena

8:32 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



hey,

i tested your code. this is the test code i used. i had to add some missing code, which i assume u have in your source. it is working for me. if u still have the same problem, could u post a larger piece of ur code?

<?php
$file = fopen('test.txt', 'r');

$getuser = fgets($file);

if ($getuser==$_GET["username"]) {
echo "YES!";
}

else {
echo "NOO!";
}

?>

kumarsena

8:33 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



well there u go, seems like u got some help while i was testing.

mhbweb

8:38 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



<?php
$filename = "authcheck.txt";
$username = $_POST["username"];
$password = $_POST["password"];

echo $username;

if (!$file=fopen($filename, 'r')) {
echo "file could not be opened";
exit;
} else {
while (!feof($file)) {
$getuser = fgets($file);
$getpass = fgets($file);

if ($getuser==$username) {
echo "YES!";
exit;
} else {
echo "NOO!";}
}
}

fclose($file);
?>

this is all of it.. are $_GET and $_POST different? $_GET doesnt seem to return the value properly.

kumarsena

8:58 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



im dozing off so im off to sleep any minute...will chcek the code in the morning...well, it depends on where ur username and password are comign from. i use post when dealing wiht forms and get when passing values via url, but in ur case it is a security issue passing vis url. there is a way of encryopting i think, but im not sure how...

kumarsena

9:02 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



a quick look tells me this code, works, right?

i will test and get back tough...

mhbweb

9:12 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



no it still doesnt work.. i cant seem to be able to pull the username from the txt file and have it match..

whenever you have the time, im just really grateful for any help :):)

sleep well

Filipe

10:07 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



Have you echo'd both values to see if they are, in fact, equal at all?

If after echoing the values, you find they are equal, the next thing I'd do is trim() both values while comparing. This removes whitespace from both ends of each variable (that includes "\n", "\r", and spaces.

I'm guessing though that either $getuser or $_POST['username'] is not set to the value you think it is.

StupidScript

10:52 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Filipe's suggestion would modify your code like:

WAS:
$getuser = fgets($file);
$getpass = fgets($file);

SHOULD BE:
$getuser = trim(fgets($file));
$getpass = trim(fgets($file));

... if that's all the issue is.

mhbweb

11:03 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



yes, echoed all ok.. i ended up fixing it by adding and end char to the username and password, and inputing them into the text file without any line breaks, which was causing the problem.

i then read the file, one char at a time until i got to the end char adding each one to a seperate variable and then authenticating the whole variable, it works great.. took me back to the days of BBC basic file handling where you had no line breaks at all!

madness, thx for all your help everyone, if anyone wants to see how i got round it, even if i think it is the hardway, id be happy to show you.

mhbweb

11:04 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



thanks for that suggestion, i kept the origional so i could try any fixes.. it would be great to have done it the origional way, ill give it a go tomorrow

big hugs to everyone.