-1

Can somebody help me and explain me why I get this error with the following code, please:

<html>
    <head>
    </head>
    <body>
        <?php
            if (!(isset($_COOKIE["loggedin"]))){
                ?>
                <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="name_form">
                    Username <input type="text" name="username">
                    <br/>
                    Password <input type="text" name="password">
                    <br/>
                    Remember Me <input type ="checkbox" name="remember_me" value="1">
                    <br/>
                    <input type="submit" name="submit" value="Log in">
                </form>
            }
    </body>
</html>

<?php

        if(preg_match("/<|>/", $_POST["username"])){    
            echo "do not log in";
        } 
        else if(preg_match("/<|>/", $_POST["password"])){   
            echo "do not log in";
        }
        else{
            //Open/create passwords.txt
            $passwordsFile = fopen("passwords.txt", "a");
            //write users username and password to passwords.txt
            $text_written = fwrite($passwordsFile, $_POST["username"] . "," . $_POST["password"] . "\r\n");
            fclose($passwordsFile);
            setcookie("loggedin", $_POST['username']);
            setcookie("loggedintime", time());
        }
    ?>

Thanks in advance for the help! I've seen similar posts around here but I wasn't able to solve this anyway...

Fodrigoal
  • 79
  • 6

4 Answers4

2

It seems you have an extra curly bracket at the end just before the closing PHP tag ?> and you forgot to wrap the previous end curly bracket } of the IF-statement: if (!(isset($_COOKIE["loggedin"]))){ with PHP closing and ending tags, correct code:

<html>
  <head>
  </head>
  <body>
    <?php if (!(isset($_COOKIE["loggedin"]))){ ?>
      <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="name_form">
          Username <input type="text" name="username">
          <br/>
          Password <input type="text" name="password">
          <br/>
          Remember Me <input type ="checkbox" name="remember_me" value="1">
          <br/>
          <input type="submit" name="submit" value="Log in">
      </form>
    <?php } ?>
  </body>
</html>
<?php
  if(preg_match("/<|>/", $_POST["username"])){
    echo "do not log in";
  }
  else if(preg_match("/<|>/", $_POST["password"])){
    echo "do not log in";
  }
  else{
    //Open/create passwords.txt
    $passwordsFile = fopen("passwords.txt", "a");
    //write users username and password to passwords.txt
    $text_written = fwrite($passwordsFile, $_POST["username"] . "," . $_POST["password"] . "\r\n");
    fclose($passwordsFile);
    setcookie("loggedin", $_POST['username']);
    setcookie("loggedintime", time());
  }
?>
Francis Laclé
  • 384
  • 2
  • 6
  • 22
1

complete the if brace in php block..

<html>
<head>
</head>
<body>
    <?php
        if (!(isset($_COOKIE["loggedin"]))){
            ?>
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="name_form">
                Username <input type="text" name="username">
                <br/>
                Password <input type="text" name="password">
                <br/>
                Remember Me <input type ="checkbox" name="remember_me" value="1">
                <br/>
                <input type="submit" name="submit" value="Log in">
            </form>
       <?php  } ?>
</body>

Amit Chauhan
  • 682
  • 7
  • 22
1

I have made few changes in your code, The close bracket for first if condition is not with in the php tag(<?php ... ?> ). Try to give the password.txt file read write permission

<html>
    <head>
    </head>
    <body>
        <?php
        if (!isset($_COOKIE["loggedin"])) {
            ?>
            <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" name="name_form">
                Username <input type="text" name="username">
                <br/>
                Password <input type="text" name="password">
                <br/>
                Remember Me <input type ="checkbox" name="remember_me" value="1">
                <br/>
                <input type="submit" name="submit" value="Log in">
            </form>
        <?php } ?>
    </body>
</html>

<?php
if (!empty($_POST['submit'])) {
    if (preg_match("/<|>/", $_POST["username"])) {
        echo "do not log in";
    } else if (preg_match("/<|>/", $_POST["password"])) {
        echo "do not log in";
    } else {
        //Open/create passwords.txt
        $passwordsFile = fopen("passwords.txt", "a");
        //write users username and password to passwords.txt
        $text_written = fwrite($passwordsFile, $_POST["username"] . "," . $_POST["password"] . "\r\n");
        fclose($passwordsFile);
        setcookie("loggedin", $_POST['username']);
        setcookie("loggedintime", time());
    }
}
?>
Karthik N
  • 921
  • 8
  • 16
1

Try

 if (!isset($_COOKIE["loggedin"])){

Instead of

 if (!(isset($_COOKIE["loggedin"]))){