Solved

Getting rid of notices.

Posted on 2008-09-29
6
420 Views
Last Modified: 2012-05-05
I am getting tons of notice errors on my site and would like to get rid of them ofcourse i can do that by setting it so it wont display them but would be better if they were prevented in the first place.

most all of them seem to have the same indication

Notice: Undefined index: usertype in /srv/www/htdocs/agents/public_html/index.php on line 17
 undefined index and always in the session.
Is there a way to set it so it has atleast an empty value in the session so the index atleast exists eventhough its empty ??
sorta like to catch the notice ?



<?php

session_start();
 

include('includes/includes.inc.php');

include('includes/globalvars.php');
 

include('header.inc');
 
 

if (isset($_GET['store'])) {

$storeid = $_GET['store'];

} else {

$storeid = $_SESSION['paywaveid'];
 
 

// line 17 below .. 

if ($_SESSION['usertype'] == 'Employees') {

	$storeid = "";

}
 

}

Open in new window

0
Comment
Question by:mdeek
6 Comments
 
LVL 3

Expert Comment

by:R_Janssen
ID: 22597337
Choose your style below :-)
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors
error_reporting(E_ALL);
 
 
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22597339
You can accomplish this by using isset() to determine if the index is set before attempting to access it:

if (isset($_SESSION['usertype']) &&$_SESSION['usertype'] == 'Employees')
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22597347
A typo: There should be a space after the '&&'.
if (isset($_SESSION['usertype']) &&$_SESSION['usertype'] == 'Employees')

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:trickyidiot
ID: 22597680
better than using isset(), use empty() - this checks for non-existant variables, emtpy values, null values, or values of 0. All of these are technically 'empty'

if (!empty($_SESSION['usertype']) && $_SESSION['usertype'] == 'Employees'){
  // do something here
}
0
 

Author Comment

by:mdeek
ID: 22606805
ok but suppose the index doesnt exist so it has not been set , is there a way to give it an empty value ? or a default value so that it does exists and therefor eliminating the notice "undefined index" because then the index exists right ?

for instance array gets pulled index usertype is empty so doesnt exist set the usertype to lets say 1 and later on when needed change that index to something else ?

im just looking for a way to get rid of the notices without resorting to just hiding them with the php.ini configuration.
0
 
LVL 6

Accepted Solution

by:
trickyidiot earned 250 total points
ID: 22607571
to get rid of the notices, check for the existence of a var when you're checking the value.

line 17:
if (!empty($_SESSION['usertype']) && $_SESSION['usertype'] == 'Employees') {

Doing this will result in NO warnings in your logs.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Keep getting 503 on Curl request 6 30
Please show me easiest post form. 3 31
PHP Undefined Index in HTML Form? 2 31
php call to a non-object 3 32
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now