Skip to content

Instantly share code, notes, and snippets.

<?php
function duration($len){
$hr = floor($len / 60 / 60);
$min = floor($len / 60);
$sec = floor($len % 60);
return array('hr' => $hr, 'min' => $min,'sec' => $sec);
}
$dur = 28853.45;
$totals = duration($dur);
@khoand0000
khoand0000 / blacklist_lookup.php
Created September 11, 2014 15:28
http://phpsnips.com/544/Blacklist-Lookup#.VBG-zfmSySo This blacklist lookup function is a modification of this snippet. The difference is, that it returns if it suspects an ip is a spammer. If 50% or more of the lookups think your a spammer, the function returns true otherwise false.
<?php
function blacklist($ip){
$listed = true;
$dnsbl_lookup = array(
"dnsbl-1.uceprotect.net",
"dnsbl-2.uceprotect.net",
"dnsbl-3.uceprotect.net",
"dnsbl.dronebl.org",
"dnsbl.sorbs.net",
"zen.spamhaus.org"
<?php
function key_implode($glue, $array){
$keys = array_keys($array);
return implode($glue, $keys);
}
$arr = array("cats" => 4, "dogs" => 8, "fish" =>10);
echo key_implode(",", $arr);
?>
<?php
// set the variable to 0, it'll matter only if the cookie for the variable is not set
$visitCounter = 0;
// if cookie is set for the variable, it'll go to $visitCounter and get added by 1; otherwise it'll show 0 for tha variable
if(isset($_COOKIE['visitCounter'])){
$visitCounter = $_COOKIE['visitCounter'];
$visitCounter ++;
}
// if the last visist cookie is set, it'll pass the value to $lastVisit, and it'll be displayed below;
if(isset($_COOKIE['lastVisit'])){
<?php
// +----------------------------------------------------------------------+
// | Akelos PHP Application Framework |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.com/ |
// | Released under the GNU Lesser General Public License |
// +----------------------------------------------------------------------+
// | You should have received the following files along with this library |
// | - COPYRIGHT (Additional copyright notice) |
@khoand0000
khoand0000 / cache_generated_images.php
Created September 11, 2014 15:36
http://phpsnips.com/473/Cache-PHP-Generated-Images#.VBG--vmSySo Stops PHP from sending a fresh version of an image if the browser already has it in its cache.
<?php
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
// Set to expire in 2 days
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
// if the browser has a cached version of this image, send 304
header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
exit;
}
@khoand0000
khoand0000 / escape_user_input_before_preg_replace.php
Created September 11, 2014 15:38
http://phpsnips.com/603/Escape-user-input-before-preg_replace#.VBG_A_mSySo Sometimes you need to do preg_replace on user input. Don't forget to escape all special characters or you'll get funny results.
<?php
$input = str_replace(array('^', '.', '|', '(', ')', '[', ']', '*', '+', '?', '{', '}', '$' ),
array('\^', '\.', '\|', '\(', '\)', '\[', '\]', '\*', '\+', '\?', '\{', '\}', '\$' ), $input);
<?php
$regex = "/([a-z0-9_]+|[a-z0-9_]+\.[a-z0-9_]+)@(([a-z0-9]|[a-z0-9]+\.[a-z0-9]+)+\.([a-z]{2,4}))/i";
// usage
if(!preg_match($regex, $email)) {
echo "Invalid email address";
}
else {
@khoand0000
khoand0000 / check_java_install_or_not.html
Created September 12, 2014 16:32
check java install or not
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<script src="http://www.java.com/js/deployJava.js"></script>
</head>
<script>
function onLoad() {
if (deployJava.getJREs().length > 0) {
document.getElementById('install').style.display = 'block';
// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))