Jump to content

Php documentation


davej

Recommended Posts

  • 2 weeks later...

I'm currently re-learning Php and getting into more of the details so I apologize for the dumb questions. Here is another documentation situation. All I want to know is whether $_POST[] will ever return anything but a string? This...http://www.php.net/manual/en/reserved.variables.post.php...does not tell me.Next do I need to sanitize or length-limit it or check with is_numeric() or anything before attempting intval() ? This...http://us3.php.net/manual/en/function.intval.php ...does not tell me.

Edited by davej
Link to comment
Share on other sites

Data submitted as post data (or get data, for that matter) does not include a data type. When you send data to the server, you don't tell it that it is an integer, or a boolean, or a string, you just send a name and a value. So, all data is treated as a string. That doesn't mean that $_POST is always an array of string name/value pairs, though. It's possible to format a request such that PHP will create arrays inside $_POST or $_GET. So, in general without any modification, $_POST and $_GET will be an associative multi-dimensional array of strings. The vast majority of the time they are not multi-dimensional though.

 

Next do I need to sanitize or length-limit it or check with is_numeric() or anything before attempting intval() ?
You don't *need* to do anything, if you're fine with the documented behavior that intval will return 0 if the value cannot be cast to an integer (with the exception of objects and arrays, which follow the same rules for casting to boolean). If you want your data to follow other rules, then validate it however you want before converting it. Since you can't distinguish a successful versus a failed cast to 0, you might want to do some other validation first depending on what data you're working with. In other cases, if 0 is not a valid value for your data anyway then there's no reason to check before converting.
Link to comment
Share on other sites

I am confused almost every time I look something up and the Php doc says just the bare minimum definition and then all the user comments try to explain what it means and how it should be used. In the case of session_destroy() the users are keen to explain that a proper log-out must also include statements to kill the session cookie. My question is: why? Why would I care if the session cookie continues to exist? It points to a blank session and will eventually timeout and die.

Link to comment
Share on other sites

They're just saying to consider a certain session ID invalid if a security violation occurs with that session, and one way to invalidate the session ID would be to unset the cookie. That would be an additional step to take for PHP, for PHP you would want to remove all of the session data and maybe even set a value to indicate that there was a violation.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...