EllisLab text mark
Advanced Search
     
[SOLVED] Probably simple: $_REQUEST[‘username’] Undefined index error
Posted: 15 January 2010 08:24 AM
Joined: 2009-10-21
15 posts

Hi guys,

I’m having a bit of a problem.

I usally use this code just to confirm that a user has sent variables to the current page (they are post not get)—and the code works fine when the variables have been set, but when the page is loaded without the passed variables, i get this error:

A PHP Error was encountered
Severity: Notice

Message: Undefined index: username

My code is:

$username             $_REQUEST['username'];
        
$password            $_REQUEST['password'];
        
$email                $_REQUEST['email'];
        
        if(isset(
$username) && isset($password) && isset($email)){
            
// Looks good so far...
            
$showview 'join/welcome';
        
        
}else{
            $showview 
'join/retry';
        

I think it’s because the $_request is null (which is right—as the page has been deliberately loaded wrong), but instead of just leaving it unset for the php code to pick it up, codeigniter is displaying an error.

I do not want to turn error handling off, i would just like to know the proper way an experianced coder would handle the above.

Thanks so much!

Regards,

 
Posted: 15 January 2010 08:40 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-08-27
7 posts

In php you can suppress errors by adding a @ sign like:

$array = array(‘green’ => ‘existing value’);

echo @$array[‘red’]; // won’t display an error.

More clean is checking for a existing key or variable with key_exists($key, $array) or isset($var)

 
Posted: 15 January 2010 11:10 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-11-01
296 posts

first of all use

$this->input->post('username');
//not $_REQUEST 

for security issues

if you want to check if the post has been set

if($this->input->post('username')){
   
//some code
 Signature 

http://flakron.net

 
Posted: 15 January 2010 11:49 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-11-01
296 posts

And if you are dealing with forms, consider using Form Validation class
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

 Signature 

http://flakron.net

 
Posted: 15 January 2010 01:55 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-10-21
15 posts

Thanks very much for your replies.


DieterStruik—i have now used your @ trick in my views—that is great, as now i do not have to keep passing empty $data[‘vars’] to views that have variables, but somtimes do not need them grin


flaky—i have rewriten everywhere that had $_REQUEST to use $this->input->post. Great tip grin

Thank you both for your help grin

 
Posted: 21 February 2014 09:45 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2014-02-20
14 posts
DieterStruik - 15 January 2010 08:40 AM

In php you can suppress errors by adding a @ sign like:

$array = array(‘green’ => ‘existing value’);

echo @$array[‘red’]; // won’t display an error.

More clean is checking for a existing key or variable with key_exists($key, $array) or isset($var)

 

Thanks DieterStruik, Your Code Advice helped me a lot, God Bless You!

 
Posted: 21 February 2014 10:02 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2012-01-09
136 posts

There’s no point in using both error suppression (the @ operator, which is also considered a bad practice) and $this->input->post().