modern-web-design-and-development

(Brent) #1
1 $color = 'white';
2 $background = 'black';
3 if(isset($_GET['color']) && isvalid($_GET['color'])){

(^4) $color = $_GET['color'];
(^5) if(ishexcolor($color)){
(^6) $color = '#'.$color;
(^7) }
8 }
9 if(isset($_GET['background']) && isvalid($_GET['background'])){
(^10) $background = $_GET['background'];
(^11) if(ishexcolor($background)){
(^12) $background = '#'.$background;
(^13) }
14 }
15 function isvalid($col){
(^16) // only allow for values that contain a to z or 0 to 9
(^17) return preg_match('/^[a-z0-9]+$/',$col);
18 }
19 function ishexcolor($col){
(^20) // checks if the string is 3 or 6 characters
(^21) if(strlen($col)==3 || strlen($col)==6){
(^22) // checks if the string only contains a to f or 0 to 9
(^23) return preg_match('/^[a-f0-9]+$/',$col);
(^24) }
25 }
This allows for http://example.com/test.php?
color=red&background=pink or http://example.com/test.php?
color=369&background=69c or http://example.com/test.php?
color=fc6&background=449933, but not for http://example.com/

Free download pdf