Marius van Witzenburg "Learned my lesson in life, now setting my action to stay in life."

14Apr/101

Show hidden .htaccess files in Eclipse/Aptana

Most webdevelopers require .htaccess files to be visible in their projects, since by default Eclipse and Aptana both hide files starting with a . (=period) this can be quite irritating since you have to open the file from your operating system file explorer.

The fix to make .htaccess files visible is quite simple, you have to modify the file browser view filter to show hidden files but hide certain file types so you won't get a mess in your view.

First goto the filters menu by clicking the little arrow on the top right of the file browser and click 'Filters...'.

See the menu below:
eclipse hidden files 1 512x320 Show hidden .htaccess files in Eclipse/Aptana

When you are at the filter menu you will see the following window:
eclipse hidden files 2 448x512 Show hidden .htaccess files in Eclipse/Aptana

In this window uncheck '.* resources' and check 'Name filter patterns (matching names will be hidden):'.

Now you have to add a filter because you will be flooded by hidden files in your file explorer.
The filter I used is:

._*, *.svn, .cvsignore, _notes, .*.swp, .DS_Store, .AppleDouble, .project, .buildpath, .settings

You might append more filters to your needs.

In case you have a remote filesystem plugin installed you might need to check 'Show hidden files' in your preferences.

This is an example how this window might look:
eclipse hidden files r 512x447 Show hidden .htaccess files in Eclipse/Aptana

Another handy thing to know, never edit the .project file!

That was about it for now, happy coding! ;-)

10Mar/100

List only directories on the command-line or in your scripts

Here you have some different ways of showing only the directories on a path you specify.
This can be used in different other languages such as PHP with shell_exec for example.

ls -l /shares/ | grep "^d" | awk '{ print $9 }'
find /shares/ -maxdepth 1 -mindepth 1 -type d | sed 's/.\///g'
find /shares/ -maxdepth 1 -mindepth 1 -type d | perl -pi -e 's/.\///g'
find /shares/ -maxdepth 1 -mindepth 1 -type d | grep -v '^\./\.'

I hope you find them useful on some way :-)
Of course comments are more than welcome!

3Mar/101

Easy replace php short open tags with long php open tags

I wrote a quite simple script to fix short open tags to long open tags.

This script is compatible with CakePHP 1.1 and 1.2, on request :-)

Have fun using it!

I suggest saving this file to /usr/local/bin/php_fix_short_open_tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# Written by Marius van Witzenburg <info@mariusvw.com>
 
path="."
if [ ! -z "$1" ] && [ -d "$1" ]
then
    path="$1"
elif [ ! -z "$1" ]
then
    echo "Invalid path defined, using default."
fi
 
echo "Searching for php short open tags..."
echo "Starting path: $path"
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.php' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.ctp' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.inc' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?=/<\?php echo /g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?/<\?php/g' '{}' \; 
find $path -name '*.thtml' -type f -exec perl -i -wpe 's/<\?phpphp/<\?php/g' '{}' \; 
 
echo 'Done!'

Please leave the credits in if you want to modify it.
Donations are welcome haha :-P

2Mar/100

Shellproxy, an easy tool to run shell commands from PHP or other languages

This is a simple script that runs a shell command from PHP with the function system().
It sometimes happens that a command doesn't close the stdin or stdout, resulting the system() function to halt and wait for it to finish...
This causes PHP to reach the 30 second time limit and kill the script.

This script runs it in the background and you could make your commands send their output to a log file and still having full control.

Read the code below how to use it.

You can compile it by placing the code into 'shellproxy.c' and then compile it with the following command:

gcc -o shellproxy shellproxy.c

I hope this is something useful for you as it was for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio .h>
#include <stdlib .h>
#include <string .h>
 
int main(int argc, char *argv[]) {
    char command[1024];
    int t;
 
    if (argc == 1) {
        printf("\nShell Proxy\n");
        printf("By Marius van Witzenburg <http ://mariusvw.com>\n\n");
        printf("You will mostly use this program from PHP or any other\n");
        printf("webscripting language to background run programs\n\n");
        printf("Usage:\n");
        printf("- CLI:\n");
        printf("shellproxy \"<cmd> <arg>\"\n");
        printf("shellproxy \"<cmd> <arg> > redirect.txt\"\n");
        printf("- PHP:\n");
        printf("system('shellproxy \"<cmd> <arg> &\"');\n");
        printf("system('shellproxy \"<cmd> <arg> > redirect.txt\"');\n\n");
    }
 
    command[0] = 0;
 
    for (t = 1; t < argc; t++) {
        strcat(command, argv[t]);
        strcat(command, " ");
    }
 
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);
 
    system(command);
}

It would be nice if you keep my name mentioned into this code if you use it.

Ps. Donations are welcome... ;-)

13Nov/090

Got a new “Supermicro SATA server 6015B-NTR” today.

Toys, always welcome :-)

Lets see if this baby can host what I want to host on it...

Two things that I noticed directly after removing the top case are the double fans. Which means you have a more powerful air throughput and the two USB ports on the motherboard.

I must say, again, Supermicro is nice stuff to see and to have ;-)

Too bad you only have such stuff near you for some hours ;-) Then it goes directly to the datacenter to do its job...

13Sep/090

E_STRICT crashes php if date.timezone is not set in php.ini

In case you use the following in your php.ini:

error_reporting = E_ALL | E_STRICT

You might notice that if you do a phpinfo() that php crashes the apache child.

Reason is a bug in php that causes an infinite loop.

You can solve this by setting "date.timezone =" to your timezone.

For a list of timezones check:
http://www.php.net/datetime

31Jul/090

PHP Performance – isset() versus empty() versus PHP Notices

Original Posting

I’m cleaning up a lot of PHP code and always program with PHP error_reporting set to E_ALL and display_errors turned on so that I make sure to catch any PHP messages that come up. Since starting on this site, I have fixed literally hundreds (maybe thousands) of PHP Notices about using uninitialized variables and non-existent array indexes.

I have been fixing problems like this where $somevar is sometimes undefined:

if ($somevar)

by changing it to:

if (isset($somevar) && $somevar)

This successfully gets rid of the NOTICEs, but adds some overhead because PHP has to perform two checks. After fixing a lot of this in this manner, I’ve noticed that the pages seem to be generated a little slower.

So, to provide some conclusive results to myself, I wrote up a quick benchmarking script - available at php_empty_benchmark.php. It goes through 1,000,000 tests using each of these methods:

    if ($a) - This generates a notice if $a is not set
    if (isset($a)) - A simple clean way to check if the variable is set (note that it is not equivalent to the one above)
    if (isset($a) && ($a) - The one that I have been using which is equivalent to if($a), but doesn’t generate a notice.
    if (!empty($a)) - This is functionally equivalent to if($a), but doesn’t generate a notice.

It measures the time to perform 1 million tests using a defined percentage of values that are set. It then computes the difference as a percentage of the time taken for the original test (the one that generates the notices). A ‘diff’ of 100 means that the execution time is the same, greater than 100 means that it is faster, and less than 100 means that it is slower. A typical test produced these results:

With NOTICE: 0.19779300689697
With isset:  0.19768500328064 / Diff: 100.05463419811
With both:   0.21704912185669 / Diff: 91.128222590815
With !empty: 0.19779801368713 / Diff: 99.997468735875

In summary, using the if (isset($a) && $a) syntax is about 8-10% slower than generating the PHP Notice. Using !empty() should be a drop-in replacement that doesn’t generate the notice and has virtually no performance impact. Using ifset() also has no performance impact, but is not exactly the same as ‘if($a)’ since isset() will return true if the variable is set to a false value. I included it here, because it often make the code a little more readable than the !empty($a) syntax. For example:

$myvalue = !empty($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';

Versus

$myvalue = isset($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';
28Sep/080

400 new avatars added to random avatar script

Today I have added almost 400 new images to the random avatar script, you can find it under Gadgets, have fun with it!

Tagged as: , , No Comments
28Apr/080

New counter for avatar gadget

Today I wrote a counter into the avatar script.

It simply counts the total hits on the script and saves that to the database.

I set the counter to the old value of the avatar script.

You can see the total views on the info page of the this gadget.

28Apr/080

New avatar script added

Today I rewrote my avatar script. You may use this script freely on your website or on a forum for example.

This script contains a lot of my avatars that I used in the past on MSN and on forums. It rotates between over 1000 avatars and tries to display a complete random pattern so that you only see the same avatar once upon the total avatars that have been added.

You can find it under Gadgets, have fun with it!

Tagged as: , , No Comments