Forum Moderators: coopster

Message Too Old, No Replies

Errors With Variables

         

supermanjnk

9:05 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



Notice: Undefined index: loggedin in D:\site\config.php on line 4

I recently upgraded to php5, And I will be the first to admit that my coding isn't the neatest, and I don't always follow the correct syntax, However in php 5 there seems to be now room for mistake in syntax

Currently I have some session variables that are filled in by post data, when the post data hasn't been sent i get this error. I'm assuming it's because when the post hasn't been sent then the ending variable $login is equal to being empty, or undefined. I can't seem to find a way around this to keep it from being undefined. I know I could turn error reporting off, but that would just be sloppy.

Whats the best way to deal with this:

<?
session_start();
$server = $_SERVER['HTTP_HOST'];
$_SESSION['loggedin'] = $_POST['login'];
$loggedin = $_SESSION['loggedin'];
?>

added, I hope you don't mind that I put this here instead of making a new thread, don't want to spam the forum.

I'm using str replace to switch

 with <div class="code"> and 
with </div> However doing it this way allows for them to not have the closing div and screw up the site layout by doing so, whats the best way to check to make sure that both tags are there? If both aren't there I want it to show as
 instead of creating the div, Doing a replace [code]%
doesn't work I guess because the % represents one character Any help would be great.

dreamcatcher

9:14 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Try using isset()

For example:

if (isset($_POST['loggedin']))
{
$loggedin = $_SESSION['loggedin'];
}

supermanjnk

9:21 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



I was hoping I wouldn't have to do that, It seems like such a waste, even though it's only 7 extra characters, It will probably increase the page size quite a bit with how many variables I have set by sessions and post data...

dreamcatcher

9:36 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, quick fire way to do it is to switch your error reporting off.

error_reporting(0);

Not a good way of writing efficient code, but a quick fix.

ironik

9:37 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



Just try testing for one to check whether the form has been submitted. If one variable has been submitted, chances are the rest have so the $_POST variables are safe to use:

if (isset($_POST['login']))
{
// Form has been submitted safe to use $_POST super global
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
} else {
// Form hasn't been submitted, use code not related to processing data from the $_POST super global
}

You won't need to test for the existance of each $_POST variable ... just for one from the form that you know you are expecting. It's always good practice though, to test for the existance of something before you use it... saves heartache later.

ergophobe

10:40 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




It seems like such a waste

Au contraire. What's a waste is code that does not function properly because you have a small error that you haven't noticed because you've turned off error reoporting for notices.

I was going through some code not too long ago that I was trying to adapt for my purposes and the author had things like

$var['index1']
$var['indxe1']

Because he wasn't developing with error_reporting set to show notices, he didn't realize he had made a typo and that

if ($var['indxe1'])

would always and under all circumstances evaluate to false. His condition would never be met.

Check all vars. Assign default values. It will save you far more time in the long run than you would save by not declaring default values and checking for undefined variables and just getting by with turning notices off.

jatar_k

2:25 am on Mar 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> It will probably increase the page size quite a bit

that depends on what you mean by page size

script size, yes it will increase but as ergophobe mentioned it is more than worth it

html page size, no, it won't increase at all, this only happens on the server so it is never sent to the browser.

supermanjnk

2:10 pm on Mar 2, 2005 (gmt 0)

10+ Year Member



well my biggest problem is with a form that the user might not completly fill out (in other words there are areas of the form that are not required, what should I do with that? I guess i'll have to use an if (isset($_POST['whatever'])) for each individual one right? And turning off error reporting isn't an option becasue I like to have error free code.

dcrombie

4:05 pm on Mar 2, 2005 (gmt 0)



The form values will be 'set' even if they have no value (with the exception of unchecked checkboxes, radio buttons and select lists ;)).