-3

I have some PHP code that takes a post from the index and then allows you to log in. When i run it it say:

parse error: unexpected $end;

If anyone has any ideas please let me know ASAP because need it for a class in school.

<?php

    $uname = $_POST["username"];

    $resultCount = 0;

    class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('users.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   }

    $sql =<<<EOF
    SELECT $uname from users;
    EOF;

    $ret = $db->exec($sql);

    foreach($ret as $uname){
            $resultCount++;
    }

    if($resultCount > 1){
        echo "failed to log in!";
        echo "please return <a href='index.php'>home</a>";
    }
    else{
        echo "logged in succesfully!";
        echo "welcome" . $_POST[username];
    }
?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
andrew
  • 39
  • 9
  • 2
    `$_POST[username]` (2nd to last line) should be `$_POST["username"]`. There might be more syntax errors. – Halcyon Jun 05 '15 at 12:33
  • @Halcyon that is not a syntax error... – Alex Jun 05 '15 at 12:35
  • halcyon i did this and i then said unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING – andrew Jun 05 '15 at 12:35
  • Use `$sql = "SELECT $uname from users";` instead. ATTENTION! Smells like SQL injection issue... – Dietmar Jun 05 '15 at 12:36
  • @andrew does it give you a line number? – Halcyon Jun 05 '15 at 12:37
  • 1
    `SELECT $uname from users;` this is going to bite you. You probably want to select a column for a specific user and not a specific column. And the heredoc `EOF;` cannot have any spaces before it, it has to be at the beginning of the line. – jeroen Jun 05 '15 at 12:38

2 Answers2

3

There might be a problem here:

   $sql =<<<EOF
   SELECT $uname from users;
   EOF;

EOF; should be at the start of the line, there can be no whitespace before it.

See: https://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

That means especially that the identifier may not be indented


$_POST[username] (2nd to last line) should be $_POST["username"]. This might just be a warning though.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

change the brackets the other way and plus u dont have to do that just add to the top of your page this code

<? session_start(); ?>

and when you echo there username put

<?=$_SESSION['sess_user'];?> or <? echo $_SESSION['sess_user'];?>

or you could go into detail and put <? $rot = $username ?> <? echo $rot ?>

viewers.gq
  • 21
  • 8
  • the $_POST method OFTEN carry's loads of errors. so i advise my advice! try it and let me know how it works. – viewers.gq Jun 05 '15 at 12:43
  • And how and where would you assign a value to `$_SESSION['sess_user']` if *the $_POST method OFTEN carry's loads of errors*? – jeroen Jun 05 '15 at 12:49
  • that's a ridiculous question, here is the answer pal. $_POST method is automatic, $_SESSION is manual, therefor session works on its on as long as you have an actual SQL database, and as long as you have started the session, if using post then everything must be typed perfectly.. tens of pointless sentences of code. – viewers.gq Jun 05 '15 at 13:05
  • and also at the same place as the $_POST would be. of course. – viewers.gq Jun 05 '15 at 13:06
  • there isnt any value to an echo.. wtf r u talking about bro. do u even know how to code? – viewers.gq Jun 05 '15 at 13:08
  • LOL reading your comment, and seeing it correctly. has me laughing my ass off! – viewers.gq Jun 05 '15 at 13:08