Monday 15 November 2010

PHP Mail By Different SMTP Server

Senareo;

I've just had to upload a php website to an IIS 6 web server. Amazingly, everything worked apart from the ability to send email. I informed the hosing provider and they set up a different SMTP server to send email from. I now needed to tell my website where the different smtp server resided.

The Solution

I used the ini_set command in php. It allowed me to override the default php setup, and add it to a config file. I believe this can also be achieved by .htaccess, however having the setting in a config file in php suited me better.

<?

ini_set('SMTP','yourmailserver.com');

?>

Note: The ideal solutions would have been to have php.ini changed, but this solution works for now.

Friday 2 April 2010

php exchange rate

Making multi currency websites can be a complete nightmare. Exchange rates change at a moments notice and it can be a constant struggle to keep yourself from losing money if one of the many markets crash.

Therefore I did a little bit of research and found you can get a rough idea of exchange rates by using yahoo finance. Yahoo provide a csv download from their site with live data. It's probably not as reliable as a feed from xe.com, but it's free and gives you a good indication of the exchange rate.

Here's a funscion i've made fo get this data;


function fget_exchange_rate($into, $infrom='GBP'){
$lcsv = file_get_contents('http://download.finance.yahoo.com/d/quotes.csv?s='.$infrom.$into.'=X&f=sl1d1t1ba&e=.csv');
$la = split(',',str_replace('"','',$lcsv));
if($la[1] != '') return $la[1];
return false;
}

echo('US $'.fget_exchange_rate('USD').'
');
echo('AU $'.fget_exchange_rate('AUD').'
');
echo('Euro '.fget_exchange_rate('EUR').'
');
echo('Japan '.fget_exchange_rate(JPY).'
');



You could add this to an automatic script to update the prices on your site daily. However, please be aware that you still need to keep an eye on things just incase you encounter problems.

Friday 19 February 2010

Vi delete line if search does not exist

Right, if you have ever had to scroll through an endless text file looking for the needle in a haystack, there is a simpler way using the vi editor. Using vi, You can use a simple search and delete command to search the file and delete lines where a phrase doesn;t exist, like so;

:g!/text to look for/d

The example above searches all lines for text containing 'text to look for' and deletes the line if the text does not exist.

You could do the opposit and delete lines where the text exists like so;

:g/text to look for/d

Tuesday 16 February 2010

json_decode Fatal error: Cannot use object of type stdClass as array

You may receive the following error when encoding and decoding information using json in php

Fatal error: Cannot use object of type stdClass as array

When using json_decode, information is by default converted into an object. You need to use the function get_object_vars to convert the object into an array, like so;


$new_data = get_object_vars(json_decode($data));

print_r($new_data);

?>

Thursday 17 December 2009

Enable php5 on 1and1

Arrrggg - made a website in php5 and then get loads of errors when uploading to 1and1? Luckily you can change the default setting by adding the following line to a .htaccess file;


AddType x-mapp-php5 .php

Hope this helps - I found this information on the following link;

Source
http://tech.xptechsupport.com/using-php-5-with-1and1-hosting.html

Sunday 4 October 2009

PHP convert minutes to hours

PHP convert minutes to hours

Just a little function i made;

function minutes_to_hours($inmin){
$lh = floor($inmin/60);
$lm = $inmin - ($lh*60);
if ($lh > 0) return $lh.'h '.$lm.'m';
return '0h '.$lm.'m';
}

So calling minutes_to_hours(125) = 2h 5m

Tuesday 22 September 2009

Retrieve Mysql Lost Data

Right, well have you ever been in a position where you have lost information on websites through incorrect sql? Well, chances are that not everything is lost, especially with mysql.

Mysql records all sql statements run on mysql a compressed binary log. Everytime mysql restarts, a new log is created. In fedora, these logs are located in /var/lib/mysql/. For example, you will see files named mysql-bin.000001. However, to make use of them, first you must extract them into a readbale forma using the mysqlbinlog tool.

mysqlbinlog takes a binary log file and then does something with it. For example, to view the file, simply use the following example;

mysqlbinlog mysql-bin.000001

To write the log to a singel file, use the following command;

mysqlbinlog mysql-bin.000001 > all_sql_statements.sql

Then you can simply read the fil and take the statements you need from it.