mrkeck Sharing the Love

17Jun/160

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
%www ALL = (ALL) NOPASSWD: ALL

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
13Jan/150

json_decode not working from database query

Short answer:

json_decode(html_entity_decode($json));

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
19Jan/140

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>',
      $content
  );
}
add_filter('the_excerpt', 'replace_ellip');
Filed under: PHP, wordpress No Comments
12Jul/130

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
30Apr/130

PHP use / not \

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

Filed under: Convention, PHP No Comments
26Apr/130

Youtube Like IDs

Shorter IDs = Shorter URLs

weeId.com/id/1000000 => weeId.com/id/hfQ8

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

Usage

	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
22Apr/130

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
22Apr/130

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
date_default_timezone_set('America/New_York');
Filed under: codeIgniter, PHP No Comments
22Apr/130

Upgrade PHP on Hostgator

On a Shared Plan:
Edit .htaccess

Add:

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

That's it!

Filed under: hostGator, PHP No Comments
22Apr/130

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