Archive

Archive for the ‘php’ Category

PHP Development Tools that Make Money – Wordcamp Phoneix 2012

February 26th, 2012 3 comments
Categories: php Tags:

Why you should Turn off Output buffering in php when developing code for others…

September 29th, 2011 2 comments

A few times now I have developed code for my clients, and when I send it to them, they get an error dealing with output already being sent when something attempts to send a header… You know the classic:

Warning: Cannot modify header information – headers already sent by (output started at /home/a/public_html/wp-admin/includes/plugin.php:1721) in /home/a/public_html/wp-includes/pluggable.php on line 934

This is caused by output having already been sent to the browser, and the headers have to go before the output. Php is smart enough to clear the output buffer if possible before sending headers… So if the output buffer hasn’t been sent to the browser yet because output buffering is enabled php will clear the buffer, and send the header. But if output buffering is disabled, you get an error… And something won’t work right…

So, open up your php.ini file and set output_buffering = Off.

Cheers!

Joey

Categories: php Tags:

Getting the profile picture for an email address via gravatar!!

July 4th, 2011 No comments

A Gravatar is a picture associated with an email address. It is a brilliant system that allows anyone anywhere to create a photo to be associated with an email address they have (gravatar.com verifies the email address before allowing them to edit the picture). And then makes this photo publicly accessible to anyone! (See: http://en.gravatar.com/site/implement/hash/) for full details).

All you do to get the image associated with an email address is compose a url.

Just take the email address, trim any whitespace off the ends, make it all lower case, md5 it, and the url is:
http://www.gravatar.com/avatar/hash

So, php code to do this is:

$emailAddress = 'Bob.cOOl@example.com';
$url = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($emailAddress)));

You can also add a size parameter to the url to get a specific size… i.e.

$emailAddress = 'Bob.cOOl@example.com';
$size = 177;
$url = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($emailAddress))) . '?size=' . $size;

Awesome huh?

Check out this link for more you can do: http://en.gravatar.com/site/implement/hash/

Categories: php Tags:

Escaping Strings for use in Javascript from PHP

June 20th, 2011 No comments

I ran into a problem today on an application where building some inline javascript (which btw I NEVER recommend, I inherited this from someone else…   Write javascript that takes an object as a param and use json_encode instead…)…  And some data in our database had carriage returns…  Which was breaking our javascript.  I wanted to escape the string before putting it in the javascript but I didn’t want to write my own function.  After doing some searching, I discovered that json_encode will return a valid javascript string if you pass it a string instead of an array!!!

Note:  json_encode will put double quotes on the end of your string, so to fix this, you need to strip them… Use preg_replace:


function javascript_encode($toEncode){
  return preg_replace("/^\"(.*)\"$/", "$1", json_encode($toEncode));
}

Cheers!

Joey

Categories: javascript, php Tags:

How do I download an excel file from php?

June 16th, 2011 No comments

I must do this every 6 months and ALWAYS have to research it…
Instead of just setting the content-type as an octet-stream like usual, specify the mime type for Excel, and the browser will open it in the right program.  This isn’t JUST Ms Office, this content-type will tell the browser to open it in Open Office as well.

header(“Content-type: application/vnd.ms-excel “);
header(“Content-Disposition: attachment; filename=\”” . preg_replace(“/[^a-zA-Z0-9\s\-]/”, “”, $event_key) . “.csv\””);

Categories: php Tags:

Properly Preparing A String for a csv file in php

June 16th, 2011 No comments

The magic thing to do is to escape double quotes by making them double double quotes…  And enclosing the string in quotes.  Pretty easy…

 echo ‘”‘ . str_replace(“\””, “\”\””, $data) . ‘”, ‘;

Categories: php Tags:

Never ever ever make a php config file not a php file…

November 8th, 2010 1 comment

I was using a php sdk today where the config file is NOT a php file, but rather a file named .cfg…

The problem?  You have to restrict access to this file with a .htaccess file to prevent someone from reading it.  Whereas, if it were a php file, not only would you not have to prevent access to it, you also don’t have to write your own parser.

Lesson…  Don’t use a cfg file, where a php config file will do.

Categories: http, php, php5, web Tags:

HttpFox Rocks!!! (Better than Tamper Data)

October 26th, 2010 No comments

I used to rely on the Firefox Plugin Tamper Data to get the nitty gritty deatails of http requests.  Firebug has a feature that is similar as well (the net tab).  But I find it buggy.  Recently, a fellow developer introduced me to the HttpFox Firefox plug-in, and it is awesome!

I now use it all the time.

The only caveat,  is only start it when you need it, and stop it when you are done.  Leaving HttpFox running and capturing for long periods of time slows Firefox a lot.

Check it out:  http://code.google.com/p/httpfox/

Categories: debugging, firefox, http, php, web Tags: