PHP Samples, Code & Tips

Thursday, December 08, 2005

Protecting important files from visitors to your website.

Choose a hosting service that has a writable base directory, with a web root folder underneath it.

/home/yoursite/ <--base directory
/home/yoursite/public_html/ <-- web root

I store files that I want to control access to, below my web root folder. An example might be storing fonts that you use with the GD Library (an image library - see in /home/yoursite/fonts/ to protect fonts you have purchased from being stolen.

You should keep in mind other people using the same shared hosting folder can have access rights to this directory, so use chmod to make sure you are the only user who can access the folder.

Friday, November 11, 2005

Using PHP, Atom API and CURL to Post to Blogger

Ok, since this is my first 'real' post, it seems fitting to provide an example of how to use Atom API and PHP to post to your blog at Blogger.

Before you can use this script, you need a blogger acocunt.
They're free - go to
You need to have a blog that you can post to.
You need to know the blogid of the blog you want to post to.
To get the blogid, go to and log in to your blogger account.
There will be a list of blogs.
When you click on a blog name, it will go to a url like :
The blogID portion is what you use for your $blog_id- in the above example it is 1234567
To carry out other blogger tasks, refer to the Atom API Documentation forBlogger:*/

// Set the date of your post
$issued=gmdate("Y-m-d\TH:i:s\Z", time());
// Set the title of your post
// Set the body text of your post.
$body="This post, sent using the atom api and php curl modified by Daniel at";
// This needs to be changed to the blogID of the blog// you want to post to (as discussed at the top of this script)
// You must know your username and password
// to be able to post to your blog.
$your_username="username"; //change this to your blogger login username$your_password="password"; // change this to your blogger login password

// This is the xml message that contains the information you are posting
// to your blog
$content = "\r\n"
. "\r\n"
. "".$title."\r\n"
. "".$issued."\r\n"
. "Your client's namehere.\r\n"
. "\r\n"
. "
. "
\r\n" . "

// This is the custom header that needs to be sent to post to your blog.
$headers = array( "Content-type: application/atom+xml" );

// Use curl to post to your blog.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "".$blog_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $your_username.':'.$your_password);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

$data = curl_exec($ch);

if (curl_errno($ch)) {
print curl_error($ch);
} else {

// $data contains the result of the post...
echo $data;


Wednesday, November 09, 2005

Welcome to


Welcome to

I intend to use this site to publish any php sample code (and possibly encourage others to also contribute.)

My first script is going to be demonstrating how to post an entry to blogger using curl - but first some testing will be required!