Page 1 of 1

[SOLVED] isset(), empty() and unset()

Posted: Tue Apr 12, 2005 5:53 am
by steedvlx
I am trying to determine whether a session variable exists or not in my code.

If it exists, I want to increment it. If it doesn't, I want to create it. Sounds simple right?

Code: Select all

if (!isset($attempt_count)) {
	session_register("attempt_count"); //Counter for shutting down a user after too many login attempts.  UNregistered with a successful login
	$attempt_count ++;
}
I have tried isset(), unset(), and empty() enough that I think I understand what they do...So, isset() is the logical choice to see if a variable exists. Right? or Not!!

Every time I test this code, is always re-defines $attempt_count and erases the previous values. It's not supposed to do that is it. I 'thought' that session_register() left a variable alone if it was already defined. Does it in-fact re-initialize it?

I even tried

Code: Select all

if (!isset($_SESSION[attempt_count]))
just in case I was supposed to address it as a $_SESSION variable, which just shows how confused I am by none of this working.

-------------
Also, the $attempt_count ++; does not seem to work on my main server. I didn't have a problem with it on my local test server, but uploading the script to the main server causes it not to function. I have tried all variations located on the PHP online manual. But, nothing explains why it craps out on the web server.


Any ideas on either of these problems?

Thanks,

Posted: Tue Apr 12, 2005 6:57 am
by harsha

Code: Select all

<?
session_start(); //start the session on the server

if(isset($_SESSION['attempt_count'])) //if session is set
  {
    $_SESSION['count']++; //increment it
  }
else if(!isset($_SESSION['attempt_count'])) //if not set
  {
    $_SESSION['attempt_count'] = 0; //set it to zero
  }
else //if set but it is empty
  {
    $_SESSION['attempt_count'] = 0; //reset it to zero

  }
?>
1. $_SESSION[attempt_count] works on older PHP versions

2. Recommended usage is $_SESSION['attempt_count'] or $_SESSION["attempt_count"]

3. when you use session_register(); don;t use $_SESSION and the reverse is also true.

thank you

Posted: Tue Apr 12, 2005 10:01 am
by steedvlx
harsha,

Thanks for that. Evidently my PHP (4.3.0) is a bit too current for that example.

But, I did finally get it work. Although I swear I tried this before, it works fine now.

Code: Select all

if (!isset($attempt_count)) {
	session_register("attempt_count");
}
The incrementing is handled at the end of the script. But, I still can't figure out why $attempt_count++ or ++$attempt_count and even $attempt_count += will not work on my production server. I literally have to write

Code: Select all

$attempt_count = $attempt_count + 1;
in order for it to work. :?

Regards,