Daniel
Daniel

Reputation: 189

Prevent CakePHP from stripping chars from url passed as param?

I am developing an application using CakePHP 2.6 and having issues with passing a string containing url characters as a parameter through to a controller method.

In my view I have a chunk of code which echoes out a series of table rows containing data and passes through the page id, unit id and the id of the link which can sometimes contain a url.

<?php foreach($linklist as $l) { ?>
    <tr id="Link_<?php echo $l['ID']; ?>">
        <td><?php echo $l['Title']; ?></td>
        <td class="buttontd"><?php echo $this->Form->postlink('Delete', array('action' => 'deletelink', $this->request->params['pass'][0], $results[0]['PageUnitTypeID'], $l['ID']), array('class' => 'button delete')); ?></td>
    </tr>
<?php } ?>

When the postlink button passes the information over to the 'deletelink' action in the controller the url looks like this:

http://mydomainname.com/webpages/deletelink/239/7/urlhttp%3A%2F%2Fwww.google.co.uk%2F

Which shows that the url has been passed as the string but then in the action when I try to just var_dump() the third parameter it returns a string of www.google.co.uk and nothing more which is preventing me from doing a substr() call on the parameter to check if the first 3 characters are equal to url or not.

I have tried to wrap the parameter in the postlink call inside serialize() and urlencode() but neither has had the desired effect of returning the full string as

urlhttp%3A%2F%2Fwww.google.co.uk%2F

Does anyone know of a successful way to pass through a parameter like this without losing important characters?

Update 1: Deletelink action

public function deletelink($pid = null, $uid = null, $lid = null) {
    $this->autoRender = false;

    if (!is_null($pid) && is_numeric($pid) && !is_null($uid) && is_numeric($uid)) {

        if (!is_null($lid)) {

            if (substr($lid, 0, 3) == 'url') {
                echo substr($lid, 0, 3);
            } else {
                echo substr($lid, 0, 3);
            }

        } else {
            $this->Session->setFlash('It is unknown which link you wish to delete from the webpage', 'flash_message_bar', array('class' => 'error'));
            return $this->redirect(array('action' => 'edit', $pid));
        }

    } else {
        $this->Session->setFlash('It is unknown which on which webpage you wish to delete a link', 'flash_message_bar', array('class' => 'error'));
        return $this->redirect(array('action' => 'index'));
    }
}

Upvotes: 3

Views: 544

Answers (1)

Inigo Flores
Inigo Flores

Reputation: 4469

CakePHP (or mod_rewrite I would say) is getting confused with the way your URL is formed.

Your safest option is to base64_encode the url parameter in the view, which will result in call similar to:

http://mydomainname.com/webpages/deletelink/239/7/dXJsaHR0cDovL3d3dy5nb29nbGUuY28udWsv

and base64_decode it later in the action, which will transform

dXJsaHR0cDovL3d3dy5nb29nbGUuY28udWsv

into

urlhttp://www.google.co.uk/

Upvotes: 1

Related Questions