0

I have a PHP register form and every time I enter a name and password into the form it says "unexpected $end on line 52". The code is below for the index:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<form action="handler.php" method="post">
<h1>Register</h1>
<input type="text" name="username" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="submit" />
</form>

</body>
</html>

handler:

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$counter = 0;

class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('db/test.db');
      }
   }
   $db = new MyDB();

$sql =<<<EOF
    SELECT username from users WHERE username='$username';
EOF;

$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
    $counter++;
}

if($counter >= 1){
    echo "user already exists. click <a href='index.php'>here </a> to go back";
}

if($counter = 0){
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    $sql2 =<<<EOF
    INSERT INTO users(ID, username, password) VALUES (null, '$username', '$password');
    EOF;
    header('Location: index.php');
}

?>

</body>
</html>
laurent
  • 88,262
  • 77
  • 290
  • 428
andrew
  • 39
  • 9

1 Answers1

0

As the documentation mentions, heredoc closing tags cannot be indented, or in fact cannot contain any other characters.

So your second EOF should be like this:

if($counter == 0){ //Need to validate if the counter is 0
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    $sql2 =<<<EOF
    INSERT INTO users(ID, username, password) VALUES (null, '$username', '$password');
EOF;
    header('Location: index.php');
}

(Also note the $counter == 0 change as suggested by jqheart)

(In fact, for this particular case I think it would be better to use a regular string as it would make the code cleaner)

laurent
  • 88,262
  • 77
  • 290
  • 428