Page 1 of 1

Request parameter

Posted: Sat Jan 09, 2010 10:58 pm
by czy11421
I am learning PHP, in coding, I write

Code: Select all

 
if($_REQUEST['name'] == 'peter' && $_REQUEST['password'] == '1234'){
}
 
it will give me warning,

Code: Select all

 
Notice: Undefined index: name in C:\wamp\www\CasePHP\login.php on line 32
 
how can I check the 'name' is in REQUEST and avoid this notice ?

Thanks

Re: Request parameter

Posted: Sat Jan 09, 2010 11:02 pm
by SidewinderX
You can change your error_reporting level. For example

Code: Select all

<?php
error_reporting(0);
 
if($_REQUEST['name'] == 'peter' && $_REQUEST['password'] == '1234'){
 
}
 
?>
P.S.
Try to avoid using $_REQUEST it can be very ambiguous. Instead use $_GET or $_POST

Re: Request parameter

Posted: Sun Jan 10, 2010 1:31 am
by requinix
SidewinderX wrote:You can change your error_reporting level.
Oh yeah, because hiding the error makes everything work.
Bad advice. But I agree with using $_GET/$_POST instead.

To check if $_POST has the key you can use isset().

Code: Select all

<form action="..." method="post">
Username: <input type="text" name="name" />
Password: <input type="text" name="password" />
<input type="submit" name="login" value="Login" />
</form>

Code: Select all

if (isset($_POST["login"], $_POST["name"], $_POST["password"])) {
    // ...
}
Note how isset() won't check if there's something in the field - merely that it exists.

Re: Request parameter

Posted: Sun Jan 10, 2010 5:53 am
by czy11421
changed to this, it will work.

Code: Select all

 
 if(isset($_REQUEST['name']) && $_REQUEST['name'] == 'peter'
                        && isset($_REQUEST['password']) && $_REQUEST['password'] == '1234'){
}
 
Thanks for your inputs.

Re: Request parameter

Posted: Sun Jan 10, 2010 9:05 am
by Charles256
Read http://doughboy.wordpress.com/2008/01/1 ... -variable/ and stop using request. I assure you you do not need to and should not use request. Read it twice if you disagree. :)