/    Sign up×
Community /Pin to ProfileBookmark

Undefined index: problem how to fix it??

how to fix error “Undefined index: problem” in this code??

<html>
<head>
<title>Login page</title>
</head>
<body bgcolor=”aqua”>
<h1> Login page </h1>
<?php
$problem = $_GET[‘problem’];
$errormsg =”<font color=’red’> ERROR : “;
if ($problem == “invalidUser”) {$errormsg = $errormsg . ” invalid user!!”;}
if ($problem == “invalidPassword”) {$errormsg = $errormsg . ” invalid password!!”; }
if ($problem == “notLoggedIn”) {$errormsg = $errormsg . “you are not logged in”;}
$errormsg = $errormsg. “</font>”;
if ($problem !=””) {print($errormsg);}

?>
<form id=”forml” name=”forml” method=”post” action=”checkLogin.php”>
<p>
<label for=”email”>Email</label>
<input type=”text” name=”email” id=”email” />
<label for=”password”>Password</label>
<input type=”text” name=”password” id=”password” />
<input type=”submit” name=”loginButton” id=”loginButton” value=”Login” />
</p>
</form>

</body>

</html>

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 26.2014 — You could do something like this, perhaps:
[code=php]
<?php
$problem = "";
if(!empty($_GET['problem'])) {
$problem = $_GET['problem'];
}
$errormsg ="<font color='red'> ERROR : ";
switch($_GET['problem']) {
case "invalidUser":
$errormsg .= "invalid user!!";
break;
case "invalidPassword":
$errormsg .= "invalid password!!";
break;
case "notLoggedIn":
$errormsg .= "you are not logged in";
break;
default:
$errormsg .= 'unknown error';
}
$errormsg .= "</font>";
if ($problem != "") {
print($errormsg);
}
?>
[/code]
Copy linkTweet thisAlerts:
@ginerjmSep 26.2014 — You are not showing us the code that is supposed to be providing the value for $_GET['problem'] so it makes it kind of hard for us to truly debug. That - and the fact that you are using deprecated html and poorly designed php techniques to gather your analysis of that value make solving this one little problem really a waste of time.
Copy linkTweet thisAlerts:
@legijaauthorSep 26.2014 — Notice: Undefined index: problem again
Copy linkTweet thisAlerts:
@legijaauthorSep 26.2014 — i am new in php. i will appreciate if you could help me
Copy linkTweet thisAlerts:
@ginerjmSep 26.2014 — I told you already - you are not showing us the place where you have created that GET variable named 'problem'. Where is 'problem' coming from? What script is calling this script that gets the error? Show us what comes before this code.
Copy linkTweet thisAlerts:
@NogDogSep 26.2014 — Well, since $_GET is derived from the URL used to access this page, the question is how it gets into the URL, and is it being correctly defined in the link or HTTP request that is sending the user to this page? So you need to look at the page(s) that redirect you to this page, and make sure they are setting a "?problem=some_value" query string in the link (and "problem" will be case-sensitive). The code I posted in my reply performs some defensive actions to handle situations where that query string value is not set for any reason, which is the immediate source of the error you are seeing (though not necessarily the root cause of [i]why[/i] that query string element is not being set -- that's what you need to look at the calling page(s) for).
Copy linkTweet thisAlerts:
@Sup3rkirbySep 26.2014 — Well, the reason you are seeing the '[I]Undefined index[/I]' error is because you are calling an index that has not been defined. When the page loads initially there is no '[I]problem[/I]' index in the [B]$_GET[/B] array, and thus you receive your error. When pulling variables from [B]$_GET[/B] and [B]$_POST[/B] (or [B]$_REQUEST[/B]) you should always be checking that they are set first, and then assigning the value to a variable.

[code=html]
<!doctype html>
<html>
<head>
<title>Login page</title>
</head>
<body style="background: aqua;">

<h1>Login page</h1>

<?php
$problem = (isset($_GET['problem']) ? $_GET['problem'] : '';
$errormsg ="<span style'color: red;'> ERROR : ";
if($problem == "invalidUser") $errormsg .= " invalid user!!";
if($problem == "invalidPassword") $errormsg .= " invalid password!!";
if($problem == "notLoggedIn") $errormsg .= "you are not logged in";
$errormsg .= "</span>";
if($problem != '') echo $errormsg;
?>

<form id="forml" name="forml" method="post" action="checkLogin.php">
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" />
<label for="password">Password</label>
<input type="text" name="password" id="password" />
<input type="submit" name="loginButton" id="loginButton" value="Login" />
</p>
</form>

</body>
</html>
[/code]



Also, just as a side note, your original code had a [B]<font>[/B] tag being used to display the error. The [B]<font>[/B] tag is deprecated and should not be used anymore.
Copy linkTweet thisAlerts:
@legijaauthorSep 27.2014 — great. it works. thanks
Copy linkTweet thisAlerts:
@dean047Nov 28.2014 — When I declare session variables in netbeans then it gives an error of undefined index and when I declare session variables in notepad++ then it doesn't give me the error. Both are using same WAMP server so the problem isn't in the WAMP server so I guess the problem is with netbeans. Does anyone encounter the same problem or anyone knows its solution?


Thanks.
Copy linkTweet thisAlerts:
@jasontallenOct 29.2019 — What about adding the following code to the login page?:
``<i>
</i>if(isset($_SESSION['logged_in']) &amp;&amp; $_SESSION['logged_in']== 'green')
{
header('Location: index.php');
exit;
}<i>
</i>
``

_Regards,

[webdeveloper](https://www.webdeveloper.com) [expert](https://essays.agency/)_
×

Success!

Help @legija spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...