简体   繁体   中英

i need help figuring out this php problem using files and if statemtents

Me and my Teacher have both looked through it and I cant seem to find the error that makes it print wrong when the email in the file is the same as the one entered

<?php
    class Account
    {
        public $EMail;
        public $PassWord;
        public $UserName;
    }

    $user = array();
    $Username = $_GET['username'];
    $FileName = "USERS//".$Username.".txt";
    if(file_exists($FileName))
    {
        $File = file($FileName);
        foreach($File as $Line)
        {
            array_push($user, $Line);
        }

        $user[0] = (string)$user[0];
        $UserDet = new Account();
        $UserDet->EMail = $user[0];

        echo $UserDet->EMail;
        $Email = $_GET['email']; $Email = (string)$Email;

        if($Email == $UserDet->EMail)
        {
            echo "correct";
        }
        else
        {
            echo "wrong";
        }
    }
    else
    {
        echo "No Account found";
    }
?>

comparing strings with an if() statement is general not advised. you can do this but, you will end up pulling out your hair like this. I would suggest fully sanitizing both input and database records something like lowercasing (strtolower())each string and removing all spaces (preg_replace('/\\s/','',$string). Then use strcmp() http://php.net/manual/en/function.strcmp.php to compare.

The file() function reads every line of a file into elements of an array. By that I mean it reads the entire line, including the linefeed at the end . If you want to skip those newlines, use the second argument to say so:

$File = file($FileName, FILE_IGNORE_NEW_LINES);

Also, why are you using a loop to put the contents of the file into an empty array? Can't you just assign the return from file() directly to the $user array? And there's no need to typecast things as string . Anything read from a file, or in $_GET is already going to be a string so there's no advantage to be gained by casting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM