urlencode question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

urlencode question

Post by paqman »

I'm in the last stages of a site but I'm having one annoying problem. I'm trying to create urls for a news page which look a little nicer, using the title as part of the url (versus an id tag). I'm trying to use urlencode() to get the title in the right format for being used as part of the url, however, it does not encode non-alphanumeric characters, like a question mark. So if the title is 'Click Here?' url encode makes it 'Click+Here?'. When clicking on this link, the browser automatically converts the '?' to %3F. Using $_GET however, this %3F is lost, which then means it can't find the entry.

I'm also using mod rewrite in a .htaccess file, which I suspect may be another possible culprit. Here's the line that is handling this particular request (my first shot at using mod rewrite):

Code: Select all

 
RewriteRule ^(academic|drama|green|music|newspaper|newsletter|sports|studentcouncil|youthministry|parents|alumni)/([^/\.]+)/(.*) /test/department.php?department=$1&$2=$3
 
It seems to work fine so long as a character isn't an 'irregular' one (like ?, #, etc).

If you need to check it out, the site is located at http://stmc.bc.ca/test/. The 'Current' is the problem (try the top entry). The page outputs the $_GET["news"] (where news is $2 in the .htaccess) at the top...

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: urlencode question

Post by requinix »

Relying on the title being typed correctly is risky, especially since, like, everything out there does it differently. People are human: if they type "commonly-mispelled-words" instead of "commonly-misspelled-words" your system won't know about it.

This place is a little behind on the times, but the "normal" scheme is to have a URL along the lines of

Code: Select all

http://forums.devnetwork.net/php-code-1/urlencode-question-96685
The name gets converted into something close to the original but still URL safe, then the internal ID number gets appended. The RewriteRule for a thread would look like

Code: Select all

RewriteRule ^/?[^/]+-(\d+)/.*-(\d+) viewtopic.php?f=$1&t=$2
By using an ID people know (assume) it's important and are free to make as many typos in the fake thread title as they wish.


To address your original concern, though, the function you want is rawurlencode.
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: urlencode question

Post by paqman »

Turns out firefox was turning rawurlencode (when inside a hyperlink href) back into its previous value... (IE wasn't doing this). Very annoying. So to simplify everything I just decided to use preg_match in the admin area to only allow alphanumeric characters for titles for those entries. Thanks for the help though.
Post Reply