-7

I' am tryin to make a registration and login system for my website and I am getting the parse error:syntax error unexpected 'else'(T_ELSE) in the login file when I click on login what could be the problem? Below is the file with the code:

    <?php
//include config
require_once('includes/config.php');

//check if already logged in move to home page
if($user->is_logged_in()){ 
    header('Location: index.php'); 
}
}
else
{

echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
}   

//process login form if submitted
if(isset($_POST['submit'])){

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

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;
        header('Location: memberpage.php');
        exit;

    } else {
        $error[] = 'Wrong username or password or your account has not been activated.';
    }

}//end if submit

//define page title
$title = 'Login';
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
gugie1
  • 11
  • 1
  • 2

8 Answers8

3

Two curly brackets are closed } after header('Location: index.php');. Make it one and try.

AnkiiG
  • 3,468
  • 1
  • 17
  • 28
  • okay thanks i have fixed the extra brackets but I am still getting an error when I hit the login button to the site and I am getting "Notice: Undefined variable: user in C:\xampp3\htdocs\wb_html\login.php on line 6 Fatal error: Call to a member function is_logged_in() on null in C:\xampp3\htdocs\wb_html\login.php on line 6" in the same file and I dont know where I am going wrong as I am also a begginner in php and I would like my login system to work – gugie1 Feb 11 '16 at 09:20
3

remove } after else :

if($user->is_logged_in()){ 
    header('Location: index.php'); 
} else {
    echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
}
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
0

You have one extra curly brace.

Change

//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php'); 

}
  }

To

//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php'); 

}
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • okay thanks i have fixed the extra brackets but I am still getting an error when I hit the login button to the site and I am getting "Notice: Undefined variable: user in C:\xampp3\htdocs\wb_html\login.php on line 6 Fatal error: Call to a member function is_logged_in() on null in C:\xampp3\htdocs\wb_html\login.php on line 6" in the same file and I dont know where I am going wrong as I am also a begginner in php and I would like my login system to work – gugie1 Feb 11 '16 at 09:19
0

You have a double closed bracket before your first else statement; take one out:

if($user->is_logged_in()){ header('Location: index.php'); 

 }
   //} <- this is the extra one that shouldnt be there
  else
{
0

this is correct code, u Two curly brackets are closed }

//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php'); 

  }
  else
  {

echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
}**strong text**   

//process login form if submitted
if(isset($_POST['submit'])){

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

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;
        header('Location: memberpage.php');
        exit;

    } else {
        $error[] = 'Wrong username or password or your account has not been activated.';
    }

}//end if submit

//define page title
$title = 'Login';
rdn87
  • 739
  • 5
  • 18
0

Format your code well, you had double curly brackets in your code.

//include config
require_once('includes/config.php');

//check if already logged in move to home page
if($user->is_logged_in()){

    header('Location: index.php');

} else {

      echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
  }   

//process login form if submitted
if(isset($_POST['submit'])){

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

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;
        header('Location: memberpage.php');
        exit;

    } else {
        $error[] = 'Wrong username or password or your account has not been activated.';
    }

}//end if submit

//define page title
$title = 'Login';
ArrowHead
  • 609
  • 5
  • 16
0

You have added extra } in if loop. Please replace below code.

//include config
require_once('includes/config.php');

//check if already logged in move to home page
if($user->is_logged_in()){ 
   header('Location: index.php'); 
}
else
{
   echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
} 
devpro
  • 16,184
  • 3
  • 27
  • 38
Jalpa
  • 697
  • 3
  • 13
0

You have an extra closing bracket here:

if($user->is_logged_in()){ header('Location: index.php'); 

}
  }
....

This should be like:

if($user->is_logged_in())
{ 
    header('Location: index.php'); 
}
else
{
    echo "<a href=register.php>Register</a>  <a href=login.php>Login</a>";
} 

Side Note:

I suggest you to always use proper formatting this will help you to find out these kind of issues.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • okay thanks i have fixed the extra brackets but I am still getting an error when I hit the login button to the site and I am getting "Notice: Undefined variable: user in C:\xampp3\htdocs\wb_html\login.php on line 6 Fatal error: Call to a member function is_logged_in() on null in C:\xampp3\htdocs\wb_html\login.php on line 6" in the same file and I dont know where I am going wrong as I am also a begginner in php and I would like my login system to work – gugie1 Feb 11 '16 at 09:18
  • @gugie1: for ist issue, u dont have $user variable in your code.. for second maybe this function is not avaialbe is_logged_in(), , please show me full code. – devpro Feb 11 '16 at 10:09
  • @gugie1: just tell me, where u define is_logged_in() function ??? in which file> – devpro Feb 11 '16 at 10:10