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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.