FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » File read-in issue
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
File read-in issue [message #170111] Mon, 11 October 2010 05:27 Go to next message
Jason Rubenstein is currently offline  Jason Rubenstein
Messages: 2
Registered: October 2010
Karma: 0
Junior Member
I am reading a set of strings in from a file, which are separated by
newlines. When I compare the variable to the string it returns that
the string is not equal, even though by appearance it is. I am sure
that there are some non displayable characters in there, but don't
know how to fix this.

Here is some sample code

The other file is just a form and I know it's working fine.

<?php
$file = fopen("pass.txt", "r") or exit("Unable to open file");
$auth = filter_var($_POST["auth"], FILTER_SANITIZE_STRING);
while (!feof($file)) {
$current_Line = fgetss($file);
filter_var($current_Line, FILTER_SANITIZE_STRING)
if (strcmp($current_LIne, $auth) == 0) {
setcookie("auth", "true", time()+3600);
echo Success;
}
}
echo Done;
?>
<html/>

As you can probably see, I'm struggling.
Re: File read-in issue [message #170112 is a reply to message #170111] Mon, 11 October 2010 06:06 Go to previous messageGo to next message
Michael Vilain is currently offline  Michael Vilain
Messages: 88
Registered: September 2010
Karma: 0
Member
In article
<715101bc-c63c-4202-92d2-8c42069dcdd2(at)x5g2000vbf(dot)googlegroups(dot)com>,
Jason Rubenstein <rubenstein(dot)jason(dot)i(at)gmail(dot)com> wrote:

> I am reading a set of strings in from a file, which are separated by
> newlines. When I compare the variable to the string it returns that
> the string is not equal, even though by appearance it is. I am sure
> that there are some non displayable characters in there, but don't
> know how to fix this.
>
> Here is some sample code
>
> The other file is just a form and I know it's working fine.
>
> <?php
> $file = fopen("pass.txt", "r") or exit("Unable to open file");
> $auth = filter_var($_POST["auth"], FILTER_SANITIZE_STRING);
> while (!feof($file)) {
> $current_Line = fgetss($file);
> filter_var($current_Line, FILTER_SANITIZE_STRING)
> if (strcmp($current_LIne, $auth) == 0) {
> setcookie("auth", "true", time()+3600);
> echo Success;
> }
> }
> echo Done;
> ?>
> <html/>
>
> As you can probably see, I'm struggling.

I did some text processing on mail files. Sometimes the text is utf-8
which is encoded differently inside variables. You can either strip the
8th bit off each byte, which filter_var() is doing. Try var_dump() on
the string to see what's actually in there.

--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically ignored]
Re: File read-in issue [message #170113 is a reply to message #170111] Mon, 11 October 2010 06:55 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
Jason Rubenstein wrote:
> I am reading a set of strings in from a file, which are separated by
> newlines. When I compare the variable to the string it returns that
> the string is not equal, even though by appearance it is. I am sure
> that there are some non displayable characters in there, but don't
> know how to fix this.
>
> Here is some sample code
>
> The other file is just a form and I know it's working fine.
>
> <?php
> $file = fopen("pass.txt", "r") or exit("Unable to open file");
> $auth = filter_var($_POST["auth"], FILTER_SANITIZE_STRING);
> while (!feof($file)) {
> $current_Line = fgetss($file);
>

Try:

$current_Line = trim(fgetss($file));

--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
Re: File read-in issue [message #170114 is a reply to message #170113] Mon, 11 October 2010 07:15 Go to previous message
Jason Rubenstein is currently offline  Jason Rubenstein
Messages: 2
Registered: October 2010
Karma: 0
Junior Member
On Oct 11, 2:55 am, Chuck Anderson <cycletour...@invalid.invalid>
wrote:
> Jason Rubenstein wrote:
>> I am reading a set of strings in from a file, which are separated by
>> newlines. When I compare the variable to the string it returns that
>> the string is not equal, even though by appearance it is.  I am sure
>> that there are some non displayable characters in there, but don't
>> know how to fix this.
>
>> Here is some sample code
>
>> The other file is just a form and I know it's working fine.
>
>> <?php
>> $file = fopen("pass.txt", "r") or exit("Unable to open file");
>> $auth = filter_var($_POST["auth"], FILTER_SANITIZE_STRING);
>> while (!feof($file)) {
>>    $current_Line = fgetss($file);
>
> Try:
>
> $current_Line = trim(fgetss($file));
>
> --
> *****************************
>  Chuck Anderson • Boulder, CO
> http://www.cycletourist.com
>  Turn Off, Tune Out, Drop In
> *****************************

I appreciate everyone's help. The problem was an extra space at the
end of the string so trim should work too. This is the resulting code
and it works, for anyone that tries this in the future.

<?php
$options = array ('flags' =>
FILTER_FLAG_STRIP_LOW ,FILTER_FLAG_STRIP_HIGH);
$file = fopen("pass.txt", "r") or exit("Unable to open file");
$auth = filter_var($_POST["auth"], FILTER_SANITIZE_STRING, $options);
while (!feof($file)) {
$current_Line = fgets($file);
$current_Line = filter_var($current_Line, FILTER_SANITIZE_STRING,
$options);
if (strcmp($current_Line, $auth) == 0) {
setcookie("the", "true", time()+3600);
}
}
echo Done;
?>
<html>
</html>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Manage Iis log file from php
Next Topic: Manage Iis log file fom php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 05:15:23 GMT 2024

Total time taken to generate the page: 0.01948 seconds