Click here to Skip to main content
15,560,678 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php


if(isset($_POST['send']))
  {
  if (isset($_SESSION['username']))
  $fp = fopen('messages.txt', 'a',1);
  $message=htmlspecialchars($_POST['message-text']);
  } elseif (empty($message)) {
    header('Location: stop_talking.html')
  } else { // this line gives error (Syntax error, unexpected '}'
    fwrite($fp, "$_SESSION[username]: $message ".PHP_EOL);
    echo "<script>if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
  }
  document.location.reload()</script>";
  }
}
?>


What I have tried:

I have tried searching for an solution and but I didn't find I tried everything that came to my mind
Posted
Updated 5-Feb-22 0:23am
Comments
Richard MacCutchan 5-Feb-22 4:37am    
Maybe the semi-colon is missing from the previous statement.
Patrice T 5-Feb-22 5:07am    
And the error message is ...
Member 15524347 5-Feb-22 5:09am    
Syntax error, unexpected '}'
Patrice T 5-Feb-22 5:17am    
And it told you the line number ?
Which line in this code ?
Richard MacCutchan 5-Feb-22 6:46am    
Add the semi-colon on the line before the else clause. And remove the extra closing brace as mentioned by Patrice T below.

1 solution

Quote:
Syntax error, unexpected '}'

A little beautification show the problem
PHP
<?php

if (isset($_POST['send']))
{
    if (isset($_SESSION['username'])) $fp = fopen('messages.txt', 'a', 1);
    $message = htmlspecialchars($_POST['message-text']);
}
elseif (empty($message))
{
    header('Location: stop_talking.html')
}
else
{ // this line gives error (Syntax error, unexpected '}'
    fwrite($fp, "$_SESSION[username]: $message " . PHP_EOL);
    echo "<script>if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
  }
  document.location.reload()</script>";
}
} // problem is here, remove the '}', or 1 is missing somewhere else
?>


Online PHP Beautifier - PHP Formatter - BeautifyTools.com[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900