-3

i have a program i had written it with PHP 5.2.9. and it is working now for me on my customer server that runs PHP 5.2.9 fine for 3 years ago till now. everything is ok. i am now using PHP 5.4.19 . today when i want to install and use my that old program, this error is appearing to me on every page i have. for example:

 Notice: Undefined index: fullname_sima in C:\xampp\htdocs\sima\SystemAccess.php on line 4

that code line is:

$fullname=$_SESSION['fullname_sima'];

Notice: My program is working Fine and with no Errors NOW on my customer server with PHP 5.3.9 . and it is right for me, if i reinstall PHP 5.2.9 on my localhost, everything comes back to work Fine. i've test it.

Any help will gratitude!

Edit: Notice: I dont want to use "isset()" function! because my program is big and this edit will confuse me!

*PLEASE NOTICE that my program is work well in PHP 5.2.9 and i never used any isset() function in my program. *

Thanks

NGM
  • 89
  • 7
  • 3
    This has nothing to do with version. My bet is that on new environment you've changed error level (via `error_reporting()` for example). Best way is ti check your indexes before using them. – Alma Do Nov 11 '13 at 08:11
  • 1
    nothing to do with version upgrade, just check whether you are using `session_start` and also use `isset()` to check whether the session index exist – Mr. Alien Nov 11 '13 at 08:11
  • 1
    That's not an error, its just a notice. You can ignore it and set notice display to be false. But, that error is due to the fact that at that line there is no value `fullname_sima` in your `$_SESSION` array. You might want to fix that. – Hanky Panky Nov 11 '13 at 08:11
  • My Current PHP.ini settings for error_reporting is: `error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT` and i never had change it. – NGM Nov 11 '13 at 08:14
  • But it's an `E_NOTICE`. Anyway it's no good idea to surpress and ignore errors and warnings cause they lead you earlier but for sure later to problems if you go with them. – TiMESPLiNTER Nov 11 '13 at 08:15

3 Answers3

4

For one you should always check array indexes before actually using them, so replace:

$fullname=$_SESSION['fullname_sima'];

with something like

$fullname = ( isset($_SESSION['fullname_sima']) ? $_SESSION['fullname_sima'] : null );

The fact that you are using a non existing index causes your error. You probably only see that error now since your error level has been changed during the upgrade to 5.4, which makes it show notices now.

To check you default error level, check your php.ini for the error_reporting = ... bits. If it says E_ALL, it will show all errors by default. Change it to E_ALL ^ E_NOTICE to only show errors, but not notices.

You can also do the same with the function error_reporting(E_ALL ^ E_NOTICE);

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
  • Remove semicolon after `$_SESSION['fullname_sima']` and add `)` before `?` – Sal00m Nov 11 '13 at 08:14
  • Notice that my program is work well in PHP 5.2.9 and i never used any `isset()` function in my program. – NGM Nov 11 '13 at 08:18
  • That just means your php configuration did not show E_NOTICE errors. The notice was always there because its not the proper way to do things. Thats why I gave you 2 solutions. The first one is the best one. The second one just hides the notice. – Damien Overeem Nov 11 '13 at 08:25
  • @Damien Overeem . OK. its means i am forcing to Edit all my program (over 300.000 lines) and i have to use `isset()` function in my program to prevent this damn errors. However NOBODY GIVE ME THE CORRECT ANSWER and just i got solutions, and NEVER ANSWER ME THAT WHY PHP5.2.9 HAS NO PROBLEM WITH MY PROGRAM, AND PHP5.4.17 HAS IT? Anyway, I thanks anybody who want to helping me. God Bless You. – NGM Nov 11 '13 at 08:38
  • NO IT DOES NOT. AND STOP TYPING IN CAPS, WE ARE JUST TRYING TO HELP... read the bloody 2nd part of my answer. It states how to disable E_NOTICE warnings, which will also solve your problem WITHOUT altering your code. You seriously need to read more man.. – Damien Overeem Nov 11 '13 at 08:44
  • According to your comments your error_reporting value in your php.ini is `error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT`, which means you are showing all errors except deprecated and strict warnings. Bit you are not hiding E_NOTICES, which is the error you are getting. Just change it to `error_reporting=E_ALL ^ E_NOTICE` as stated in my answer and you'll be all set. – Damien Overeem Nov 11 '13 at 08:46
  • @DamienOvereem Thank you so much. please read comments under `user4035` Answer below. i've test your latest idea and nothing has changed. – NGM Nov 11 '13 at 09:10
  • Open your file `C:\xampp\htdocs\sima\SystemAccess.php` and put `error_reporting(E_ALL ^ E_NOTICE);` on line 3. That error in your example will probably disappear. If so, search your code for "error_reporting" you are most likely overriding the default settings from php.ini somewhere in your code. – Damien Overeem Nov 11 '13 at 09:14
2

Probably, you just had notices suppressed in your previous php installation. It should appear there as well. To remove it, you should correct your code for case of missing key, and add an if statement:

if (isset($_SESSION['fullname_sima']))
{
    $fullname = $_SESSION['fullname_sima'];
}
else
{
    $fullname = null;
}
user4035
  • 22,508
  • 11
  • 59
  • 94
  • Not to bash your answer (instructional only..), but isset is actually much faster then array_key_exists. (see: http://stackoverflow.com/questions/6337893/why-is-array-key-exists-1000x-slower-than-isset-on-referenced-arrays). If you need to check for an index, use isset instead. – Damien Overeem Nov 11 '13 at 08:17
  • Notice that my program is work well in PHP 5.2.9 and i never used any `isset()` function in my program. – NGM Nov 11 '13 at 08:19
  • @NGM So what? Do you want to learn something new or argue with us? – user4035 Nov 11 '13 at 08:21
  • @DamienOvereem Thank you for the useful link. I improved the answer. An idea for a good question: why php has 2 functions, that duplicate each others behaviour? And one is more efficient, then the other. – user4035 Nov 11 '13 at 08:23
  • @ user4035 I'm not argue with anybody. I told you that my program is WORK WELL for me. if not using of `isset()` in program is bad, so why my program is working fine about 3 years till now FINE? – NGM Nov 11 '13 at 08:29
  • @NGM Please, set `error_reporting=E_ALL` for your old php and let me know, whether the notice appears. – user4035 Nov 11 '13 at 08:32
  • @NGM No, no, I meanth in php.ini, remove & ~E_DEPRECATED & ~E_STRICT from error_reporting, and make it just error_reporting=E_ALL – user4035 Nov 11 '13 at 08:43
  • @user4035 i test it. nothing has changed. – NGM Nov 11 '13 at 08:46
  • @NGM Interesting. Probably, your $_SESSION['fullname_sima'] is defined in one case and undefined in the other. Unfortunately, it's impossible to say anything more, without careful debugging. – user4035 Nov 11 '13 at 08:50
  • @user4035 Thank you for your researching. I Opened this Question just for knowing why this error is appear. I know what is `isset()` and i use it in my codes. But my question is just why my old PHP has no problem with my program and it is running well and works fine? anyway, I Thank you so much bro. – NGM Nov 11 '13 at 09:05
0

the problem is not the line that you copied, check the line that is setting a value into the $_SESSION['fullname_sima']

or if it should be null before calling the value you can check it is set or not

  $fullname=null; // or you can set as any other value like : $fullname=0; or $fullname='';
  if (isset($_SESSION['fullname_sima'])){
        $fullname=$_SESSION['fullname_sima'];
  }
Mohammad Hossein Amri
  • 1,842
  • 2
  • 23
  • 43
  • if you haven't used isset before so your error reporting was off so you couldn't see any error,but actually the error was there, anyway please copy and paste the line that set value into the session variable – Mohammad Hossein Amri Nov 11 '13 at 08:26