Posts Tagged with "tricks"

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

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

How to trim array elements in PHP in one shot

Posted by Stanislav Furman on April 18, 2013
Remove all leading and trailing white spaces in all PHP array values.

How to get most accurate visitor's IP address in PHP

Posted by Stanislav Furman on April 15, 2013

Web developers often need to get visitor's IP address to use it in web applications. This can be used in internal traffic analytics tools, or as a part of security measures.

Most common and standard method of getting visitor's IP address is getting the value "REMOTE_ADDR" from the global PHP array $_SERVER:


<?php
$visitor_ip = $_SERVER['REMOTE_ADDR'];

However, standard PHP $_SERVER['REMOTE_ADDR'] value not necessarily contains the originating visitor's IP address because, for example, visitor can use a proxy server to access your web site. Using PHP we could try to detect user's IP address even if he uses proxy, but keep in mind that there is no guarantee that IP address that you get is 100% accurate (e.g. proxy can be anonymous).

Continue reading

Reading file list from a mapped Windows network drive

Posted by Stanislav Furman on March 25, 2013

Last week I ran into a situation when I had to read directory file list from a mapped Windows network drive. At your very first look it may seem pretty simple. It is! However, there is a little trick - Windows may require your network/domain credentials, and then standard PHP functions such as opendir() won't be able to access the directory. Fortunately, the solution is pretty simple:

Continue reading

CSS rounded corners

Posted by Stanislav Furman on February 26, 2012

Basic approach and main idea

Very often, web designers use CSS rounded corners to make websites nicer. This blog uses rounded corners too. You could use different methods to create rounded corners on your web site, but from my point of view the simplest one of these methods is the method that uses CSS3.

Unfortunately, this method won't work in IE (what a surprise!), but, fortunately, there is a hack exists that can fix this problem.

For most of web browsers the following code will work:


<style>
#container {
  width: 200px;
  height: 200px;
  background-color: #cccccc;	
}
.rounded {
  -moz-border-radius: 10px; /* Works in Firefox */
  -webkit-border-radius: 10px; /* Works in Safari, Chrome */
  border-radius: 10px; /* It's CSS3 */
}
</style>

<div id="container" class="rounded"></div>
Continue reading

How to secure your php application

Posted by Stanislav Furman on February 21, 2012

Working on web projects you should consider most common dangers such as XSS ("Cross Site Scripting"), SQL Injection, Session Hijacking, Spoofed Form Submissions, etc. This little guide will help you to understand basics of web security and it will help you to protect your web applications against possible attacks.

Remember that security must be balanced with expense. Don't use paranoidal security approaches if it is not really needed.

Please note that this post covers only basic security approach which is quite enough for most of web sites but not enough for large, e-commerce projects. However, listed measures are fundamental for all types of web applications.

Continue reading

CSS Centering Trick: How to center a div or an image?

Posted by Stanislav Furman on February 15, 2012

From time to time we create HTML/CSS templates where we need to center a div or an image. For some newbies it poses a problem. In this blog post I'll show you one of the ways of CSS centering.

Suppose you have a div or an image (with the size 150px X 100px) that should be centered on the web page. First step you do is the following CSS:


centered {
  position:  fixed;
  top:  50%;
  left:  50%;
}

CSS Centering Trick (image 1)

Continue reading

12 SEO tips that everyone should know

Posted by Stanislav Furman on February 10, 2012

1. The first thing that you have to know - content is your number one and it's your primary priority. Therefore, make sure that you publish good, clear and unique content that is based and focused on your target keywords.

2. Quality backlinks is your number two. Buliding of a network of backlinks is a very important thing in terms of Search Engine Optimization. Make sure backlinks contain your keywords or key phrases between the tags.

3. Remember that one single link from a good site can beat 10 links from other 10 web sites with poorer quality.

Continue reading