Help - String comparisons are failing me?

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
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Help - String comparisons are failing me?

Post by Cucumberdude »

So, I'm a PHP newbie working on a basic account system.

I have one page with a password type input form. When I try to compare the pass to one I have in txt file, it fails. However, when I echo both the password and and the one I have on file, they both are identical... Help?

PS: file system works by basically creating a username.txt file for each user. second line of the file is their password.

Code: Select all

<?php
	$username = $_POST['username'];
	$password = $_POST['password'];

	$filelines = file($username.".txt");
	
	if ($filelines === false) //checking for user existence
	{
		echo("Error: that username/password combination does not exist. (1)");
	}
	elseif ($password == $filelines[1]) //checking for pass/user combo consistency
	{
		echo("Login successful, welcome to PixelNations ".$username."! You will remain logged in for 30 mintues or until you choose to log out.<br>");
		setcookie("loggedin", "1", time()+1800);
		echo("<a href='blahblah/url.php'>CLICK HERE</a>");
	}
	else
	{
		echo("Error: that username/password combination does not exist. (2)");
		echo("<br>pass = ".$password); //temporary, simply trying to get an idea of what's going on - except that they are identical! wtf?
		echo("<br>file = ".$filelines[1]); //temporary ^
	}
?>
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Help - String comparisons are failing me?

Post by mikecampbell »

file() will include the end-of-line characters. You can do something like this:

Code: Select all

if ($password == rtrim($filelines[1]))
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Re: Help - String comparisons are failing me?

Post by Cucumberdude »

Excellent, that works. I now encounter a new problem:

Warning: Cannot modify header information - headers already sent by (output started at /home/sterli41/public_html/pixelnations/loginscript.php:11) in /home/sterli41/public_html/pixelnations/loginscript.php on line 34

is displayed when the login is successful.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Help - String comparisons are failing me?

Post by Darhazer »

This is because the echo before setcookie
call setcookie first
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Re: Help - String comparisons are failing me?

Post by Cucumberdude »

Code: Select all

<?php
        $username = $_POST['username'];
        $password = $_POST['password'];

        $filelines = file($username.".txt");
       
        if ($filelines === false) //checking for user existence
        {
                echo("Error: that username/password combination does not exist. (1)");
        }
        elseif ($password == $filelines[1]) //checking for pass/user combo consistency
        {
                setcookie("loggedin", "1", time()+1800);
                echo("Login successful, welcome to PixelNations ".$username."! You will remain logged in for 30 mintues or until you choose to log out.<br>");
                echo("<a href='blahblah/url.php'>CLICK HERE</a>");
        }
        else
        {
                echo("Error: that username/password combination does not exist. (2)");
        }
?>
Still get that error.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help - String comparisons are failing me?

Post by Jonah Bron »

Is the code you gave us at the very top of the file? Is there any whitespace/other text before the opening <?php tag? Anything outputted before calling setcookie (even whitespace) will give you an error.
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Re: Help - String comparisons are failing me?

Post by Cucumberdude »

There is a bunch of html.

However, I'd like to keep that there. Any way I could call setcookie without getting rid of everything on the page?
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Re: Help - String comparisons are failing me?

Post by Cucumberdude »

Tried using include() to bypass the problem, didn't fix it.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help - String comparisons are failing me?

Post by Jonah Bron »

No, you cannot have any output before setcookie().
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Help - String comparisons are failing me?

Post by Darhazer »

You can use output buffering ( see ob_start for example or you can set implicit output buffering in your PHP settings ) to avoid this error. However, generally outputting HTML before processing login (or some other server-side logics) is a bad design, and output buffering is a workaround for your bad design.

By the way, cookie can be set with JavaScript as well.
Post Reply