Jump to content

avoid special character on url


robert_gsfame

Recommended Posts

How can i avoid special character to be written on url..

let say i have

 

mypage.php?name=James+Barnwell

 

then when i put special character like %?()* n i will get an error...i know that this is very dangerous but i dont have any idea on how to avoid that

 

i try to use str_replace but not working...

 

$string=$_GET['value'];

$replacethis=array('\%','\?','\*');

$replaceby=array('','','');

$string1=str_replace($replacethis,$replaceby,$string);

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/207412-avoid-special-character-on-url/
Share on other sites

when i use urlencode() assume "Jim Carey", then i will have trouble with the query

 

I will have

$query=mysql_query("SELECT * FROM table1 WHERE actor_name='Jim+Carey'");

instead of

$query=mysql_query("SELECT * FROM table1 WHERE actor_name='Jim Carey'");

 

 

???

 

You can undo urlencode with its brother function urldecode. When you're passing the names in the url you'll use urlencode, eg

 

$name = 'Jim Carey';
echo '<a href="name.php?name='.urlencode($name).'">Send Name</a>';

 

When you reftrieve the name from the url you'll use urldecode,

if(isset($_GET['name']))
    echo urldecode($_GET['name']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.