2

Apologizes for such basic question, but I've gone mad debuggind the following code:

$fname = 'results.txt';

$handle = fopen($fname,"a+");
if ($handle){
    $cnt = file_get_contents('./results.txt');
    $pos = strpos($cnt,":");
    if ($pos === 'false'){
        $str = htmlspecialchars($_COOKIE['username']).": ".$_COOKIE['score'];
        fwrite($handle,$str);
        }
    if ($cnt) echo $cnt;
    else echo 'Error in file_get_contents!<br />';
}
else echo '<span>Error while opening file</span>';

$cnt returns false, whatever I do. I've tried to change the argument to 'results.txt', full url to file - still no progress. I looked up the function on php.net, and from what I see, syntax is correct.

Thanks for your time.

P.S. Code itself is not nice (f.e. regular expressions would suit better for this task), as I wrote it in haste, but I want to get it running before rewriting.

Arnthor
  • 2,563
  • 6
  • 34
  • 54
  • 1
    Is error reporting turned on? Try `error_reporting(E_ALL);` at the top of the file – Pekka Jan 20 '11 at 00:41
  • Don't put html-escaped data in databases or database-like things (like text files). You can do that when outputting stuff. Otherwise you'll have a problem if you need unescaped data at some point (e.g. for plaintext emails). – ThiefMaster Jan 20 '11 at 00:43

1 Answers1

5

strpos returns boolean false, not the string value of 'false'. Try this:

if ($pos === false) {
Ben Rowe
  • 28,406
  • 6
  • 55
  • 75