[other] Function add_url_params returns invalid encoded url #298

Open
opened 2026-10-04 23:51:07 +02:00 by adam · 1 comment
Owner

Reported by thimo on 30 Jan 2015 15:43

The parameter "$arg_separator" of function "add_url_params" in file include/functions_url.inc.php (line 88) should default to "&" instead "&". Also each value should be url encoded:

/**

  • adds one or more _GET style parameters to an url
  • example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
  • add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&a=b
  • @param string url
  • @param array params
  • @return string
    */
    function add_url_params($url, $params, $arg_separator='&' )
    {
    if ( !empty($params) )
    {
    assert( is_array($params) );
    $is_first = true;
    foreach($params as $param=>$val)
    {
    if ($is_first)
    {
    $is_first = false;
    $url .= ( strpos($url, '?')===false ) ? '?' : $arg_separator;
    }
    else
    {
    $url .= $arg_separator;
    }
    $url .= $param;
    if (isset($val))
    {
    $url .= '='.urlencode($val);
    }
    }
    }
    return $url;
    }

Piwigo Bugtracker #3193

**Reported by thimo on 30 Jan 2015 15:43** The parameter &quot;$arg_separator&quot; of function &quot;add_url_params&quot; in file include/functions_url.inc.php (line 88) should default to &quot;&amp;&quot; instead &quot;&amp;&quot;. Also each value should be url encoded: /** - adds one or more _GET style parameters to an url - example: add_url_params(&#039;/x&#039;, array(&#039;a&#039;=&gt;&#039;b&#039;)) returns /x?a=b - add_url_params(&#039;/x?cat_id=10&#039;, array(&#039;a&#039;=&gt;&#039;b&#039;)) returns /x?cat_id=10&amp;a=b - @param string url - @param array params - @return string */ function add_url_params($url, $params, $arg_separator=&#039;&amp;&#039; ) { if ( !empty($params) ) { assert( is_array($params) ); $is_first = true; foreach($params as $param=&gt;$val) { if ($is_first) { $is_first = false; $url .= ( strpos($url, &#039;?&#039;)===false ) ? &#039;?&#039; : $arg_separator; } else { $url .= $arg_separator; } $url .= $param; if (isset($val)) { $url .= &#039;=&#039;.urlencode($val); } } } return $url; } [Piwigo Bugtracker #3193](http://piwigo.org/bugs/view.php?id=3193) <!-- github-sync-id:97227024 -->
Author
Owner

Comment posted by thimo on 30 Jan 2015 15:44

It should default to "&" instead "& amp;"

**Comment posted by thimo on 30 Jan 2015 15:44** It should default to &quot;&amp;&quot; instead &quot;&amp; amp;&quot; <!-- github-comment-id:124899147 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/Piwigo#298