Posts Tagged with "how-tos"

Detect an Ajax request in PHP

Posted by Stanislav Furman on August 25, 2017

If you would like to use same PHP code to handle both AJAX and non-AJAX requests, here is a quick and simple trick that you can use to check if the incoming request is AJAX. For our trick we will use a HTTP header called HTTP_X_REQUESTED_WITH. It is supported by all modern browsers that support AJAX. Therefore, it should work in 99% of cases.

Continue reading

Delete all Docker containers and images

Posted by Stanislav Furman on September 10, 2016

First time I heard about Docker was beginning of 2016. Finally, I got a chance to try Docker in "wild nature".

One of the most common things you might need from time to time is to stop or remove all your Docker images in one shot. Here are some simple and useful commands to stop or remove all of Docker containers.

Before deleting Docker containers we need stop them:


$ docker stop $(docker ps -a -q)

Now, when our Docker containers are not running, we can delete them:


$ docker rm $(docker ps -a -q)

Also there is a nice untility to clean your Docker images, volumes and networks. It's called Docker Clean.


How to use optional parameters in URI path in Yii framework

Posted by Stanislav Furman on July 10, 2014

Yii - is one of my favorite frameworks. I've done a few projects using this framework and have been always satisfied with its functionality, flexibility, performance and community support. This post starts a series of "how to" articles where I will give some tips, advises and best practices.

If you need to use optional parameters in URLs here is how you can do it in UrlManager:

Continue reading

How to update fields from another table in MySQL

Posted by Stanislav Furman on May 28, 2014

This article is to continue the series of short handy MySQL tips that I started a while ago. 

If you need to update a number of column fields in a MySQL table with data from another table, there is a simple way to do it. Lets say you need to update user phone numbers in table1 with the corresponding phone numbers from table2. Then you could run the following query.

Continue reading

Unix shell commands to detect a DDoS attack and its source

Posted by Stanislav Furman on April 25, 2014
5 Unix shell commands to check if your server is under DDoS attack and its source

Redis: How to delete keys matching a pattern

Posted by Stanislav Furman on April 10, 2014

Sometimes you might want to purge a set of similar Redis keys in one shot. So far, standard Redis "DEL" command does not allow to remove keys using patterns. However, there is a trick to do this action. Execute the following command in bash:


redis-cli -h [host] -p [port] KEYS "prefix:*" | xargs redis-cli DEL

Seems clear? Wait a minute! What if you use multiple databases (keyspaces) and need to remove keys from a database different from default (0) one? No problem there! Here is the solution:

Continue reading

Backward version compatibility in PHP web application

Posted by Stanislav Furman on February 17, 2014
How to handle backward PHP version compatibility in your web application

How to recognize a good programmer

Posted by Stanislav Furman on September 30, 2013
How to find a good programmer: most important indicators of a good professional

File search and wildcards in PHP

Posted by Stanislav Furman on June 7, 2013

This post will continue one of my previous posts Reading file list from a mapped Windows network drive.

Recently, I have had a challenge with reading files from a directory using a wildcard pattern (i.e. " *.jpg "). Obviously, regular is_file() or file_exists() functions do not work in this case. So, I started looking for a solution that could help me with my problem.

Luckily, there is a function glob() that searches for the pathnames matching given pattern.


<?php
foreach(glob('*.jpg') as $image) {
  if( !is_dir($image) ) {
    $images[] = $image;
  }
}
?>
Continue reading

Concatenating NULL and blank fields in MySQL

Posted by Stanislav Furman on May 17, 2013

If you ever need to concatenate fields that are empty or equal null, you may run into troubles because MySQL doesn't concatenate NULL-fields as you might expect - if there is one of the concatenating fields equals NULL, the whole concatenating value will be NULL.

See the following dummy table:


firstname | middlename | lastname | email
John        J.           Smith      [email protected]
Amanda      NULL         Smith      [email protected]

As you can see Amanda Smith doesn't have middle name. So, if you now run a query that will try to concatenate first name, middle name and last name, you'll get NULL value instead of expected concatenated value.

Continue reading