Jump to content


Photo

Unexpected '{' in Session Code!!


  • Please log in to reply
6 replies to this topic

#1 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 14 March 2006 - 01:21 PM

I'm trying a login session...

but the following code is producing an error "Parse error: parse error, unexpected '{' " and its the first one the error is pointing at.

<?php
                    if (isset($_SESSION['username']) {

echo "Welcome";
echo $_SESSION['username'];

}
if (unset($_SESSION['username']) {

echo "Please Sign in";

} 
?>

then there is also the following at the beginning of the page
<?php session_start(); ?>

What have I done wrong?

Thanks

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 01:33 PM

you're missing your closing parenthesis on your if statement:
// change this:
if (isset($_SESSION['username']) {

// to this:
if (isset($_SESSION['username'])) {

btw, you'll get the same error on your second if statement, too.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 14 March 2006 - 01:37 PM

thanks i changed both now it says ...

Parse error: parse error, unexpected T_UNSET

??

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 March 2006 - 01:50 PM

[!--quoteo(post=354873:date=Mar 14 2006, 08:37 AM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ Mar 14 2006, 08:37 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks i changed both now it says ...

Parse error: parse error, unexpected T_UNSET

??
[/quote]

that's because unset() doesn't return any value, and your if statement requires some sort of argument in it. if you're wanting to see if there is a username set and do one or the other, you'd be better off just using an else:
if (isset($_SESSION['username'])) {
  echo "Welcome, $_SESSION[username]!";
} else {
  echo "Please sign in";
}

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 01:54 PM

change this:

if (unset($_SESSION['username']) {

to this

if (!isset($_SESSION['username'])) {



EDIT: ooops same time post again......in which case, the way obsidian suggested is much better for what you're after anyway

cheers
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#6 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 14 March 2006 - 02:02 PM

yeah both worked .....

but now further errors relating to the session at the beginning....

Warning: session_start(): open(/tmp\sess_99a3b92d86e67afe762f29c6b156dfe4, O_RDWR) failed: No such file or directory (2) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

then it displays it correctly ... saying 'Please Sign in'

then further errors....

Warning: Unknown(): open(/tmp\sess_99a3b92d86e67afe762f29c6b156dfe4, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

#7 webwiese

webwiese
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 14 March 2006 - 02:19 PM

[!--quoteo(post=354879:date=Mar 14 2006, 03:02 PM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ Mar 14 2006, 03:02 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
yeah both worked .....

but now further errors relating to the session at the beginning....

Warning: session_start(): open(/tmp\sess_99a3b92d86e67afe762f29c6b156dfe4, O_RDWR) failed: No such file or directory (2) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\LoginTest.php on line 1

then it displays it correctly ... saying 'Please Sign in'

then further errors....

Warning: Unknown(): open(/tmp\sess_99a3b92d86e67afe762f29c6b156dfe4, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
[/quote]


Hi from Germany,

the first error message seems to be php.ini - config problem. you're using windows, the standard php.ini (\tmp) for session dir is no usable under windows. you have do create a directory and add this to your php.ini like

session.save_path = C:\Program Files\Apache Group\Apache2\sessions

The second and third error message due to the first error message, this was an output and session_start() must be set before any screen output.

Hope it will work,
webwiese







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users