Serialize error at offset – how to fix
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 🙂
Queries using datetime in mysql to search by month
I recently started working with a sales database and rather than using my personal favorite timestamp column type they used datetime – it took me a couple googles before I finally found the syntax to do queries to find date ranges:
SELECT * FROM `sales` WHERE MONTH(column_name) = 6 AND DAY(column_name) < 10;
Will get all the sales before the 10th in the month of June and ANY year 😀 not so great if you have more than a years data hehe 😛 but now you know how it works and to find year is YEAR(column_name)
If you wanted to get all records that were for this year you can use:
Select * from table_name where field_name > YEAR(CURDATE());
How to send UTF8 encoded mail in php
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 🙂
Fix for X-Authentication-Warning: .. apache set sender to using -f
Like many people I love to send mail from PHP using apache! But when I do a tail -f of my mail logs I can see many “X-Authentication-Warning”‘s. I don’t like to see warnings in my logs and I read rumors on the net that this also affects the deliverability of your mail.. which is bad!
Anyway to fix this you just need to add apache as a trusted user in the trusted users list at /etc/mail/trusted-users
After opening that file and adding apache on its own line the error message vanishes 😀
Find the size of the current working directory in linux
Its not so obvious how to find out the size of the directory and all subdirectories in linux but here is the command for quick reference 😀
du -sch
This will tell you the disk space used by the current working directory and all its sub-directories in a human readable format!
Restart the firewall on CentOS/Redhat
Another simple one 😛
service iptables restart
Simply restarts iptables on centos or redhat! I wonder how my shell client stays connected when it does this though..
Start/stop and restart mysql server
So simple I should know this by now 🙂
/etc/init.d/mysqld restart
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
That works for RedHat, this works for Ubuntu/Debian:
/etc/init.d/mysql restart
Unzip all zip files into current directory on linux using find
I needed a quick and dirty command to unzip all the .zip files in the current working directory on my redhat box and this was it!
find *.zip -exec unzip {} \;
Enjoy 😀
EDIT: Same but for rar files, auto renames the files if they exist and uses rar name as the directory:
find *.rar -exec unrar e -ad -or {} \;
Use wget to download from hotfile – automated!
To automatically download from Hotfile using Wget you can do the following:
wget --save-cookies /path/to/hotfilecookie --post-data "returnto=%2F&user=1234567&pass=yourpass&=Login" -O - http://www.hotfile.com/login.php > /dev/null
This will save your login to a cookie on your server and then we can do:
wget -c --load-cookies /path/to/hotfilecookie -i /path/to/inputfile -o /path/to/downloadlog -nc -b
This will set wget running in the background logging its progress to a download log. It will skip downloading any files that already exist, and it will read all the files to download from a file called inputfile. I hope this helps someone 🙂
Stop mails sent from PHP being marked as spam by editing SPF records!
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!