Skip to content

Instantly share code, notes, and snippets.

@phpdistiller
phpdistiller / sanitize_database_inputs.php
Last active January 1, 2016 00:38
This snippet sanitizes database inputs.
<?php
// Source : http://css-tricks.com/snippets/php/sanitize-database-inputs/
// Function for stripping out malicious bits
function cleanInput($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
@phpdistiller
phpdistiller / detect_location_by_ip.php
Last active January 1, 2016 00:29
This snippet detects the location of a specific IP. If the location isn't found, UNKNOWN is returned.
<?php
// Source : http://www.catswhocode.com/blog/snippets/detect-location-by-ip
function detect_city($ip) {
$default = 'UNKNOWN';
if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
$ip = '8.8.8.8';
@phpdistiller
phpdistiller / get_tweets_by_hashtag.php
Last active January 1, 2016 00:29
This snippet gets all tweets of a specific hashtag.
<?php
// Source : http://www.inkplant.com/code/get-twitter-posts-by-hashtag.php
function getTweets($hash_tag) {
$url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ;
echo "<p>Connecting to <strong>$url</strong> ...</p>";
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
@phpdistiller
phpdistiller / detect_browser_language.php
Last active January 1, 2016 00:29
This snippet detects the browser language and provides $available_languages as an array('en', 'fr', 'es').
<?php
// Source : http://snipplr.com/view/12631/detect-browser-language/
function get_client_language($available_languages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $available_languages)){