Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> __get $_GET, $_POST, pobieranie danych od usera
dr_bonzo
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Zamiast spac wpadlem na ciekawy pomysl i napisalem cos takiego:
  1. <?php
  2. class UserData
  3. {
  4. private $arrValues = array();
  5.  
  6. public UserData( $strSource )
  7. {
  8. $this->strSource = strval( $strSource );
  9. }
  10.  
  11. public __get( $strValName )
  12. {
  13. if ( isset( $this->arrValues[ $strValName ] ) )
  14. {
  15. if ( is_null( $this->arrValues[ $strValName ] ) )
  16. {
  17. throw new Exception( 'Value not present' );
  18. }
  19. else
  20. {
  21. return $this->arrValues[ $strValName ];
  22. }
  23. }
  24. else
  25. {
  26. $arrSuperGlobalArray = NULL;
  27.  
  28. switch ( $this->arrValues )
  29. {
  30. case 'get':
  31. {
  32. $arrSuperGlobalArray &= $_GET;
  33. break;
  34. }
  35.  
  36. case 'post':
  37. {
  38. $arrSuperGlobalArray &= $_POST;
  39. break;
  40. }
  41.  
  42. case 'cookie':
  43. {
  44. $arrSuperGlobalArray &= $_COOKIE;
  45. break;
  46. }
  47.  
  48. default :
  49. {
  50. throw new Exception( 'Invalid source type '' . $this->strSource . ''' );
  51. }
  52. }
  53.  
  54. if ( isset( $arrSuperGlobalArray[ $strValName ] ) )
  55. {
  56. return $arrSuperGlobalArray[ $strValName ];
  57. }
  58. else
  59. {
  60. $this->arrValues[ $strValName ] = NULL;
  61. }
  62. }
  63. }
  64. }
  65.  
  66. $objGetValues = new UserData( 'get' );
  67. print( $objGetValues->action );
  68.  
  69. $objPostValues = new UserData( 'post' );
  70. print( $objPostValues->id );
  71.  
  72. ?>


Nowy sposob pobierania danych wykorzystujacy php5. Nie testowany -- nie mam serwera pod reka. Co o tym sadzicie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
NuLL
post
Post #2





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

Ostrzeżenie: (0%)
-----


@hawk - a mi się wydaje, że każdy powinien robić tak jak ma ochotę.
To co jest pod spodem to mój lib, który stosuję do prawie każdego projektu - niestety pod php 4, ale wiadomo jakie mamy realia. Mnie osobiście jeśli koduje coś saemu takie rozwiązanie w zupełności wystarcza. Jakoże naprawdę nie wiele reczy przechodzi przez $_GET nie mam potrzeby rozgraniczania $_GET i $_POST. Reszta tablic to inna mańka (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
  1. <?
  2. class inputter{
  3. var $aData=array();
  4.  
  5. function clean_value($val){
  6. global $config_vars;
  7.  
  8. if ($val == &#092;"\") return \"\";
  9.  
  10. $val = str_replace( &#092;" \", \" \", $val );
  11.  
  12. if ( $onfig_vars['_strip_space_chr'] ){ $val = str_replace( chr(0xCA), &#092;"\", $val );}
  13.  
  14. $val = str_replace( &#092;"&\", \"&amp;\", $val );
  15. $val = str_replace( &#092;"<!--\", \"<!--\" , $val );
  16. $val = str_replace( &#092;"-->\", \"-->\", $val );
  17. $val = preg_replace( &#092;"/<script/i\" , \"<script\"  , $val );
  18. $val = str_replace( &#092;">\", \"&gt;\", $val );
  19. $val = str_replace( &#092;"<\", \"&lt;\", $val );
  20. $val = str_replace( &#092;"\"\", \"&quot;\", $val );
  21. $val = preg_replace( &#092;"/n/\", \"<br />\", $val ); //nl2br()
  22. $val = preg_replace( &#092;"/$/\", \"$\", $val );
  23. $val = preg_replace( &#092;"/r/\", \"\", $val ); // /r
  24. $val = str_replace( &#092;"!\", \"!\", $val );
  25. $val = str_replace( &#092;"'\", \"'\", $val ); //sql sec
  26. //unicode
  27. if ( $config_vars['_allow_unicode']){ $val = preg_replace(&#092;"/&amp;#([0-9]+);/s\", \"\", $val );}
  28. //wywalamy slashe
  29. if ( $config_vars['_stripslashes'] ){$val = stripslashes($val);}
  30. //wywalamy backslash'e
  31. $val = preg_replace( &#092;"/(?!&amp;#|?#)/\", \"\", $val );
  32. return $val;
  33. }
  34.  
  35. function clean_key($key){
  36. if ($key == &#092;"\"){return \"\";}
  37. $key = htmlspecialchars(urldecode($key));
  38. $key = preg_replace( &#092;"/../\"  , \"\" , $key );
  39. $key = preg_replace( &#092;"/__(.+?)__/\" , \"\" , $key );
  40. $key = preg_replace( &#092;"/^([w.-_]+)$/\", \"$1\", $key );
  41. return $key;
  42. }
  43.  
  44. function parse_incoming(){
  45. $return = array();
  46. if( is_array($_GET)){
  47. while( list($k, $v) = each($_GET) ){
  48. if ( is_array($_GET[$k]) ){
  49. while( list($k2, $v2) = each($_GET[$k]) ){
  50. $return[ inputter::clean_key($k)][ inputter::clean_key($k2)]=inputter::clean_value($v2);
  51. }
  52. }else{
  53. $return[ inputter::clean_key($k) ] = inputter::clean_value($v);
  54. }
  55. }
  56. }
  57.  
  58. if( is_array($_POST) )
  59. {
  60. while( list($k, $v) = each($_POST) )
  61. {
  62. if ( is_array($_POST[$k]) )
  63. {
  64. while( list($k2, $v2) = each($_POST[$k]) )
  65. {
  66. $return[inputter::clean_key($k)][inputter::clean_key($k2)]=inputter::clean_value($v2);
  67. }
  68. }
  69. else
  70. {
  71. $return[ inputter::clean_key($k) ] = inputter::clean_value($v);
  72. }
  73. }
  74. }
  75. $return['request_method'] = strtolower($_SERVER['REQUEST_METHOD']);
  76. return $return;
  77. }
  78. }
  79.  
  80. ?>

Ta klasa zawraca mi tablicę wszystkich danych ktore przyszły.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 28.12.2025 - 00:38