Parse error with my loginsystem

Hey guys!

I am trying to do a login system based on this video:

Everything was going well but I am trying to test my signup page and I am getting the following errors:

Parse error: syntax error, unexpected ‘else’ (T_ELSE), expecting end of file in C:\xampp\htdocs\loginsystem\includes\signup2.php on line 51

I have gone through my codes a few times but can’t find any errors…

<?php
   
   if (!isset($_POST['submit'])) {
      header("Location: ../signup.php");
      exit();
   } else {
   	   include_once 'dbh.php';

   	   $first = mysqli_real_escape_string($conn, $_POST['first']);
   	   $last = mysqli_real_escape_string($conn, $_POST['last']);
   	   $email =mysqli_real_escape_string($conn, $_POST['email']) ;
   	   $uid = mysqli_real_escape_string($conn, $_POST['uid']);
   	   $password = mysqli_real_escape_string($conn, $_POST['password']);

   	   //Error handlers...

   	   if (empty($first) || empty($last) || empty($email || empty($uid)) || empty($password)) {
   	   	   header("Location: ../signup.php?signup=empty");
   	   	   exit();
   	   } else {
          //Check if input characters are valid
   	   	    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
                header("Location: ../signup.php?signup=invalid");
                exit();
   	   	    } else {
   	   	  	   //Checking for valid emails
                 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     header("Location: ../signup.php?signup=email");
                     exit();
                 } else {
                      $sql = "SELECT * FROM users WHERE user_uid='$uid'";
                      $result = mysqli_query($conn, $sql);
                      $resultCheck = mysqli_num_rows($result);

                      if($resultCheck > 0) {
                         header("Location: ../signup.php?signup=usertaken");
                         exit();
                      } else {
                      	//Hashing the password
                      	$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                      	//Insert the user into the database
                      	$sql ="INSERT INTO users (user_first, user_last, user_email, user_uid, user_password) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";

                      	mysqli_query($conn, $sql);
                      	header("Location: ../signup.php?signup=success");
                      	exit();
                      }
                 }
   	   	  }
   	   }
   	 } else {
   	 	 header("Location: ../signup.php");
   	 	 exit();
   	 }

Thanks!

In your “Error handlers section” you have a missing ) … empty($email

Often when you get an ‘unexpected …’ type of error that means that in the lines just before the line stated in the error message, there is a missing bracket, parentheses, brace or semi-colon.

Thanks! I have fixed that problem but I am still getting the same message… about line 51…

I have also used some php checker and they all came back with:

PHP Syntax Check: Parse error: syntax error, unexpected ‘else’ (T_ELSE) in your code on line 51
} else {

Not sure what this means…

Your code is full of errors bud. Your else statements don’t match your if statements

Try this code and see what happens

<?php
    if (!isset($_POST['submit'])) {
       header("Location: ../signup.php");
       exit();
    } else {
        include_once 'dbh.php';

        $first = mysqli_real_escape_string($conn, $_POST['first']);
        $last = mysqli_real_escape_string($conn, $_POST['last']);
        $email =mysqli_real_escape_string($conn, $_POST['email']) ;
        $uid = mysqli_real_escape_string($conn, $_POST['uid']);
        $password = mysqli_real_escape_string($conn, $_POST['password']);

        if (empty($first) || empty($last) || empty($email || empty($uid)) || empty($password)) {
            header("Location: ../signup.php?signup=empty");
            exit();
        } else {
            if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
                header("Location: ../signup.php?signup=invalid");
                exit();
            } else {
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    header("Location: ../signup.php?signup=email");
                    exit();
                } else {
                    $sql = "SELECT * FROM users WHERE user_uid='$uid'";
                    $result = mysqli_query($conn, $sql);
                    $resultCheck = mysqli_num_rows($result);

                    if ($resultCheck > 0) {
                       header("Location: ../signup.php?signup=usertaken");
                       exit();
                    } else {
                        //Hashing the password
                        $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                        //Insert the user into the database
                        $sql ="INSERT INTO users (user_first, user_last, user_email, user_uid, user_password) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";

                        mysqli_query($conn, $sql);
                        header("Location: ../signup.php?signup=success");
                        exit();
                    }
                }
            }
        }
    }
?>

I got it to work! I repeated the last if statement twice…

When you follow a tutorial video, make sure you are not just blindly typing the code that is provided, but that you really understand every line of it and how it relates to the rest of the code, and what its purpose is. That way you are less likely to end up in a situation like this where you repeated an if-statement.

2 Likes

I am trying to do an email activation now and I have another file called activate.php but no information is passed on from the form action. I tried to do something liked this:


<?php



include_once 'dbh.php';

$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$token = mysqli_real_escape_string($conn, $_POST['token']);

$sql = "SELECT * FROM users WHERE user_email='$uid' OR user_token='$token';";

$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
	while ($row = mysqli_fetch_assoc($result))
		echo $row['user_token'];
}

but I got the following errors:

Notice: Undefined index: uid in C:\xampp\htdocs\loginsystem\includes\activate.php on line 7

Notice: Undefined index: token in C:\xampp\htdocs\loginsystem\includes\activate.php on line 8

The page is being called from another page with a form and the $uid input variable has not been selected and posted to the activate.php page. It is best to validate the post parameter instead of assuming the post variable has been passed.

I am still confused… I understand that the information has not been passed but does that mean that I can’t use $_POST? How would I do an email activation to set 1?

Also, I am also trying to add some extra line of codes here:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     header("Location: ../signup.php?signup=email");
                     exit();
                 } else {
                   if (Strlen($password <= 5)) {
                   header("Location: ../signup.php?signup=invvalidlength");
                   exit();
                   }else {
                 	
                      $sql = "SELECT * FROM users WHERE user_uid='$uid'";
                      $result = mysqli_query($conn, $sql);
                      $resultCheck = mysqli_num_rows($result);

                      if($resultCheck > 0) {
                         header("Location: ../signup.php?signup=usertaken");
                         exit();
                      } else {
                         // Supply a random generated token for email activation

but I am always getting invalid length…

Remember what I said previously - if you get an 'unexpected … ’ error message, it means that there is either a missing closed bracket or brace, or a missing semi-colon . In other words, there is an unfinished statement before the else. So look at this if statement and see if you can see what is missing.

I had the wrong syntax… it is working now…but I still can’t figure out how to pass the information on to my activation.php…

Are you testing for POST but passing as GET?

i.e. the “?name=foo” would be in the GET array
if ($_GET['name'] === 'foo') { // true
not in the POST array
if ($_POST['name'] === 'foo') { // false

I already know what’s wrong with his code. He’s got 2 else statements which is what is causing the error.

1 Like

I found out the program… it was the way i put the {} but should i use preg_match and substr to check if the first character of a password is uppercase?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.