Jump to content

[SOLVED] Why is this mini-session script not working???


Recommended Posts

Hi all,

 

I am new to PHP and new to this forum but was wondering if anyone could give me some input on why this script is not working correctly. 

 

<?php
session_start();
if (isset($_SESSION['record_number'])) {
  $_SESSION['record_number'] = 0;
}
?> 

 

The error I am getting is this...

 

Parse error: syntax error, unexpected '{' in /home/carlos/web/my-program/test.php on line 3

 

I have beat my proverbial head against the wall on this one.  I just don't get it. 

 

Anybody got any suggestions? 

 

Thanks. 

 

Carlos

 

 

Yeah...I tried echo'ing stuff all over the place to no avail. 

 

When I include the line you suggested the error just moves down to be on line 4 instead of line 3 :).  And nothing gets echo'ed to the screen (other than the error of course). 

 

Carlos

Hi ?,

 

Thanks again for your input.  Your code snippet worked as expected with no errors.  It output "nothing to set" to the screen. 

 

As for where my $_SESSION['record_number'] variable comes from...it doesn't come from anywhere.  The code snippet that I originally posted was just a snippet on my way to producing something larger.  I had narrowed it down enough to where it might have hopefully been easier to figure out but the snippet itself doesn't make any sense by itself. 

 

Do you or anyone else have any ideas as to why the original code snippet that I posted does not work and produces an error?  I am new to PHP and like the simplicity of it but I am also coming to realize that PHP behaves odly sometimes and in a way that makes it rather difficult to work with sometimes since some things that are supposed to work don't quite work the way they are supposed to...in some cases...though most times they work as they are supposed to...which overall makes PHP seem like a quirky, temporamental language.  Just a newbies impression but the code snippet I posted is a case in point.  There is no logical reason why it should produce an error...that I can see...but it does. 

 

Carlos

<?php
session_start();
if (!isset($_SESSION['record_number'])) { // added !
  $_SESSION['record_number'] = 0;
}
?>

 

note I added the exclamation mark before isset, which now means, instead of it checking IF the variable has been set, it will check if it HAS NOT been set. Then if it hasn't it will set it. This may solve the problem, or it may not. Anything is worth a try.

 

You could also try:

<?php
session_start();
if (!isset($_SESSION['record_number'])) { 
  die( "session variable is not set" );
} else {
echo "session variable is set";
}
?>

 

and see what you get.

 

Regards ACE

Thanks for your additional input ACE.  It helped me track the problem down!

 

I think I finally tracked the problem down but it's not what I expected.  In this case it's not PHP's fault :)

 

I originally copied some session handling code from a web page somewhere and inserted the copy into my code.  It then didn't work, producing the error I mentioned, and I then proceeded to isolate the code down to what was essentially a copy of the copy I made. 

 

Well it turns out that there is some invisible character that I copied with the code that is causing the problem. 

 

I copied the code I posted in my original post on this thread into a test4.php file and ran it just fine.  I then copied the section of code that I originally copied into a test3.php and ran it and reproduced the error which I started this thread with.  The contents, at least to the visible eye are identical.  The same text.  But when I run a diff utility on the two files there is a difference.  I don't have a hex editor handy but somewhere the two files differ and it is the character difference that is causing the problem. 

 

I don't know if that means I should never again copy code :) (a real pain)...I'll have to think about this to see if there is some way to prevent having to track an invisible character down when I copy code I find on the web amidst possible syntax errors and the like. 

 

I'm going to set this thread to SOLVED unless someone wants to discuss this further. 

 

Thanks again. 

 

Carlos

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.