0

I tried to add extra security by removing special characters. I want to allow letters, numbers and ? = & only.

I tried:

if (strpos($_SERVER['REQUEST_URI'],'\'')) {  echo 'true';   }

I cannot just simply put ' in between the '' as it breaks it so I tried adding the \ but it didn't work. Is there a way to detect all the symbols in the url string or input field?

EDIT:

tried adding < simply into the list

if (preg_match('#[@*,!$\'\-;:<>~`^|\(\\)\\{\\}\\[\\]]#i', $_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'],'script')) { 
    echo 'Cannot do that';
}

I tried adding ([\<])([^\>]{1,})*([\>]) into there but it didn't work. I also tried adding a condition if strcmp($_SERVER['REQUEST_URI'], strip_tags($_SERVER['REQUEST_URI'])) != 0

and when i added into the url, it didn't do anything

OGcode
  • 31
  • 6
  • 3
    Why not just handle strings properly? – Niet the Dark Absol Sep 12 '14 at 21:24
  • 1
    extra security for what? What is your script supposed to be used for? – Mike 'Pomax' Kamermans Sep 12 '14 at 21:25
  • `str_replace()` if you _really_ need to use it – Class Sep 12 '14 at 21:25
  • @NiettheDarkAbsol I am using pdo prepared statements everywher but for one specific area I have a lot of dynamic values for a search filter so it was hard to use pdo prepared statements so i am trying to somehow do a work around – OGcode Sep 12 '14 at 21:27
  • @Mike'Pomax'Kamermans I have a search filter that takes input from words, radio buttons, and checkboxes. The query length changes constantly so I am not sure how to dynamically make prepared statements to satisfy both the query and the prepared statement – OGcode Sep 12 '14 at 21:34

3 Answers3

1

Use preg_match to test for anything but the characters you want:

if (preg_match('#[^a-z0-9?=&]#i', $str)) { echo 'true'; }

Use preg_replace to remove them:

$str = preg_replace('#[^a-z0-9?=&]#i', '', $str);

If you just want to prohibit certain characters, use a regular expression that just matches those characters:

if (preg_match('#[\'\-;:~`]#i', $str)) { echo 'true'; }
Barmar
  • 669,327
  • 51
  • 454
  • 560
0

You can fix that using double quotes as strings delimiter, try this

if (strpos($_SERVER['REQUEST_URI'],"'")) {  echo 'true';   }
math
  • 155
  • 10
0

One thing that none of the posts addressed is why strpos didn't work for you. strpos can return two types. It can return an integer that is greater than or equal to zero. 0 being the first character. It can also return a boolean type false. To check if if strpos found a match it would have to have been written like this:

if (strpos($_SERVER['REQUEST_URI'],'\'') !== false) {  echo 'true';   }

From the PHP Documentation The comparison $a !== $b operator works this way:

return TRUE if $a is not equal to $b, or they are not of the same type.

Information on strpos returning two types (boolean false or an integer) can be found in this PHP strpos Documentation. In particular:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

So as you can see 0 and false are not the same thing which is why your test failed.

As for security and strings in PHP I recommend you look at this StackOverflow article for some opinions on the matter.

Community
  • 1
  • 1
Michael Petch
  • 43,801
  • 8
  • 98
  • 174