Monkuar Posted March 1, 2012 Share Posted March 1, 2012 I am letting users select there color code option for text-shadow for there username I am storing them into a field called "color" HEX exaMPLE: FF6633,AA3633 First is the background color, 2nd is the foreground. (then i'll explode them) Is there a PHP function that let's me validate the input so it's HEX ONLY? I will be db escaping it and intval it too. Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/ Share on other sites More sharing options...
dragon_sa Posted March 1, 2012 Share Posted March 1, 2012 If you only want to use websafe colors can do this <?php $usercolor=somecolor; $colors=file(websafe_colors.txt); if (in_array($usercolor, $colors) { // your color exists } else { // error not a user color } ?> attached is the text fiel of colors 17675_.txt Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322631 Share on other sites More sharing options...
Monkuar Posted March 1, 2012 Author Share Posted March 1, 2012 If you only want to use websafe colors can do this <?php $usercolor=somecolor; $colors=file(websafe_colors.txt); foreach ($colors as $color) { if ($color==$usercolor) { // do something here it is correct } else { // error meassage here not in list } } ?> attached is the text fiel of colors Oh great idea is there a list of more? users can enter any hex amount, I want most most of them work, hmm I dont waant them to be limited. Your idea works great tho, cuz hackers can't do shit if doesn't check against the array lol epic Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322633 Share on other sites More sharing options...
dragon_sa Posted March 1, 2012 Share Posted March 1, 2012 will look for another list Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322634 Share on other sites More sharing options...
dragon_sa Posted March 1, 2012 Share Posted March 1, 2012 there are 16,777,216 colors to choose from so another option maybe is preg_match $hex_color = "#000111"; if(preg_match('/^#[a-f0-9]{6}$/i', $hex_color)){ echo "The color code is valid"; }else{ echo "Invalid color code"; } I edited the code to check the array above from the list <?php $usercolor=somecolor; $colors=file(websafe_colors.txt); if (in_array($usercolor, $colors) { // your color exists } else { // error not a user color } ?> Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322639 Share on other sites More sharing options...
Monkuar Posted March 1, 2012 Author Share Posted March 1, 2012 there are 16,777,216 colors to choose from so another option maybe is preg_match $hex_color = "#000111"; if(preg_match('/^#[a-f0-9]{6}$/i', $hex_color)){ echo "The color code is valid"; }else{ echo "Invalid color code"; } I edited the code to check the array above from the list <?php $usercolor=somecolor; $colors=file(websafe_colors.txt); if (in_array($usercolor, $colors) { // your color exists } else { // error not a user color } ?> Beautiful, man Topic Solved Love preg match and regex Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322640 Share on other sites More sharing options...
dragon_sa Posted March 1, 2012 Share Posted March 1, 2012 almost forgot I also have this which you might find useful 17676_.zip Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322641 Share on other sites More sharing options...
Monkuar Posted March 1, 2012 Author Share Posted March 1, 2012 almost forgot I also have this which you might find useful Oh ya i use that too! it's so lightweight! check my other topic, i got some more multi array stuff for ya lol Link to comment https://forums.phpfreaks.com/topic/258026-php-security-for-hex-color-codes/#findComment-1322642 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.