mrkeck Sharing the Love


PHP sudo as user with no password required

Was getting: "sudo: no tty present and no askpass program specified php"
When running:
echo shell_exec("cd /var/www/html/repo/;sudo -u ec2-user /usr/bin/git pull 2>&1");

sudo vim /etc/sudoers

sudo usermod -a -G www ec2-user
sudo usermod -a -G www apache
Puts users ec2-user and apache are part of group www

May have to restart server

Filed under: EC2, PHP No Comments

json_decode not working from database query

Short answer:


Had some JSON in a database that I wanted to use in my PHP code, but it keep coming up NULL.
Did a var_dump and the number of characters didn't match. Tried all sorts of trims and tried converting to utf8.
Ended up calling html_entity_decode() on the JSON string, then json_decode worked perfectly!

Filed under: PHP No Comments

Make the ellipsis clickable in WordPress

At the end of the excerpt summary it has a [...] but it doesn't link to anything. The code below makes it a link and changes [...] to [Read More].

Add this code to the functions.php

function replace_ellip($content) {
  return str_replace('[…]',
    '<a href="'. get_permalink() .'" class="more-link">[Read More]</a>',
add_filter('the_excerpt', 'replace_ellip');
Filed under: PHP, wordpress No Comments

Amazon S3 Upload Directory Including Itself Using PHP SDK

//Upload directory into s3 directory
//Ex. 2013/01/02/test.jpg
//$bucket = 'myBucket';
//$dirToUpload = '2013';
//$uploadDir = '2013';
//$client->uploadDirectory($dirToUpload, $bucket . '/' . $uploadDir);
//In S3 you have 2013/01/02/test.jpg
//If you didn't add the /directory to the bucket would get 01/02/test.jpg
$bucket = 'myBucket';
$dirToUpload = 'C:/someDir/localDir';
$uploadDir = 's3Dir';  //Could be the same as $dirToUpload but you can change name too
$response = $client->uploadDirectory($dirToUpload, $bucket . '/' . $uploadDir);
Filed under: Amazon, PHP, S3 No Comments

PHP use / not \

When referencing folders use / instead of \
/ doesn't have to be escaped and works perfectly.

Filed under: Convention, PHP No Comments

Youtube Like IDs

Shorter IDs = Shorter URLs =>

Light-weight PHP class that converts an integer into alphanumeric and vice versa

One-to-one conversion with no collisions, no database, and no dependencies


	include ('weeId.class.php');
	$weeId = new weeId;
	echo $weeId->numberToWord(1000000);		//hfQ8
	echo $weeId->wordToNumber('hfQ8');		//1000000

Get weeId

Filed under: GitHub, PHP, YouTube No Comments

PHP int limit

Integer limit depends on the Architecture of the processor.

32-bit version: 2,147,483,647
64-bit version: 9,000,000,000,000,000,000~

I noticed once I reached large numbers, the modulus function gave incorrect results, and it was because I was using the 32-bit version of PHP

Filed under: PHP No Comments

Default Timezone CodeIgniter

Add this to index.php in the root of the site under the Error Reporting section

//Change America/New_York to your timezone
Filed under: codeIgniter, PHP No Comments

Upgrade PHP on Hostgator

On a Shared Plan:
Edit .htaccess


# Use PHP 5.3
AddType application/x-httpd-php53 .php

That's it!

Filed under: hostGator, PHP No Comments

PHP throw new Exception does not work

"throw new Exception" does not work if you have display_errors turned off in php.ini

Filed under: hostGator, PHP No Comments