/    Sign up×
Community /Pin to ProfileBookmark

Hello guys,

I tried to make a simple login script that prints “Welcome to duty admin” if the username that the user typed is “admin”. Otherwise, the message would be “Unauthorized”. Nonetheless, I can’t figure out why it gives me an error when I first run the script.

This is the code:

[code=html]<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$user = $_POST[‘Username’];
if ($user == “admin”) {
print (“Welcome to duty admin”);
}
else {
print(“You are not allowed here”);
}
?>
</head>
<body>

<FORM NAME =”form1″ METHOD =”Post” ACTION = “basicForm.php”>

<input type=”Text” name=”Username” value=”Username”>
<input type=”Submit” name=”Submit1″ value=”Login”>

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

The error:

Notice: Undefined index: Username in C:wampwwwbasicForm.php on line 5
Call Stack

# Time Memory Function Location

1 0.0000 139824 {main}( ) ..basicForm.php:0

P.S: It also says “Unauthorized” at the beginning even though I haven’t pressed the Login button.

Thanks!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@winthropiteFeb 01.2014 — Don't know if this is the problem but get rid of the spaces: FORM NAME =, METHOD =, and ACTION = "". Should be NAME="" METHOD="" ACTION="". I don't know if it's a bigger problem but that stuck out to me like a sore thumb.
Copy linkTweet thisAlerts:
@EmpowerauthorFeb 01.2014 — The problem persists :/
Copy linkTweet thisAlerts:
@NogDogFeb 01.2014 — $_POST['Username'] won't be set when you initially access the page, only when you actually submit the form. So if you are going to do it as a single script, then you need to wrap the code that does the test of the username in an IF block that tests whether the form field exists (either with isset() or !empty()).
×

Success!

Help @Empower 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.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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