Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for POST variable w/out getting errors 1

Status
Not open for further replies.

natwod

Programmer
Nov 21, 2002
41
US
What I am trying to do is ask for a password, and possibly a username, and then direct the person to a certain page depending on what they type. There will not be a large number of people, so they will all have their own pages, but they will be "inside" a main PHP file.

The way I tried to do it was to use a form directed to its self with a password field, and then check to see if the password was correct. However, when I use something like this:

<?php if ($_POST[&quot;password&quot;]) { ?>

or any time I look for the &quot;password&quot; field before the password has been sent, I get an &quot;index not found&quot; message.

I know that with ASP, I can do the exact same thing and it does not have a problem. Any Ideas on how to get rid of the message (without lowering the &quot;error message&quot; level)?

Thanks!!
Natwad
 
There are several ways of doing this. My preferred way (for no particular reason other than that's my coding style) is to use array_key_exists() ( This function will tell you whether a key exists in an array without raising any kinds of errors.

In your case, I would use:

Code:
if (array_key_exists('password', $_POST))
{
    // then do something
}
Want the best answers? Ask the best questions: TANSTAAFL!
 
From a security standpoint it would be better to check for when there is no password passed i.e., the user did not fill anything in.
if (!$_POST[password]) { -> maybe go back to the login screen

If you do this check first there's no possibility that $_POST[password] is not set.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top