Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Probably simple: $_REQUEST['username'] Undefined index error
#1

[eluser]Guest[/eluser]
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:

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined index: username

My code is:

Code:
$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

[eluser]DieterStruik[/eluser]
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

[eluser]flaky[/eluser]
first of all use
Code:
$this->input->post('username');
//not $_REQUEST
for security issues

if you want to check if the post has been set
Code:
if($this->input->post('username')){
   //some code
}
#4

[eluser]flaky[/eluser]
And if you are dealing with forms, consider using Form Validation class
http://ellislab.com/codeigniter/user-gui...ation.html
#5

[eluser]Guest[/eluser]
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 :-)


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

Thank you both for your help :-)
#6

[eluser]kacyblack[/eluser]
[quote author="DieterStruik" date="1263562830"]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)[/quote]



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

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




Theme © iAndrew 2016 - Forum software by © MyBB