Tag Archive for php

Only allow AJAX for a HTTP Request

// If the request did not come from AJAX, exit:
if($_SERVER['HTTP_X_REQUESTED_WITH'] !='XMLHttpRequest'){
	exit;
}

source

Chat system example

<?php
/**
 * Add a message to the chat
 * @param Redis $redis Redis connection handler
 * @param String $message message to add
 * @return void
 */
function addMessage($redis, $message) {
	$redis->lPush("chat", $message);
	if($redis->lLen("chat") > 100) {
		$redis->rPop("chat");
	}
}

source

Remove Non Numeric Characters

function  remove_non_numeric($string)
{
	return preg_replace('/D/', '', $string)
}

// OR

$result = ereg_replace( '[^0-9]+', '', $source );

source

Resize image

function crear_miniatura($name,$filename,$new_w,$new_h,$carpeta){
	$system=explode(".",$_FILES['Filedata']['name']);
	if (preg_match("/jpg|jpeg|JPG|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
	if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}

	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	$proporcion = $old_x/$old_y;
	$proporcion_correcta = $new_w/$new_h;
	if ($proporcion >= $proporcion_correcta) {
		$recorte_h=$old_y;
		$recorte_w=$old_y*$proporcion_correcta;
	}else{
		// si es vertical
		$recorte_h=$old_y;
		$recorte_w=$old_y*$proporcion_correcta;
		//$recorte_w=$old_x;
		//$recorte_h=$old_x/$proporcion_correcta;
	}
	$x_recorte = $old_x/2 - $recorte_w/2;
	$y_recorte = $old_y/2 - $recorte_h/2;
	$dst_img=ImageCreateTrueColor($new_w,$new_h);
	imagecopyresampled($dst_img,$src_img,0,0,$x_recorte,$y_recorte,$new_w,$new_h,$recorte_w,$recorte_h); 
	imagejpeg($dst_img,$carpeta.$filename,85);
	imagedestroy($dst_img);
	imagedestroy($src_img);
}


	crear_miniatura($_FILES['Filedata']['tmp_name'],$nuevo_nom,400,300,"../media/");
	crear_miniatura($_FILES['Filedata']['tmp_name'],"pq_".$nuevo_nom,40,40,"../media/");

source

Dummy Image Random

<img src="<a href="http://dummyimage.com/620x300/<?php" >http://dummyimage.com/620x300/<?php</a> echo dechex(rand(111,999)); ?>/fff" />

source

Full url without variables

<?
function full_url()
 	{
 $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
 $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
 $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
 $address = $protocol . "<a href="://"" >://"</a> . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
 $parseUrl = parse_url(trim($address));
 return $parseUrl['scheme'].'<a href="://'.$parseUrl['host'].$parseUrl['port'].$parseUrl['path'];" >://'.$parseUrl['host'].$parseUrl['port'].$parseUrl['path'];</a> 
 	}
?>

source

Deploying php applications

#Get last code
svn export svn<a href="://path/to/repository/tags/RELEASE_VERSION" >://path/to/repository/tags/RELEASE_VERSION</a> /usr/local/application/releases/TIMESTAMP
stop services (Apache, daemons)
run unlink /usr/local/application/current && ln -s /usr/local/application/releases/TIMESTAMP /usr/local/application/current
run ln -s /usr/local/application/var /usr/local/application/releases/TIMESTAMP/var
run /usr/local/application/current/scripts/migrate.php
start services

source

URI Segment

<?php

function segment($index) {
	$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

	$path = (strpos($path, '/') == 0) ? ltrim($path, '/') : $path ;

	$path_chunked = explode('/', $path);

	return ($index > count($path_chunked) ) ? FALSE : $path_chunked[$index-1];
}

?>

source

objects into array

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

source

WordPress Archives Basic

<ul><?php wp_get_archives('type=monthly'); ?></ul>

source