All posts by Manish

Some Useful Regex

Mail-id verification:

/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/

Though both can be used in PHP and JS but there is another method to verify an email-id in PHP, by using the below code

if(!filter_var($email, FILTER_VALIDATE_EMAIL))

FILTER_VALIDATE_EMAIL is a predefined filter in PHP.
Name Verification:

/^[A-Za-z .'-]+$/

Phone Verification:

/^[0-9 -]+$/

Removing all spaces:

$pattern = '/\s+/';
$replace = "";
$string = preg_replace($pattern,$replace,$string);

 

Secure Upload Folders

There are two ways to prevent execution of any (malicious) scripts uploaded to by users.
Method one – like described in this post.
Method two – add the below code to .htaccess file of the directory that needs to be protected.

RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo .sh .html .shtml .jsp

If it doesn’t work for PHP scripts on servers where suPHP is enabled, then see this post

Throw scripts as text instead of executing it

To throw PHP or other scripts as text instead of executing it, add the below code to .htacess file.

AddType text/plain .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo .sh .html .shtml .jsp

This method can be used to execute .html files as .php (or for executing any other type of file as some other type).

This requires mod_mime module of Apache to be installed and enabled.

There might be problem if su_php is enabled. In case the above doesn’t work for PHP scripts when suPHP is enabled then try adding the following to Apache Directives (main is the highlighted code).

<Directory /var/www/domain.com/files/images/>
        suPHP_Engine off
 </Directory>

It is not a good idea to disable suPHP for the whole site, instead disable only for the required directory.

Apache, Make Shared Hosting (single) IP open a particular domain

This is based on Apache on Ubuntu.
The server has a single IP Address and is used to host multiple domains using VirtualHost
If it is needed that on using the IP address of the server (in a browser) a particular site/domain get opened, then do the following:

  • Create a vhost file with the IP address as name e.g 192.168.0.1.vhost
  • Add the necessary VirtualHost configuration. It can be copied from the domain’s vhost file
  • The ServerName in the vhost file will be the IP. e.g. ServerName 192.168.0.1
  • Create a link of the vhost inside sites-available folder to a proper file in sites-enabled folder. e.g.
    ln -s /etc/apache2/sites-available/192.168.0.1.vhost ../sites-enabled/192.168.0.100.vhost
  • Restart apache

Please note adjustments might be necessary in the .htaccess file. Like if non-www to www redirection is enabled or http to https redirection is enabled, they will give error and warning (respectively) when IP is used to access the site. Adding

RewriteCond %{HTTP_HOST} !192\.168\.0\.1

before the existing condition ( e.g. RewriteCond %{HTTP_HOST} !^www\. ) should help.

PHP Microframeworks

Below is a list of some PHP Microframeworks

  • Epiphany
  • Flight
  • Fat Free
  • Silex
  • Slim
  • BulletPHP
  • Limonade
  • Phalcon
  • Recess PHP
  • Tonic
  • Wave Framework
  • Zaphpa

Though all have their own merits and demerits but Phalcon is the most recommended one for speed and Slim and Flight for ease and speedy learning. In general Slim seems to be the most preferred one and has quite some handy features.

Trends in PHP based CMS

WordPress seems to be gaining up and looks like it will do so for some time more.

Search Trends for WordPress, Drupal and Joomla


Search for WordPress by Country

Search for Drupal by Country

Search for Joomla by Country