Challenges that each developer faces every day

Wednesday, November 4, 2009

How to validate Canadian domains (php regex)

<?php

// devcha.com
$domain = empty($_REQUEST['domain']) ? '' : trim($_REQUEST['domain']);

if (!preg_match('#^[a-z0-9\-]+(?:\.(?:bc|ab|sk|mb|on|qc|nb|ns|pe|nf|nt|nu|yk))?\.ca$#si', $domain)) {
echo 'Error';
}

?>

 Digg  Del.icio.us  Reddit  SlashDot

Domain Cleanup Regular Expressions

If you have to prompt the user to enter a domain could use these php regular expressions to *clean up* the domain name.

<?php

// devcha.com
$domain = empty($_REQUEST['domain']) ? '' : trim($_REQUEST['domain']);

$domain = preg_replace('#^(ht|f)tps?:/*#si', '', $domain); // http OR https OR ftp OR ftps
$domain = preg_replace('#^w+\.#si', '', $domain); // www.domain...
$domain = preg_replace('#/.*#si', '', $domain); // e.g. domain.com/afsaksfasfj

?>

 Digg  Del.icio.us  Reddit  SlashDot

Tuesday, November 3, 2009

How to select a date range of current month in Zend Framework

    $acc_obj = new My_Db_Table_Accounts();
$select = $acc_obj->select();
$select->from('your_table', array('cnt' => new Zend_Db_Expr('COUNT(*)')));
$select->where("YEAR(reg_date) = YEAR(CURDATE()) AND MONTH(reg_date) = MONTH(CURDATE())");
$acc_res = $acc_obj->fetchRow($select);
$stats['clients_reg_this_month'] = empty($acc_res['cnt']) ? 0 : $acc_res['cnt'];


Notes:

My_Db_Table_Accounts inherits from Zend_Db_Table_Abstract.
My_Db_Table_Accounts also defines getName() method which allows me to do:
$select->from($acc_obj->getName(), array('cnt' => new Zend_Db_Expr('COUNT(*)')));

Related

Keywords: Select Entries Within Current Month

 Digg  Del.icio.us  Reddit  SlashDot

Monday, November 2, 2009

Outsourcing Services to Your Customers

I am a subscriber of Telus (one of Canadian phone companies).
The phones are locked for it by the way so I cannot use any phone.

I have several phones on my account and one of the phone's microphone stopped working.

I was given several options:
- pay for a repair (min $125 + labour + parts)
- buy a phone that's a pay-as-you-go thing and replace the broken phone with it
- renew the contract for another 3 years and get $150 discount.

I checked my drawers and I found an old but working Telus phone.

It seems Telus allows customers to activate their phones by themselves (for $10 as of today) OR in a store (for approx. $25-$35).

That's a pretty good idea.
That way some of the clients can help themselves which would reduce support requests.

Do you have any services that you could outsource to your customers ?
.... and have more time developing your services/products.

--S

 Digg  Del.icio.us  Reddit  SlashDot

Thursday, October 29, 2009

Setup/Enable Anonymous FTP Access in Plesk

Note: Make sure you check the important notes below the instructions.

These are the instructions from Plesk's manual.

To allow anonymous FTP access:
1 Click the Anonymous FTP icon in the Hosting group.
2 To activate anonymous FTP service, click the Switch On icon.
3 To set up a welcoming message to be displayed when users log in to
FTP site, select the Display login message check box and type the
message text in the input field as desired.
Note that not all FTP clients display welcoming messages.
4 To allow visitors to upload files to the /incoming directory, select the
Allow uploading to incoming directory check box.
5 To allow users to create subdirectories in the /incoming directory,
select the Allow creation of directories in the incoming directory check box.
6 To allow downloading files from the /incoming directory, select the
Allow downloading from the incoming directory checkbox.
7 To limit the amount of disk space that can be occupied by uploaded
files, deselect the Unlimited check box corresponding to the Limit disk
space in the incoming directory option, and specify the amount in kilobytes.
This is the hard quota: the users will not be able to add more files to the directory
when the limit is reached.
8 To limit the number of simultaneous connections to the anonymous
FTP server, deselect the Unlimited check box corresponding to the Limit
number of simultaneous connections option and specify the number of
allowed connections.
9 To limit the bandwidth for anonymous FTP connections, deselect the
Unlimited check box corresponding to the Limit download bandwidth for this
virtual FTP domain option and enter the maximum bandwidth in kilobytes
per second.
10 Click OK.

Important Notes:
There are 2 requirements for the anonymous FTP icon to show up for your domain.
1) the domain setup must be using physical hosting and
2) configured on exclusive IP only
3) When you change the IP it may take from 4h-24h.
During that time plesk will probably show the default host's page.
A nice workaround for this is to create an .htaccess file in
/var/www/vhosts/default/htdocs/

This snippet will proxy the requests to your domain from the default one.
That way you won't have to wait.

RewriteEngine On
RewriteOptions Inherit
Options -Indexes +FollowSymlinks +Includes

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule .? http://example\.com%{REQUEST_URI} [P,L]

Users should be able to log into ftp://example.com using user: anonymous and any password.

 Digg  Del.icio.us  Reddit  SlashDot

Tuesday, October 27, 2009

How to configure HTTP authentication in Plesk from the command line

How to configure HTTP authentication in Plesk from the command line

You may be moving old sites to your Plesk powered super server.
Here is how to do it.

I am assuming that the domain has .htpasswd file already generated.

Login/switch to root
Edit /var/www/vhosts/example.com/conf/vhost.conf and add these lines

<Location /protected-folder/>
AuthUserFile /var/www/vhosts/example.com/httpdocs/protected-folder/.htpasswd
AuthName "Your Company"
AuthType Basic

<Limit GET POST>
require valid-user
</Limit>
</Location>


Restart webserver using web server manager
/usr/local/psa/admin/bin/websrvmng -a -v

 Digg  Del.icio.us  Reddit  SlashDot

Saturday, October 24, 2009

How to get server load average in PHP

Under *nix operating systems one would normally use /proc/loadavg and parse its output to gather info about the server load.

Fortunately since PHP 5.1.3 there is sys_getloadavg() command which gives you three samples (last 1, 5 and 15 minutes).

Example:

 <?php
$load = sys_getloadavg();
if ($load[0] > 80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>



More info: http://php.net/manual/en/function.sys-getloadavg.php

Things to keep in mind. If you want to be notified when your server is overloaded you may think that sending an email to yourself is a pretty good idea.
Actually it is a good idea only when you control the number of emails it sends to notify you. I'd suggest you implement some sort of check so the function/method puts a small delay e.g. 3-5 mins between emails.

Related

--S

 Digg  Del.icio.us  Reddit  SlashDot