how to hide php version

Edit your php.ini file
expose_php = off

memcached example

<?php

$sql = "SELECT * FROM `processo` limit 5";

$mem = new Memcache;
$mem->connect('localhost');
$chave = sha1($sql);

// Buscamos o resultado na memória
$cache = $mem->get($chave);

// Verifica se o resultado não existe ou expirou
if ($cache === false) {
	echo "sem cache";
	// Executa a consulta novamente
	$resultado = mysql_fetch_array(mysql_query($sql));

	$tempo = 60; // 60 * 60; // 3600s
	$mem->set($chave, $resultado, MEMCACHE_COMPRESSED, 3600);
} else {
	echo "com cache";
	// A consulta está salva na memória ainda, então pegamos o resultado:
	$resultado = $cache;
}

print_r($resultado);

?>

sanitize function

<?
function sanitize($data)
{
	$data = trim($data); 
	if(get_magic_quotes_gpc())
	{
		$data = stripslashes($data);
	}
	$data = mysql_real_escape_string($data);
	return $data;
}

//example
$username = sanitize($_POST['username']);
?>

check domain

check domain availability in one php file http://github.com/leonardofaria/php-check-domain
<html>
<head>
	<title>whois</title>
</head>
<body>
	<form method="post">
		<label for="domain">Domain:</label>
		<input type="text" id="domain" name="domain" />
		<input type="submit"/>
	</form>

	<?php
		function get_server($domain){
			$whoisservers = array(
			array("ac","whois.nic.ac"),
			array("ac.cn","whois.cnnic.net.cn"),
			array("ac.jp","whois.nic.ad.jp"),
			array("ac.uk","whois.ja.net"),
			array("ad.jp","whois.nic.ad.jp"),
			array("adm.br","whois.nic.br"),
			array("adv.br","whois.nic.br"),
			array("aero","whois.information.aero"),
			array("ag","whois.nic.ag"),
			array("agr.br","whois.nic.br"),
			array("ah.cn","whois.cnnic.net.cn"),
			array("al","whois.ripe.net"),
			array("am.br","whois.nic.br"),
			array("arq.br","whois.nic.br"),
			array("at","whois.nic.at"),
			array("au","whois.aunic.net"),
			array("art.br","whois.nic.br"),
			array("as","whois.nic.as"),
			array("asn.au","whois.aunic.net"),
			array("ato.br","whois.nic.br"),
			array("be","whois.geektools.com"),
			array("bg","whois.digsys.bg"),
			array("bio.br","whois.nic.br"),
			array("biz","whois.biz"),
			array("bj.cn","whois.cnnic.net.cn"),
			array("bmd.br","whois.nic.br"),
			array("br","whois.registro.br"),
			array("ca","whois.cira.ca"),
			array("cc","whois.nic.cc"),
			array("cd","whois.cd"),
			array("ch","whois.nic.ch"),
			array("cim.br","whois.nic.br"),
			array("ck","whois.ck-nic.org.ck"),
			array("cl","whois.nic.cl"),
			array("cn","whois.cnnic.net.cn"),
			array("cng.br","whois.nic.br"),
			array("cnt.br","whois.nic.br"),
			array("com","whois.internic.net"),
			array("com.au","whois.aunic.net"),
			array("com.br","whois.nic.br"),
			array("com.cn","whois.cnnic.net.cn"),
			array("com.eg","whois.ripe.net"),
			array("com.hk","whois.hknic.net.hk"),
			array("com.mx","whois.nic.mx"),
			array("com.ru","whois.ripn.ru"),
			array("com.tw","whois.twnic.net"),
			array("conf.au","whois.aunic.net"),
			array("co.jp","whois.nic.ad.jp"),
			array("co.uk","whois.nic.uk"),
			array("cq.cn","whois.cnnic.net.cn"),
			array("csiro.au","whois.aunic.net"),
			array("cx","whois.nic.cx"),
			array("cz","whois.nic.cz"),
			array("de","whois.denic.de"),
			array("dk","whois.dk-hostmaster.dk"),
			array("ecn.br","whois.nic.br"),
			array("ee","whois.eenet.ee"),
			array("edu","whois.internic.net"),
			array("edu.au","whois.aunic.net"),
			array("edu.br","whois.nic.br"),
			array("eg","whois.ripe.net"),
			array("es","whois.ripe.net"),
			array("esp.br","whois.nic.br"),
			array("etc.br","whois.nic.br"),
			array("eti.br","whois.nic.br"),
			array("eun.eg","whois.ripe.net"),
			array("emu.id.au","whois.aunic.net"),
			array("eng.br","whois.nic.br"),
			array("far.br","whois.nic.br"),
			array("fi","whois.ripe.net"),
			array("fj","whois.usp.ac.fj"),
			array("fj.cn","whois.cnnic.net.cn"),
			array("fm.br","whois.nic.br"),
			array("fnd.br","whois.nic.br"),
			array("fo","whois.ripe.net"),
			array("fot.br","whois.nic.br"),
			array("fst.br","whois.nic.br"),
			array("fr","whois.nic.fr"),
			array("g12.br","whois.nic.br"),
			array("gd.cn","whois.cnnic.net.cn"),
			array("ge","whois.ripe.net"),
			array("ggf.br","whois.nic.br"),
			array("gl","whois.ripe.net"),
			array("gr","whois.ripe.net"),
			array("gr.jp","whois.nic.ad.jp"),
			array("gs","whois.adamsnames.tc"),
			array("gov","whois.nic.gov"),
			array("gs.cn","whois.cnnic.net.cn"),
			array("gov.au","whois.aunic.net"),
			array("gov.br","whois.nic.br"),
			array("gov.cn","whois.cnnic.net.cn"),
			array("gov.hk","whois.hknic.net.hk"),
			array("gob.mx","whois.nic.mx"),
			array("gs","whois.adamsnames.tc"),
			array("gz.cn","whois.cnnic.net.cn"),
			array("gx.cn","whois.cnnic.net.cn"),
			array("he.cn","whois.cnnic.net.cn"),
			array("ha.cn","whois.cnnic.net.cn"),
			array("hb.cn","whois.cnnic.net.cn"),
			array("hi.cn","whois.cnnic.net.cn"),
			array("hl.cn","whois.cnnic.net.cn"),
			array("hn.cn","whois.cnnic.net.cn"),
			array("hm","whois.registry.hm"),
			array("hk","whois.hknic.net.hk"),
			array("hk.cn","whois.cnnic.net.cn"),
			array("hu","whois.ripe.net"),
			array("id.au","whois.aunic.net"),
			array("ie","whois.domainregistry.ie"),
			array("ind.br","whois.nic.br"),
			array("imb.br","whois.nic.br"),
			array("inf.br","whois.nic.br"),
			array("info","whois.afilias.info"),
			array("info.au","whois.aunic.net"),
			array("it","whois.nic.it"),
			array("idv.tw","whois.twnic.net"),
			array("int","whois.iana.org"),
			array("is","whois.isnic.is"),
			array("il","whois.isoc.org.il"),
			array("jl.cn","whois.cnnic.net.cn"),
			array("jor.br","whois.nic.br"),
			array("jp","whois.nic.ad.jp"),
			array("js.cn","whois.cnnic.net.cn"),
			array("jx.cn","whois.cnnic.net.cn"),
			array("kr","whois.krnic.net"),
			array("la","whois.nic.la"),
			array("lel.br","whois.nic.br"),
			array("li","whois.nic.ch"),
			array("lk","whois.nic.lk"),
			array("ln.cn","whois.cnnic.net.cn"),
			array("lt","ns.litnet.lt"),
			array("lu","whois.dns.lu"),
			array("lv","whois.ripe.net"),
			array("ltd.uk","whois.nic.uk"),
			array("mat.br","whois.nic.br"),
			array("mc","whois.ripe.net"),
			array("med.br","whois.nic.br"),
			array("mil","whois.nic.mil"),
			array("mil.br","whois.nic.br"),
			array("mn","whois.nic.mn"),
			array("mo.cn","whois.cnnic.net.cn"),
			array("ms","whois.adamsnames.tc"),
			array("mus.br","whois.nic.br"),
			array("mx","whois.nic.mx"),
			array("name","whois.nic.name"),
			array("ne.jp","whois.nic.ad.jp"),
			array("net","whois.internic.net"),
			array("net.au","whois.aunic.net"),
			array("net.br","whois.nic.br"),
			array("net.cn","whois.cnnic.net.cn"),
			array("net.eg","whois.ripe.net"),
			array("net.hk","whois.hknic.net.hk"),
			array("net.lu","whois.dns.lu"),
			array("net.mx","whois.nic.mx"),
			array("net.uk","whois.nic.uk"),
			array("net.ru","whois.ripn.ru"),
			array("net.tw","whois.twnic.net"),
			array("nl","whois.domain-registry.nl"),
			array("nm.cn","whois.cnnic.net.cn"),
			array("no","whois.norid.no"),
			array("nom.br","whois.nic.br"),
			array("not.br","whois.nic.br"),
			array("ntr.br","whois.nic.br"),
			array("nx.cn","whois.cnnic.net.cn"),
			array("nz","whois.domainz.net.nz"),
			array("plc.uk","whois.nic.uk"),
			array("odo.br","whois.nic.br"),
			array("oop.br","whois.nic.br"),
			array("or.jp","whois.nic.ad.jp"),
			array("org","whois.internic.net"),
			array("org.au","whois.aunic.net"),
			array("org.br","whois.nic.br"),
			array("org.cn","whois.cnnic.net.cn"),
			array("org.hk","whois.hknic.net.hk"),
			array("org.lu","whois.dns.lu"),
			array("org.ru","whois.ripn.ru"),
			array("org.tw","whois.twnic.net"),
			array("org.uk","whois.nic.uk"),
			array("pl","nazgul.nask.waw.pl"),
			array("pp.ru","whois.ripn.ru"),
			array("ppg.br","whois.nic.br"),
			array("pro.br","whois.nic.br"),
			array("psi.br","whois.nic.br"),
			array("psc.br","whois.nic.br"),
			array("pt","whois.ripe.net"),
			array("qh.cn","whois.cnnic.net.cn"),
			array("qsl.br","whois.nic.br"),
			array("rec.br","whois.nic.br"),
			array("ro","whois.rotld.ro"),
			array("ru","whois.ripn.ru"),
			array("sc.cn","whois.cnnic.net.cn"),
			array("sd.cn","whois.cnnic.net.cn"),
			array("se","whois.nic-se.se"),
			array("sg","whois.nic.net.sg"),
			array("sh","whois.nic.sh"),
			array("sh.cn","whois.cnnic.net.cn"),
			array("si","whois.arnes.si"),
			array("sk","whois.ripe.net"),
			array("slg.br","whois.nic.br"),
			array("sm","whois.ripe.net"),
			array("sn.cn","whois.cnnic.net.cn"),
			array("srv.br","whois.nic.br"),
			array("st","whois.nic.st"),
			array("sx.cn","whois.cnnic.net.cn"),
			array("tc","whois.adamsnames.tc"),
			array("th","whois.nic.uk"),
			array("tj.cn","whois.cnnic.net.cn"),
			array("tmp.br","whois.nic.br"),
			array("to","whois.tonic.to"),
			array("tr","whois.ripe.net"),
			array("trd.br","whois.nic.br"),
			array("tur.br","whois.nic.br"),
			array("tv","whois.tv"),
			array("tv.br","whois.nic.br"),
			array("tw","whois.twnic.net"),
			array("tw.cn","whois.cnnic.net.cn"),
			array("uk","whois.thnic.net"),
			array("va","whois.ripe.net"),
			array("vet.br","whois.nic.br"),
			array("vg","whois.adamsnames.tc"),
			array("wattle.id.au","whois.aunic.net"),
			array("ws","whois.worldsite.ws"),
			array("xj.cn","whois.cnnic.net.cn"),
			array("xz.cn","whois.cnnic.net.cn"),
			array("yn.cn","whois.cnnic.net.cn"),
			array("zlg.br","whois.nic.br"),
			array("zj.cn","whois.cnnic.net.cn"),
			array("nu","whois.nic.nu"));

			for ($x=0;$x<count($whoisservers);$x++){
				$artld = $whoisservers[$x][0];
				$tldlen = intval(0 - strlen($artld));
				if (substr($domain, $tldlen) == $artld) { $whosrv = $whoisservers[$x][1]; }
			}
			return $whosrv;
		}
		
		function get_content($dom){
			$lusrv = get_server($dom);
			if (!$lusrv) return "";

			$fp = fsockopen($lusrv,43);
			fputs($fp, "$dom\r\n");
			$string="";
			while(!feof($fp)){
				$string.= fgets($fp,128);
			}
			fclose($fp);
			return $string;
		}
		
		function show_whois($dom){
			$string = get_content($dom);

			$reg = "/Whois Server: (.*?)\n/i";
			preg_match_all($reg, $string, $matches);
			$secondtry = $matches[1][0];

			if ($secondtry){
				$fp = fsockopen($secondtry,43);
				fputs($fp, "$dom\r\n");
				$string="";
				while(!feof($fp)){
					$string.=fgets($fp,128);
				}
				fclose($fp);
			}
			return $string;
		}
		
		function check_availability($dom){
			$string = get_content($dom);

			if (strpos($string, "No match for") > 0) {
				return true;
			} else {
				return false;
			}
		}
	
		if (isset($_POST['domain'])){ 
			if (check_availability($_POST['domain']) == true) { 
				echo $_POST['domain'] . " avaliable!";
			} else {
				echo $_POST['domain'] . " not avaliable!";
			}
			echo "<pre>".show_whois($_POST['domain'])."</pre>"; 
		}
	?>
</body>
</html>

wordpress: posting via php

via http://codex.wordpress.org/Function_Reference/wp_insert_post
<?php

	include "wp-blog-header.php";

	$my_post = array();
	$my_post['post_title'] = 'My post';
	$my_post['post_content'] = 'This is my post.';
	$my_post['post_status'] = 'publish';
	$my_post['post_author'] = 1;
	$my_post['post_category'] = array(13);

	$post_id = wp_insert_post($my_post);

	$post = get_post($post_id);
	wp_redirect($post->guid);

?>

display popular posts (most commented)

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
<?php } } ?>

display recent updated posts/pages

<?php
	$today = current_time('mysql', 1);
	$howMany = 5; // Number of posts you want to display
	if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
?>
<ul>
<?php
foreach ($recentposts as $post) {
	if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
	echo "<li><a href='".get_permalink($post->ID)."'>";
	the_title();
	echo '</a></li>';
}
?>
</ul>
<?php endif; ?>

display rss using wordpress

<?php 
	include_once(ABSPATH.WPINC.'/rss.php');
	wp_rss('http://mysite.com/feed', 5); 
?>

display recent posts

<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>

count search results

Results for: <?php $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; echo $key . ' (' . $count . ')'; wp_reset_query(); ?>

disable post revisions

in your /wp-config.php
<?php

define('WP_POST_REVISIONS', false);

?>

page x of y in wordpress

based on http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/
<?php

// paste in functions.php

function pagination($pages = '', $range = 4){
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '') {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages) {
             $pages = 1;
         }
     }   

     if(1 != $pages) {
         echo "Page ".$paged." of ".$pages;
     }
}

?>



<?php

// in your template 

if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } 

?>

wordpress $post variables

<?php
$post–>ID # ID of the current post.
$post–>post_category # Retrieves the ID of the post category.
$post–>post_parent # ID of the page parent. Useful for creating custom navigational elements.
$post–>post_title # Post Title
$post–>post_excerpt # Post excerpt
$post–>post_content # Retrieves all of the post content, along with any markup.
$post–>post_name # Retrieving the slug of a post.
$post–>guid # Post Url
$post–>post_author # ID of post author post_parent
$post–>post_type # Returns the type, page or post.
$post–>menu_order # Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
$post–>post_date # Retrieves the integer timestamp for when the post was published. The output can be customized. See the php.net date manual.
$post–>post_modified # Retrieves the integer timestamp for when the post was last modified.
$post–>post_status # Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
$post–>comment_count # Returns the number of comments, pings, and trackbacks for a given post.
?>

remove wordpress 3.1 admin bar

paste in functions.php
wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

clean wordpress gallery css

paste in your functions.php and get these elements: .gallery {} .gallery-item {} .gallery-icon {} .gallery-caption {}
<?php

function remove_gallery_style() {
  return "<div class='gallery'>";
}
add_filter('gallery_style', 'remove_gallery_style');

?>

wordpress post in an external page

<?php
   require('WORDPRES_PATH/wp-load.php');
   global $query_string;
   $qstring_array = array();
   parse_str($query_string,  $qstring_array);
   $args = array_merge($args,$qstring_array);
   query_posts($args) ;
 
 if (have_posts()) : while (have_posts()) : the_post();
         echo "Title :";
         the_title();
        echo "<br/>";
        echo "Author: ";
        the_author();
       echo "<br/><br/>";
      endwhile;
    else:
       echo "No posts";
    endif;
  ?>

get server uptime with php

<?php 
// format the uptime in case the browser doesn't support dhtml/javascript
// static uptime string
function format_uptime($seconds) {
  $secs = intval($seconds % 60);
  $mins = intval($seconds / 60 % 60);
  $hours = intval($seconds / 3600 % 24);
  $days = intval($seconds / 86400);
  
  if ($days > 0) {
    $uptimeString .= $days;
    $uptimeString .= (($days == 1) ? " day" : " days");
  }
  if ($hours > 0) {
    $uptimeString .= (($days > 0) ? ", " : "") . $hours;
    $uptimeString .= (($hours == 1) ? " hour" : " hours");
  }
  if ($mins > 0) {
    $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
    $uptimeString .= (($mins == 1) ? " minute" : " minutes");
  }
  if ($secs > 0) {
    $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs;
    $uptimeString .= (($secs == 1) ? " second" : " seconds");
  }
  return $uptimeString;
}

// read in the uptime (using exec)
$uptime = exec("cat /proc/uptime");
$uptime = split(" ",$uptime);
$uptimeSecs = $uptime[0];

// get the static uptime
$staticUptime = "Uptime: ".format_uptime($uptimeSecs);
?>
<html>
<head>
<script language="javascript">
<!--
var upSeconds=<?php echo $uptimeSecs; ?>;
function doUptime() {
var uptimeString = "Uptime: ";
var secs = parseInt(upSeconds % 60);
var mins = parseInt(upSeconds / 60 % 60);
var hours = parseInt(upSeconds / 3600 % 24);
var days = parseInt(upSeconds / 86400);
if (days > 0) {
  uptimeString += days;
  uptimeString += ((days == 1) ? " day" : " days");
}
if (hours > 0) {
  uptimeString += ((days > 0) ? ", " : "") + hours;
  uptimeString += ((hours == 1) ? " hour" : " hours");
}
if (mins > 0) {
  uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins;
  uptimeString += ((mins == 1) ? " minute" : " minutes");
}
if (secs > 0) {
  uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") + secs;
  uptimeString += ((secs == 1) ? " second" : " seconds");
}
var span_el = document.getElementById("uptime");
var replaceWith = document.createTextNode(uptimeString);
span_el.replaceChild(replaceWith, span_el.childNodes[0]);
upSeconds++;
setTimeout("doUptime()",1000);
}
// -->

</script>
</head>
<body onLoad="doUptime();">

<div id="uptime"><?php echo $staticUptime; ?></div>

</body>
</html>

minify css using php

<?php
function optimizeCSS($text) {
	$text = preg_replace('~/\*.*?\*/~s','',$text); // comments
	$text = preg_replace('/[\r\n\t]/','',$text); // newlines and tabs
	$text = preg_replace('/ *(;|\:|\{|\}) */','$1',$text); // space before/after colons, semicolons and braces
	$text = str_replace(';}','}',$text); // remove final semicolon
	$text = trim($text);
	return $text;
}
?>

find php.ini path

php -i | grep php.ini

wordpress popular posts

<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>
Displaying Code 1 - 20 of 30 in total