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.

Thursday, 16 July 2009

PHP Session Test

Recently, i've had problems setting a clients site live on a 3rd party providers hosting. The problem is the site wouldn't allow sessions to be written so login scripts and catcha codes did not work as intended. The main problem is that technical support always blame programmers code before actually investigating the problem (sigh).

Therefore here is a simple php script to prove to the technical support that sessions are not working as they should;

<?
session_start();
if($_GET["test"] == '1'){
if($_SESSION['atest'] == 'yes'){
echo('Your hosting supports sessions');
} else echo('Your hosting does not support sessions');
} else {
$_SESSION['atest'] = 'yes';
echo '
<head>
<script type="text/javascript">
<!--
function delayer(){
window.location = "'.$_SERVER['PHP_SELF'].'?test=1";
}
//-->
</script>
</head>
<body onLoad="setTimeout(\'delayer()\', 5000)">
About to test sessions.
<br /><br />
please wait...
</body>
';
}
?>

The script performs a 301 redirect to test if sessions are working correctly when a page is refreshed.

Sunday, 25 January 2009

Mysql Remove Line Breaks

I've recently had line breaks added to some data i've imported. Here's how you remove line breaks at the beginning and end of a field in mysql;

update temp_table set fieldname=trim(both char(13) from fieldname)

The above is only if you are in windows. I believe you can also use the folowing for linux;

update temp_table set fieldname=trim(both '\n' from fieldname)

You should also be able to use reaplce usinng the char method, e.g.

update temp_table set fieldname=replace(fieldname, char(13), '-')

I've not tested it though - I hope this helps!

Friday, 16 January 2009

MySQL Monthly Report

Here is is brief howto on generating a Monthly MySQL report from an orders table.

This example assumes you have a table called orders a stored date field called added and a price field for each order - in this example the price is stored as a varchar so it is casted using a (0+price).

The fist column returned is the current month, the second is the total price for that month. Here is the MySQL SQL Monthly Report Query;


SELECT TIMESTAMPDIFF(MONTH, STR_TO_DATE(concat(month(added),'/1/',year(added)), '%m/%d/%Y'), STR_TO_DATE(concat(month(now()),'/',DAYOFMONTH(LAST_DAY(now())),'/',year(now())), '%m/%d/%Y')) as tmonth,
sum(0+total_price) as lprice
from orders
where valid=1 and total_price > 0
group by tmonth


There probably are quicker ways of doing this but it seems like the best solution for reporting on a single orders table. This example basically makes the date as a string using CONCAT and then compare it.

I hope this helps.

Saturday, 22 November 2008

Sendmail stat=Deferred: Name server: host name lookup failure

It's a saturday. I have just spent 4 1/2 hours making sendmail actually work on a development server. All other websites I found gave me useless information on how to fix this, So I thought I'd post my fix here to Save countless hours of your weekend....

I kept receiving the followingerror

stat=Deferred: Name server: host name lookup failure

in my maillog file.

The main crox of the problem is that I set up a FC9 development server on my local network without installing Bind. At the time I didn't want to use Bind, which is a common thing not to have on a development server. However, sendmail doesn't look at my hosts file to resolve network addresses... oh no, why be so simple! instead isendmail decides to ignore my hosts file and visits the router to find out where localhost is. Therefore, to fix this problem, you need to install bind and just setup a localhost.

You Need to install Bind on your server to get Sendmail to work.



Don't rely on your hosts file. If you are unfamiliar with using Bind, on fedora you can just type; yum install bind bind-chroot. You can setup a simple configuration using this guide.

After you have Bind setup, you will then need to edit your network settings to make sure all DNS requests look to your server first, before going to the internet to resolve. So edit this file;

vi /etc/sysconfig/networking/devices/ifcfg-eth0

and change

DNS1=192.168.1.1

to

DNS1=127.0.0.1
DNS2=192.168.1.1

Restart your server and make sure that sendmail and bind are both up and running. Then try crossing your fingers, finding some wood to touch and then sending an email.

For me, this worked a treat and shows in my case that Sendmail completely ignores my hosts file and goes straight for the servers primary DNS setting.