Categories

Arithmetic captcha

Captcha’s are a necessary part of websites nowadays unfortunately, this is a slightly less painful variation than the usual random hard to read characters

session_start();
header("Content-type: image/png");
$width = 120;
$height = 30;
$im = @imagecreate($width, $height)
    or die("Cannot Initialize new GD image stream");
imagecolorallocate($im, 247, 250, 233);
$noise_color = imagecolorallocate($im, 253, 175, 90);
	for( $i=0; $i<($width*$height)/3; $i++ ) {
         imagefilledellipse($im, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
      }
      /* generate random lines in background */
      for( $i=0; $i<($width*$height)/120; $i++ ) {
         imageline($im, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
      }
$text_color = imagecolorallocate($im, 175, 102, 43);


$action = array('+', '*', '-');
shuffle($action);
if($action[0] == '+') {
	$a = rand(1, 9);
	$b = rand(1, 9);
	$_SESSION['code'] = $a + $b;
}
elseif($action[0] == '*') {
	$a = rand(1, 9);
	$b = rand(1, 9);
	$_SESSION['code'] = $a * $b;
}
elseif($action[0] == '-') {
	$b = rand(1, 9);
	$c = $b+1;
	$a = rand($c, 9);
	
	$_SESSION['code'] = $a - $b;
}

$text = $a.$action[0].$b;

for($j = 0; $j < strlen($text); $j++) {
	$angle = rand(-20, 20);
	imagettftext($im, 18, $angle, 29+($j*23), (18+(($height-18)/2)), $text_color, './WC_Wunderbach_Rough.ttf', $text[$j] );
}
imagepng($im);
imagedestroy($im);

//usage : save this snippet as captcha.php file and use it that
//point it as image source 
//<img src="captcha.php" />
//to check is it passed correctly, do following
//if(isset($_SESSION['code']) && ($_POST['captcha'] == $_SESSION['code'])) 
//its needed to check is $_SESSION['code'] is set, because if somebody block
//the image loading, the check ($_POST['captcha'] == $_SESSION['code']) will 
//return false positive if the form is sent with empty captcha field

Sanitize data

This was user submitted example

function clean($string,$type){
        switch ($type){
            case "int":
                return filter_var(filter_var($string, FILTER_SANITIZE_NUMBER_INT), FILTER_VALIDATE_INT);
            case "string":
                return filter_var($string, FILTER_SANITIZE_STRING);
            case "sql":
                return mysql_real_escape_string($string);
                break;
            case "email":
                return filter_var(filter_var($string, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
                break;
            case "url":
                return filter_var(filter_var($string, FILTER_SANITIZE_URL), FILTER_VALIDATE_URL);
                break;
            case "ip":
                return filter_var(filter_var($string, FILTER_SANITIZE_IP), FILTER_VALIDATE_IP);
                break;
            default:
                return 0;
                break;
        }
    }

Twitter RSS example

This requires file_get_contents to be enabled on your web server, this is frequently disabled on many hosts

<?php
					try {
						$feed = 'http://twitter.com/statuses/user_timeline/465363463.rss';
						$xml = new SimpleXMLElement(file_get_contents($feed));
						for($i=0; $i < 5; $i++){
							print '<p class="feeditem"><a href="'.$xml->channel->item[$i]->link.'">'.ltrim($xml->channel->item[$i]->description, 'shuffleInteract:').'</a><br /><span>Posted on '.date('F jS, Y', strtotime($xml->channel->item[$i]->pubDate)).'</span></p>';
						}
					} catch(Exception $e){}
				?>

Generate HTML Tag

/**
 * Generate HTML Tag
 *
 * @param	string	tag
 * @param	string	content
 * @param	array   tag attributes - array('class' => 'active')
 * @return	string	compiled HTML tag
 */
function tag($tag = '', $content = '', $atts = array()) {
	$str = '<' . $tag;
	foreach ($atts as $att => $value) {
		$str .= ' ' . $att . '="' . $value . '"';
	}
	$str .= '>' . $content . '</' . $tag . '>';
	return $str;
}

Append a string to a file

    $fp = fopen("filename", "a");  
    if($fp) 
{  
        fwrite($fp, "aString");  
        fclose($fp);  
    }