473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strval $_REQUEST

I have seen some code like
$value = strval($REQUEST['value']);

I would like to know what is the use of strval here since $_REQUEST
values are strings?

May 16 '07 #1
11 7275
jm***@fastermail.com wrote:
I have seen some code like
$value = strval($REQUEST['value']);

I would like to know what is the use of strval here since $_REQUEST
values are strings?
Hi,

$_REQUEST values might as well contain arrays.

Consider this form:
<form action="test.php" Method="POST">
<input type="checkbox" name="bla[]" value="1">1<br>
<input type="checkbox" name="bla[]" value="2">2<br>
<input type="checkbox" name="bla[]" value="3">3<br>
</form>

When receiving $_POST["bla"] it contains an array.

The programmer wanted to make sure it is a string.

Regards,
Erwin Moller

PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.
May 16 '07 #2
At Wed, 16 May 2007 20:17:07 +0200, Erwin Moller let his monkeys type:
jm***@fastermail.com wrote:
>I have seen some code like
$value = strval($REQUEST['value']);

I would like to know what is the use of strval here since $_REQUEST
values are strings?
>
PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.
Erwin, what's wrong with using $_REQUEST instead of $_GET / $_POST ?
Security issue?

Sh.

May 16 '07 #3
I would guess they are just being redundant.
You're right $_REQUEST is a string (most PHP variables are).
So converting a string to a string is pointless.

May 16 '07 #4
On May 16, 1:17 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
j...@fastermail.com wrote:
I have seen some code like
$value = strval($REQUEST['value']);
I would like to know what is the use of strval here since $_REQUEST
values are strings?

Hi,

$_REQUEST values might as well contain arrays.

Consider this form:
<form action="test.php" Method="POST">
<input type="checkbox" name="bla[]" value="1">1<br>
<input type="checkbox" name="bla[]" value="2">2<br>
<input type="checkbox" name="bla[]" value="3">3<br>
</form>

When receiving $_POST["bla"] it contains an array.

The programmer wanted to make sure it is a string.

Regards,
Erwin Moller

PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.
But as per documentation
http://us.php.net/manual/en/function.strval.php

strval can only be used on scalars
using strval on arrays has no effect.
the programmer should have used is_string if its a string

May 17 '07 #5
On May 16, 3:05 pm, Schraalhans Keukenmeester <inva...@invalid.spam>
wrote:
At Wed, 16 May 2007 20:17:07 +0200, Erwin Moller let his monkeys type:
j...@fastermail.com wrote:
I have seen some code like
$value = strval($REQUEST['value']);
I would like to know what is the use of strval here since $_REQUEST
values are strings?
PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.

Erwin, what's wrong with using $_REQUEST instead of $_GET / $_POST ?
Security issue?

Sh.
This is more similar to a type checking issue and not security.
$_REQUEST can be either a get, a post, or a cookie variable. There are
circumstances where its more convenient to use $_REQUEST, like if you
have to call a certain script using either get or post method.

May 17 '07 #6
jm***@fastermail.com wrote:
On May 16, 3:05 pm, Schraalhans Keukenmeester <inva...@invalid.spam>
wrote:
>At Wed, 16 May 2007 20:17:07 +0200, Erwin Moller let his monkeys type:
j...@fastermail.com wrote:
>I have seen some code like
$value = strval($REQUEST['value']);
>I would like to know what is the use of strval here since $_REQUEST
values are strings?
PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.

Erwin, what's wrong with using $_REQUEST instead of $_GET / $_POST ?
Security issue?

Sh.

This is more similar to a type checking issue and not security.
$_REQUEST can be either a get, a post, or a cookie variable. There are
circumstances where its more convenient to use $_REQUEST, like if you
have to call a certain script using either get or post method.
Yes, excactly what I ment.
A little more elaborated explanation:
Problem with $_REQUEST is that it gets populated (virtually I think) from
POST GET COOKIE.
If you KNOW what you are receiving, use the right array.

And yes, I have been in circumstances in which it was convienient for me to
use $_REQUEST, but not often.

It is more a check for the programmer than a security issue.
A silly example:
I expect from a form-posting the name 'example1', but in the form I wrote
'exmple1'.
If I have a cookie in use named 'example1' or in my URL something like:
http://www.example.com/test.php?example1=45

Then using $_REQUEST will fill retrieve the 'example' name/value pair from
the wrong place.
That is why I advise using the superglobal you KNOW you are using.

SO it is not really a security issue (because everybody should check info
from cookie, get and post anyway), but more a line of protection against
coding/thinking mistakes.

just my 2 cent..

Regards,
Erwin Moller

May 17 '07 #7
jm***@fastermail.com wrote:
On May 16, 1:17 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>j...@fastermail.com wrote:
I have seen some code like
$value = strval($REQUEST['value']);
I would like to know what is the use of strval here since $_REQUEST
values are strings?

Hi,

$_REQUEST values might as well contain arrays.

Consider this form:
<form action="test.php" Method="POST">
<input type="checkbox" name="bla[]" value="1">1<br>
<input type="checkbox" name="bla[]" value="2">2<br>
<input type="checkbox" name="bla[]" value="3">3<br>
</form>

When receiving $_POST["bla"] it contains an array.

The programmer wanted to make sure it is a string.

Regards,
Erwin Moller

PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.

But as per documentation
http://us.php.net/manual/en/function.strval.php

strval can only be used on scalars
using strval on arrays has no effect.
the programmer should have used is_string if its a string
good point.
Kind of useless action indeed.

Regards,
Erwin Moller
May 17 '07 #8
On May 16, 1:17 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
j...@fastermail.com wrote:
I have seen some code like
$value = strval($REQUEST['value']);
I would like to know what is the use of strval here since $_REQUEST
values are strings?

Hi,

$_REQUEST values might as well contain arrays.

Consider this form:
<form action="test.php" Method="POST">
<input type="checkbox" name="bla[]" value="1">1<br>
<input type="checkbox" name="bla[]" value="2">2<br>
<input type="checkbox" name="bla[]" value="3">3<br>
</form>

When receiving $_POST["bla"] it contains an array.

The programmer wanted to make sure it is a string.

Regards,
Erwin Moller

PS: I think you should avoid $_REQUEST. Just use $_POST and $_GET.
strval works only on scalar values. Using it on arrays has no effect.
is_string() should be the function to be used to determine if a value
is string. strval just casts values into strings. Casting a string
into a string is meaningless.
There are some instances where $_REQUEST can be more convenient than
using $_POST and $_GET like for example where you have a script that
is called by either get or post method

May 17 '07 #9
SterLo <st***************@gmail.comwrote:
I would guess they are just being redundant.
You're right $_REQUEST is a string (most PHP variables are).
So converting a string to a string is pointless.
As far as I know, $_GET $_POST and $_REQUEST are associative arrays
whose elements can be:

- unset (i.e. undefined)

- a string of arbitrary bytes, ranging from 0 up to max(memory_limit,
post_max_size, some_web_server_limit)

- an array of any combination of strings and arrays of strings with
arbitrary keys.

Consider these examples:

1) www.mysite.com/test.php?a=x%00%01x
$_GET['a'] becomes an unexpected "x%5C0%01x"
(here the string is urlencoded() for readability; note the "0" after "%5C")

2) www.mysite.com/test.php?a[]=xxx
$_GET['a'] becomes array(0=>"xxx")

3) www.mysite.com/test.php?a[one]=xxx&a[1]=yyy
$_GET['a'] becomes array("one"=>"xxx", 1=>"yyy"))
(note the keys "one" and 1)

4) www.mysite.com/test.php?a[3]=xxx&a[2][0]=yyy&a[-1]=zzz
$_GET['a'] becomes array(3=>"xxx", 2=>array(0=>"yyy"), -1=>"zzz")

5) www.mysite.com/test.php
$_GET['a'] is not set

The same holds for the $_POST and $_REQUEST arrays.

If all that involves some security issue or not, it depends on how all these
values are handled by your program. An array can be evaluated as "Array"
when a string is expected, and can be evaluated as 0 if a number is expected.
Some functions of the standard library behave differently depending on the
value they receive, be it a string or an array. Some test for validation
may fail. For example, for the case 3:

strlen($_GET['a']) gives always 5 for any array.

$_GET['a'] + 1 gives 1 for any array

preg_match("/^\\w\$/", $_GET['a']) raises an error and return false

$somearray[ $_GET['a'] ] always select the element of key "Array"

....and so on, with more and more unexpected behaviors and oddities.

The solution: build your own functions/classes that validate and sanitize
every type of input. As a base rule:

A) Most of the received parameters are (should be...) strings. Apply
a (string) type-cast:

$s = (string) $_GET['a'];

then remove control chars and check proper encoding (ISO-..., UTF-8, etc.).

B) <textarea>: as for A, but \t \r and \n should be preserved.

C) Numbers: if a little integer number is expected, apply the (int) type-cast
then check the range:

$i = (int) $_GET['a'];
$i = min( max($i, SOME_MIN), SOME_MAX );

Otherwise, if the number is a monetary value (example: "1,234.99") convert
to string and apply preg_match() with a proper REGEX, something like:

$a = trim( (string) $_POST['a'] );
if( preg_match("/^[0-9]+(,[0-9]{3})*(\\.[0-9]+)?\$/", $a ) === 1 ){
# ok, now remove commas then use BCMath or GMP for calculations
} else {
# BAD
}

D) <select multiple>: more difficult to validate. Check the received array
be actually an array, then copy every value in another array converting
every element to string or to int, depending on the expected type; ignore
duplicated values:

$a = $_POST['a'];
$a_sanitized = array();
if( is_array($a) ){
foreach($a as $v){
$i = (int) $v;
if( array_search($i, $a_sanitized) === FALSE )
$a_sanitized[] = $i;
}
}
# Here: check the values $a_sanitized[] be valid, for example they
# may be compared againts good values saved in the session, or in a
# hidden field protected with HMAC.

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

May 17 '07 #10
jmark wrote:
I have seen some code like
$value = strval($REQUEST['value']);

I would like to know what is the use of strval here since $_REQUEST
values are strings?
It may just be for readability -- anyone skimming through the code could
instantly see that $value is intended to be a string, and not, say, an
integer.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 18 '07 #11
On May 17, 9:56 am, Umberto Salsi <s...@icosaedro.italiawrote:
SterLo <sterling.hamil...@gmail.comwrote:
I would guess they are just being redundant.
You're right $_REQUESTis a string (most PHP variables are).
So converting a string to a string is pointless.

As far as I know, $_GET $_POST and $_REQUESTare associative arrays
whose elements can be:

- unset (i.e. undefined)

- a string of arbitrary bytes, ranging from 0 up to max(memory_limit,
post_max_size, some_web_server_limit)

- an array of any combination of strings and arrays of strings with
arbitrary keys.

Consider these examples:

1)www.mysite.com/test.php?a=x%00%01x
$_GET['a'] becomes an unexpected "x%5C0%01x"
(here the string is urlencoded() for readability; note the "0" after "%5C")

2)www.mysite.com/test.php?a[]=xxx
$_GET['a'] becomes array(0=>"xxx")

3)www.mysite.com/test.php?a[one]=xxx&a[1]=yyy
$_GET['a'] becomes array("one"=>"xxx", 1=>"yyy"))
(note the keys "one" and 1)

4)www.mysite.com/test.php?a[3]=xxx&a[2][0]=yyy&a[-1]=zzz
$_GET['a'] becomes array(3=>"xxx", 2=>array(0=>"yyy"), -1=>"zzz")

5)www.mysite.com/test.php
$_GET['a'] is not set

The same holds for the $_POST and $_REQUESTarrays.

If all that involves some security issue or not, it depends on how all these
values are handled by your program. An array can be evaluated as "Array"
when a string is expected, and can be evaluated as 0 if a number is expected.
Some functions of the standard library behave differently depending on the
value they receive, be it a string or an array. Some test for validation
may fail. For example, for the case 3:

strlen($_GET['a']) gives always 5 for any array.

$_GET['a'] + 1 gives 1 for any array

preg_match("/^\\w\$/", $_GET['a']) raises an error and return false

$somearray[ $_GET['a'] ] always select the element of key "Array"

...and so on, with more and more unexpected behaviors and oddities.

The solution: build your own functions/classes that validate and sanitize
every type of input. As a base rule:

A) Most of the received parameters are (should be...) strings. Apply
a (string) type-cast:

$s = (string) $_GET['a'];

then remove control chars and check proper encoding (ISO-..., UTF-8, etc.).

B) <textarea>: as for A, but \t \r and \n should be preserved.

C) Numbers: if a little integer number is expected, apply the (int) type-cast
then check the range:

$i = (int) $_GET['a'];
$i = min( max($i, SOME_MIN), SOME_MAX );

Otherwise, if the number is a monetary value (example: "1,234.99") convert
to string and apply preg_match() with a proper REGEX, something like:

$a = trim( (string) $_POST['a'] );
if( preg_match("/^[0-9]+(,[0-9]{3})*(\\.[0-9]+)?\$/", $a ) === 1 ){
# ok, now remove commas then use BCMath or GMP for calculations

} else {
# BAD
}

D) <select multiple>: more difficult to validate. Check the received array
be actually an array, then copy every value in another array converting
every element to string or to int, depending on the expected type; ignore
duplicated values:

$a = $_POST['a'];
$a_sanitized = array();
if( is_array($a) ){
foreach($a as $v){
$i = (int) $v;
if( array_search($i, $a_sanitized) === FALSE )
$a_sanitized[] = $i;
}}

# Here: check the values $a_sanitized[] be valid, for example they
# may be compared againts good values saved in the session, or in a
# hidden field protected with HMAC.

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
Thank you Umberto for your detail information. The answer as you have
pointed out is on sanitization

May 18 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sandro Dentella | last post by:
I have some scripts that used to work under webmin under Debian(woody). Under Debian Sarge php passed from 4.1.2 to 4.3.4 and I'm experimenting several problems. Among the others, array...
7
by: melty | last post by:
This line of code make an "Undefined index" error on my PC, however it is okay on my friend's PC. Anyone has this experience? $somevar = $_REQUEST;
2
by: Sean | last post by:
Hello, The isset($_REQUEST) works okay on other servers but not on mine. The way the application works is the links are provided as: http://url.com/cc.php?page=currencies&new And then in...
4
by: Geoff Soper | last post by:
I'm working on an authentication system in which it's possible that a user might be requested to log-in as a result of submitting a form if the inactivity timeout is exceeded. In order that they...
2
by: Geoff Winkless | last post by:
Hi My knowledge of php is regrettably poor but I need to call a third-party php script from within a bash cgi script (don't ask why, it's a long story). Now normally (with eg perl-cgi) to do...
4
by: Fred!head | last post by:
Hi, Probably this is a newbie question so I appreciate you bearing with me. I've got an application where users can create forms with name= values they define. I'd like to write a script that...
6
by: sathyashrayan | last post by:
Dear Group, Please look at the following demo link. http://www.itsravi.com/demo/new_pms/admin/addproject.php
3
by: hassnajib | last post by:
I am having problem retrieving $_REQUEST array key/value i submintted using post method. here is the simple form I am posting: <html> <head> </head> <body> <form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.