Parse error: syntax error, unexpected {[closed]

3

I have a PHP file to check if SESSION is set:

<?php

session_start();
if(isset($_SESSION['user']){
   echo "<script>document.location.href='../areaprivada.html';</script>";
   }
 else{
    echo "<script>document.location.href='login.php';</script>";*/
}
?>

But when I run it gives this error:

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\exercicios\saber2\php\session.php on line 4

PS: Line 4 is the one that closes if .

    
asked by anonymous 09.07.2015 / 18:38

3 answers

5

You closed the () brackets and removed the * / closing comment tag

<?php
session_start();
if(isset($_SESSION['user'])){
   echo "<script>document.location.href='../areaprivada.html';</script>";
} else {
    echo "<script>document.location.href='login.php';</script>";
}
    
09.07.2015 / 18:47
4

You already have the answer to your problem, but since no one has explained how to read the error, here is my contribution so that in the future you can understand what PHP is saying:

  

Parse error: syntax error, unexpected '{' in C: \ xampp \ htdocs \ exercises \ saber2 \ php \ session.php on line 4

The error is divided into 3 parts:

  • Type of error:

    You get Parse error , from the E_PARSE family, which are basically errors that will prevent execution from continuing.

  • Error:

    You get syntax error which indicates that something is wrong with the PHP code whose interpreter can not understand.

  • Description:

    The description of your error identifies the problem, the line and the file where the error is located.

    In your case, unexpected '{' in line 4 of file C:\xampp\htdocs\exercicios\saber2\php\session.php , that is, a { was found in line 4 of said file, when something else was expected.

Since the error points to an unexpected character, the actual problem is before it:

#01 <?php
#02
#03 session_start();
#04 if(isset($_SESSION['user']){
#05   echo "<script>document.location.href='../areaprivada.html';</script>";
#06   }
#07 else{
#08    echo "<script>document.location.href='login.php';</script>";*/
#09 }
#10 ?>

And so, on your line # 4, before { should be something else. Analyzing from the beginning of the line up to { we finally realize that we have to close the parentheses of the if condition with a ) .

More about errors and their types:

10.07.2015 / 16:56
2

This should be done by closing the else key and if :

<?php
session_start();
if(isset($_SESSION['user'])){
   echo "<script>document.location.href='../areaprivada.html';</script>";
} else {
    echo "<script>document.location.href='login.php';</script>";
}
?>
    
09.07.2015 / 18:59