Request parameter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
czy11421
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 9:03 pm

Request parameter

Post 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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Request parameter

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Request parameter

Post 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.
czy11421
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 9:03 pm

Re: Request parameter

Post 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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Request parameter

Post 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. :)
Post Reply