Archive

Archive for the ‘PHP’ Category

Serialize error at offset – how to fix

June 29, 2010 1 comment

Recently I have been storing some stuff in mysql using serialize which is very useful at storing things like arrays in a DB safely without anything corrupting. However recently I have been getting the error message “unserialize() [function.unserialize]: Error at offset 2 of …”.. after looking around at some strange and confusing solutions.. simple code fixed it:


$safeformysqldata = base64_encode(serialize($cutearrayhere));

and to get it back out of the DB:


unserialize(base64_decode($row['mydata']));

I hope this helps someone 🙂

Categories: mysql, PHP

How to send UTF8 encoded mail in php

May 24, 2010 Leave a comment

After some of my foreign language emails were coming out strange I had a look around to find a way to send out mails through the mail function in PHP.. here it is:


$headers = "MIME-Version: 1.0" . "\r\n" . "Content-type: text/plain; charset=UTF-8" . "\r\n";
mail($to, "=?UTF-8?B?".base64_encode($subject)."?=", $message, $headers);

You can use this method to utf encode the sender/recipient/message. Be sure to change text/plain to text/html if you are sending html in a mail 🙂

Categories: PHP

Stop mails sent from PHP being marked as spam by editing SPF records!

April 4, 2010 2 comments

So I just moved servers and my inbound mail traffic was going down each day.. I took a look at the mail headers and got this:

Return-Path:
Received-SPF: neutral (google.com: 66.x.x.x is neither permitted nor denied by best guess record for domain of support@thaifriendly.com) client-ip=66.x.x.x;

I managed to change this from a neutral to a pass by adding a return path to my mail command in PHP:

mail($toemail,$subject,$message,$headers," -f support@mydomain.com");

.. and by logging into namecheap and adding a TXT record to my domain (Login -> select domain -> All host records) for host name add @ for IP address/URL add “v=spf1 a:my.mailserver.ip.address ~all” and selecting “TXT Record”. After saving changes and sending a test mail google now happily reports:

Return-Path:
Received-SPF: pass (google.com: domain of support@mydomain.com designates my.mailserver.ip.address as permitted sender) client-ip=my.mailserver.ip.address;

It took me a few searches to get all this information together, particularily editing host records in namecheap too, I hope it helps someone!

Categories: DNS, PHP Tags: ,

Recursive file and directory delete in PHP

March 26, 2010 Leave a comment

Some code that takes a directory deletes all its files/folders – everything will go!

function DELETE_RECURSIVE_DIRS($dirname)
{ // recursive function to delete
// all subdirectories and contents:
if(is_dir($dirname))$dir_handle=opendir($dirname);
while($file=readdir($dir_handle))
{
if($file!="." && $file!="..")
{
if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file);
else DELETE_RECURSIVE_DIRS($dirname."/".$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}

Categories: PHP