Jump to content

PHP Security for HEX COLOR CODES


Monkuar

Recommended Posts

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

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

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

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
  }
?>

 

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

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.