fork download
  1. <?php
  2. //-- usage
  3.  
  4. $_SERVER['REQUEST_URI'] = 'foo.com/blah?a=1&b=2';
  5. function get_link( $my_params )
  6. {
  7. $param_querystring = "";
  8. $http_host = $_SERVER['HTTP_HOST'];
  9.  
  10. //-- get the part of the url before the querystring, if applicable
  11. $url = explode( '?', $_SERVER['REQUEST_URI'] );
  12.  
  13. $request_uri = $url[0];
  14. $querystring = $url[1];
  15.  
  16. foreach ( $my_params as $param_key => $param_value )
  17. {
  18. $param_querystring .= $param_key . '=' . $param_value;
  19. }
  20.  
  21. if ( empty( $querystring ) )
  22. {
  23. //-- generates foo.com/blah?x=1&y=2&z=string if no
  24. //-- querystring was present
  25. $link = $request_uri . '?' . $param_querystring;
  26. }
  27. else
  28. {
  29.  
  30. //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2
  31. //-- querystring was already present.
  32. $link = $request_uri . $querystring . '&' . $param_querystring;
  33. }
  34.  
  35. return $link;
  36. }
  37. function assertEquals($a, $b) {
  38. if ($a === $b)
  39. return true;
  40. echo "Failed assertion; got: $a\n";
  41. echo " expected: $b\n";
  42. }
  43. assertEquals(get_link(array('x' => 1, 'y' => 2, 'z' => 'string')),
  44. 'foo.com/blah?a=1&b=2&x=1&y=2&z=string');
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
Failed assertion; got: foo.com/blaha=1&b=2&x=1y=2z=string
             expected: foo.com/blah?a=1&b=2&x=1&y=2&z=string
stderr
PHP Notice:  Undefined index: HTTP_HOST in /home/wCWjDs/prog.php on line 8