Search code examples
phpcodeigniterescapingspecial-characterscodeigniter-url

Codeigniter disallowed characters workaround


I have already checked quite a few other answers but to no avail.

I have been hired to fix bugs for a job that some other developer ran away from. The application has a add comment and delete comment functionality.

The problem comes in the delete comment part. He designed the database such that all comments are simply entered into a single cell separated by pipe characters. So while deleting a comment, the entire comment needs to be placed in the url as a parameter which is then passed to the model and removed from the database.

I do know this is bad, but I cannot recode the entire functionality.

Now, when a user enters a comment such as "What's Up?", the delete comment url throws the "Codeigniter: The URI you submitted has disallowed characters." error.

I tried converting the quotes to HTML character entities but they again contain disallowed characters.

Can anybody please suggest a possible workaround for this problem? Redesigning the database is not a viable option as I'll then have to change the extensive php code used for handling the different delimiters. Messing with the disallowed characters list also seems to be a bad idea.

Thank you.


Solution

  • I am not sure if htmlentities will help. Did you first call urlencode on just the parameters?

    <?php
    
    $query_string = 'foo=' . urlencode("What's Up?");
    echo '<a href="mycgi?' . htmlentities($query_string) . '">';
    
    ?>
    

    <a href="mycgi?foo=What%27s+Up%3F">

    Also check if you need to add escape characters to any of these if they are treated as special characters by the database.

    e.g. If % is treated as special character, then you may need to add a \ before it.