Welcome Guest, Not a member yet? Register   Sign In
simpler way to find request method
#1

[eluser]zauber[/eluser]
Quick question: is there a CI-built-in method to do: if($_SERVER["REQUEST_METHOD")=="POST"){...}

Not that it's very hard to do, I just couldn't find anything about request-method handling in the docs, and figured there might be something.
#2

[eluser]xwero[/eluser]
you could check for the $_POST global
Code:
isset($_POST)
If you want a CI method you could name your submit or use some input that will always gets posted to check
Code:
!$this->input->post('submitInputName')
#3

[eluser]zauber[/eluser]
Ah, thanks! the $this->input thing I did not know about. I was just pulling things from $_POST directly. But I think that the $_POST array is always set (it's just empty sometimes) so the first alternative doesn't work. The second alternative looks brilliant though.
#4

[eluser]zauber[/eluser]
Another follow up then - now that I know about the input class - does the validation library get the same security filtering on the post-data as you get from input->post()?
#5

[eluser]Michael Wales[/eluser]
If you use validation, the following three will all be equal:

Code:
$_POST['var'];
$this->input->post('var');
$this->validation->var;

Without validation, omit the 3rd line.
#6

[eluser]xwero[/eluser]
Sometimes the $_POST global is set but you can do
Code:
isset($_POST) && count($_POST) > 0

If you mean that the validation uses the global xss filter i don't think so but i'm not sure. But you can add it to the field rules yourself using xss_clean.
#7

[eluser]zauber[/eluser]
[quote author="Michael Wales" date="1198103376"]If you use validation, the following three will all be equal:

Code:
$_POST['var'];
$this->input->post('var');
$this->validation->var;

Without validation, omit the 3rd line.[/quote]

The docs say, regarding the input class, that it:

Quote:...Filters the POST/COOKIE array keys, permitting only alpha-numeric (and a few other) characters...

On the other hand, no mention of this filtering appears together with the description of the input->post() function, so perhaps it's something that's done at construction time.

I suppose it doesn't matter unless I want to:
foreach($_POST as $key=>$val){...do something irresponsible with $val }

...which I of course wouldn't Smile
#8

[eluser]ejangi[/eluser]
I recently put together a request_helper.php file for this which just contains:
Code:
/**
* Return the request type that the user is making
*
* @return string
*/
function request_method()
{
    return strtolower($_SERVER['REQUEST_METHOD']);
} // request_method()



/**
* Is the current request a POST request?
*
* @return bool
*/
function request_is_post()
{
    if (request_method() == 'post') {
        return true;
    }
    return false;
}

I autoload it on all my projects so that in the controller I can just do things like:

Code:
public function edit()
{
    if (request_is_post()) {
        // do save/validation stuff...
    } else {
        // just display the form...
    }
}
#9

[eluser]zauber[/eluser]
hey, ucantblamem - that's a sweet and simple little solution. Being new to the CI-stuff, it's really helpful to get to see ways in which people are tweaking CI to do what they want in elegant ways. Thanks!
#10

[eluser]maadmac[/eluser]
[quote author="zauber" date="1198100383"]Ah, thanks! the $this->input thing I did not know about. I was just pulling things from $_POST directly. But I think that the $_POST array is always set (it's just empty sometimes) so the first alternative doesn't work. The second alternative looks brilliant though.[/quote]

Be sure to read the Input Class page in the CI user guide; it explains this and a whole lot more.

@walesmd You are like a forum posting madman. Where do you find the time??




Theme © iAndrew 2016 - Forum software by © MyBB