0

Hello all, I have a question that is more based off of a unique occurrence (unique for me having no real prior php knowledge).

I am trying to create a webpage where a user inputs a password, and a php reads the user inputed key and then compares it to an array of "acceptable" keys. If the key is real, you move on, if not you get an error. When I try various keys, I have noticed something interesting...

Keys in the following format work:

aaaaaaaaaaaaaaaaaaaaaaaaa
HHHHHHHHHHHHHHHHHHHHHHHHH

Keys like this do not work:

ahahahahahahahhahahahhaha

Basically any key that is not the same case & character the whole way through will not work, and I do not understand why. Here is my php, any help is much appreciated.

<html>
<head>
<title> pC </title>
</head>
<body>
<h1 align="center"> This is a heading in html </h1>
<br>
<?php

	$var_inputPassword = ($_POST["Pword"]);
?>

<?php

$myFile = "passwords.txt";
$fh = fopen($myFile, 'r');
$var_data = fread($fh, filesize($myFile));
fclose($fh);
$var_list = explode("\n",$var_data);
?>
<?php

	if (in_array($var_inputPassword , $var_list, TRUE)) 
	{
		//redirect to survey
		echo ( '<meta http-equiv="REFRESH" content="0;url=WinthropPoll01.html">');
		
	}
	else
	{
		//redirect to index2.html
		echo ( '<meta http-equiv="REFRESH" content="0;url=index2.html">');
		
	}

?>

</body>
</html>
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by atticusr5
0

Change

$var_list = explode("\n",$var_data);

to

$var_list = explode("\r\n",$var_data);
1

Try debugging your code. Make sure you're getting the correct list/array of passwords.

eg:

$var_list = explode("\n",$var_data);
var_dump($var_list);

or:

print_r($var_list);

Zero13 recommended using:

$var_list = explode("\r\n",$var_data);

Instead since Windows line breaks are \r\n instead of just \n in Linux. However, to work with both Windows and Linux you need something like:

$var_list = explode("\n",$var_data);
// trim each value in the data
$var_list = array_map('trim', $var_list);

However, if spaces mean something in each line you need to only trim the \r character.

Just a note, instead of using:

echo ( '<meta http-equiv="REFRESH" content="0;url=WinthropPoll01.html">');

You can use:

header('Location: WinthropPoll01.html');

That sends a HTTP redirect which is followed without displaying any content. However, that only works if you have yet to display any content at all.

To solve that you usually use output buffering to buffer your content (see: ob_start() ) or you can test for output using headers_sent().
http://us2.php.net/manual/en/function.headers-sent.php

eg:

if (!headers_sent()) {
   header('Location: WinthropPoll01.html');
} else {
   echo ( '<meta http-equiv="REFRESH" content="0;url=WinthropPoll01.html">');
}
0

thanks for the help guys, the error was in the datafile with an extra space after each key entry.

also i tried the header function, but i got an error (probably because I have the extra echos) but i will play around with it some more.

thanks again for all the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.