Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[SOLVED] Probably simple: $_REQUEST['username'] Undefined index error

January 15, 2010 8:24am

Subscribe [4]
  • #1 / Jan 15, 2010 8:24am

    Guest

    10 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,

  • #2 / Jan 15, 2010 8:40am

    DieterStruik

    6 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)

  • #3 / Jan 15, 2010 11:10am

    flaky

    291 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
    }
  • #4 / Jan 15, 2010 11:49am

    flaky

    291 posts

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

  • #5 / Jan 15, 2010 1:55pm

    Guest

    10 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

  • #6 / Feb 21, 2014 9:45am

    kacyblack

    12 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)

     

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

  • #7 / Feb 21, 2014 10:02am

    Narf's avatar

    Narf

    155 posts

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

ExpressionEngine News

#eecms, #events, #releases