Friday, November 26, 2010

Setting up the firewall in Mac OS X 10.5 (Leopard)

SkyHi @ Friday, November 26, 2010
After receiving a port scan this morning (thanks for the new year present):

I’ve came to realize that I need to setup the firewall on my Mac asap before someone decided to take a tour of my iMac. Until recently, I’ve always use my PC laptop to surf the Internet, so I always forgot to setup the firewall on my Mac…but after the incident it looks like I have to act quick before things got out of hand. If you are new to Leopard and you have no idea how to setup the firewall, here is a simple step by step guide:
Step 1: Click on the System Preferences icon on your dock:

Step 2: Click on Security:

Step 3: Click on Firewall:

Step 4: By default, the firewall will allow all incoming connections…which is not very helpful at all:

We need to change that by clicking on the 3rd option:

Step 5: Now click on the “+” button at the bottom left hand corner to choose which your application(s) you want to allow or block access by the firewall:

Select the application(s) you want and click on the Add button.
Step 6: After you have added your application(s), click on the Advanced button:

As you can see, the Stealth Mode is not turned on by default:

Turn on the Stealth Mode and click on the OK button:
Step 7: The applications you have added are allowed (incoming connections) by default, if you want to block some of your application(s), just click on that application and change to “Block incoming connections“:

- – - -
I hope this article will help you to setup your firewall
REFERENCES

BAD HEADER, Improper use of control character (char 0D hex)

SkyHi @ Friday, November 26, 2010
I looked at the headers of a message from pligg by chance and saw this in there.


X-Amavis-Alert: BAD HEADER, Improper use of control character (char 0D hex):

From: webmaster@nothing.no.no\r\n


So my Amavis virus filtering didn't like the \r\n - which I don't believe is needed. I tested it with just the \n and no problems. It still passes but the more stringent mail systems may bounce it.


diff linkadmin.php.original linkadmin.php

117c117

< $headers = 'From: ' . PLIGG_PassEmail_From . "\r\n";

---

> $headers = 'From: ' . PLIGG_PassEmail_From . "\n";


editlink.php, login.php, recommend.php also have this. In recommend.php on line 91 it looks like it was fixed. It was commented out and has a line below with just the \n and no \r.


I looked at the svn from last night - 407 I think?


I solved the issue by turning almost all \r\n into \n in the php

module called "compose2.php". I hope I will not have to do the same

with the whole code...

REFERENCES
http://forums.pligg.com/questions-comments/2801-emails-have-control-character-them.html
http://osdir.com/ml/mail.ilohamail.devel/2007-03/msg00000.html

Remote Desktop Into Mac OS X Leopard From Windows

SkyHi @ Friday, November 26, 2010

Using remote desktop, you can access one computer from another computer through the internet or a local area network. Here, I’ll outline how to remote desktop into Mac OS X 10.5 (Leopard) from Windows, which is something I had to do today.

On the Mac, go to System Preferences > Sharing and check “Remote Login,” if it is unchecked. You may get a message saying that your current power settings will not allow remote login when the Mac is sleeping. Just hit OK for that and Take note of the example ssh command given in the information area.

Then check “Screen Sharing,” if it is unchecked. Click the “Computer Settings” button on the right. A prompt should appear. Check “VNC Viewers may control screen with password” and type in a memorable password. Then hit OK and uncheck “Screen Sharing.” This is a security precaution. You will turn this on later when you ssh in.

On your PC, you will need to install PuTTY and TightVNC. You can get PuTTY here: http://www.chiark.greenend.org.uk/~sgtatham/putty/ and TightVNC from here: http://www.tightvnc.com/download.html.

On your PC, open up PuTTY, and in the “Host Name” field, type in the host name of the computer. This is the text after the @ symbol in the provided ssh command example noted earlier, e.g. my.host.name.edu. Select SSH as the connection type.

Then, on the left category menu, click on Tunnels, which is under Connection > SSH. For the “Source Port” box, type 5901, and for the “Destination” box, type localhost:5900. Then hit the Add button. And then hit the Open button in the bottom-right.

A black window will appear and a prompt may appear, asking you whether you trust the host. Click yes. Then, in the black window, you will be asked for your login name – enter your Mac login name. Then it will prompt for your password – enter that too.

Then, enable Screen Sharing by entering the following commands into PuTTY:
cd /Library/Preferences
echo -n enabled > com.apple.ScreenSharing.launchd
Next, open up TightVNC Viewer on your PC. It will prompt you for a VNC Server. Enter localhost:1. (This is possible because you are tunneling Port 5901 through SSH.) Hit "Connect." You should then be prompted for a password. Enter the VNC password that you set earlier.

That's it! A window should appear on your PC with the exact same display as your Mac and you should be able to control the mouse and do most things remotely.
Unfortunately, this method can yield severe lag between the two computers, especially if your Mac's internet connection has a slow upload speed.

One performance tweak you can do on the Mac is enter into a Terminal:
defaults write com.apple.ScreenSharing controlObserveQuality 3
This changes the quality of the remote display to setting 3 which is 8-bit color. The other settings are:
1 = black and white
2 = grey scale
3 = 8 bit color
4 = 16 bit color
5 = full color

Just replace the "3" in the command with the number of the desired setting.
You should probably also change your mac desktop wallpaper to a solid color and/or reduce the resolution so the screen image is more easily compressed.

Finally, before you close the connection, you should disable Screen Sharing. In your PuTTY window, which you must keep active while you are on VNC, enter the following commands:
cd /Library/Preferences
rm com.apple.ScreenSharing.launchd
Edit: Apparently, removal of this file and disabling of Screen Sharing only takes place after you restart the computer. i.e. after you remove this file, you can still vnc into the system. However, I don't know whether you can still vnc into it from a computer different from the one from which you originally vnc'd. You can restart the system immediately (closing all open files without saving changes) with:
sudo shutdown -r now
And that's it! You should now be able to remote desktop into Mac OS X Leopard securely from a Windows machine. Happy remote desktop-ing! Also, please leave a comment below if this was helpful or if you have any questions.

Note: If you have Cygwin installed like I do, then you can just open up a Cygwin shell window and enter:
ssh username@hostname -L 5901:localhost:5900
to ssh in with port forwarding.

Note 2: If you want to be able to access your Mac anytime, go to Energy Saver, which is also in System Preferences, and move the slider for Time Before Sleep to Never. You can also schedule automatic turn on/off of the Mac to save energy, e.g. have it off while you're asleep.

References:
http://lifehacker.com/319528/remote-control-leopard-with-tightvnc
http://www.macosxhints.com/article.php?story=20080318190503111
http://www.alternapop.com/2007/12/01/leopard-screen-sharing-image-quality/
http://www.whenbrainsfly.com/2009/01/remote-desktop-into-mac-os-x-leopard-from-windows/

rsync error: "IO error encountered -- skipping file deletion"

SkyHi @ Friday, November 26, 2010
IO error encountered -- skipping file deletion


Figured it out....



In hardy there is a hidden folder in /home/user/ called .gvfs...No idea what it is, but just exclude it from your rsync backup.

REFERENCES
http://ubuntuforums.org/showthread.php?t=290617

Thursday, November 25, 2010

25 Best SSH Commands / Tricks

SkyHi @ Thursday, November 25, 2010
OpenSSH is a FREE version of the SSH connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate eavesdropping, connection hijacking, and other attacks. Additionally, OpenSSH provides secure tunneling capabilities and several authentication methods, and supports all SSH protocol versions.

SSH is an awesome powerful tool, there are unlimited possibility when it comes to SSH, heres the top Voted SSH commands
1) Copy ssh keys to user@host to enable password-less ssh logins.

ssh-copy-id user@host

To generate the keys use the command ssh-keygen
2) Start a tunnel from some machine’s port 80 to your local post 2001

ssh -N -L2001:localhost:80 somemachine

Now you can acces the website by going to http://localhost:2001/
3) Output your microphone to a remote computer’s speaker

dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp

This will output the sound from your microphone port to the ssh target computer’s speaker port. The sound quality is very bad, so you will hear a lot of hissing.
4) Compare a remote file with a local file

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Useful for checking if there are differences between local and remote files.
5) Mount folder/filesystem through SSH

sshfs name@server:/path/to/folder /path/to/mount/point

Install SSHFS from http://fuse.sourceforge.net/sshfs.html
Will allow you to mount a folder security over a network.
6) SSH connection through host in the middle

ssh -t reachable_host ssh unreachable_host

Unreachable_host is unavailable from local network, but it’s available from reachable_host’s network. This command creates a connection to unreachable_host through “hidden” connection to reachable_host.
7) Copy from host1 to host2, through your host
ssh root@host1 “cd /somedir/tocopy/ && tar -cf – .” | ssh root@host2 “cd /samedir/tocopyto/ && tar -xf -”

Good if only you have access to host1 and host2, but they have no access to your host (so ncat won’t work) and they have no direct access to each other.

8) Run any GUI program remotely

ssh -fX @

The SSH server configuration requires:

X11Forwarding yes # this is default in Debian

And it’s convenient too:

Compression delayed
9) Create a persistent connection to a machine

ssh -MNf @

Create a persistent SSH connection to the host in the background. Combine this with settings in your ~/.ssh/config:
Host host
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster no
All the SSH connections to the machine will then go through the persisten SSH socket. This is very useful if you are using SSH to synchronize files (using rsync/sftp/cvs/svn) on a regular basis because it won’t create a new socket each time to open an ssh connection.
10) Attach screen over ssh

ssh -t remote_host screen -r

Directly attach a remote screen session (saves a useless parent bash process)
11) Port Knocking!

knock 3000 4000 5000 && ssh -p user@host && knock 5000 4000 3000

Knock on ports to open a port to a service (ssh for example) and knock again to close the port. You have to install knockd.
See example config file below.
[options]
logfile = /var/log/knockd.log
[openSSH]
sequence = 3000,4000,5000
seq_timeout = 5
command = /sbin/iptables -A INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 5000,4000,3000
seq_timeout = 5
command = /sbin/iptables -D INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn
12) Remove a line in a text file. Useful to fix

ssh-keygen -R

In this case it’s better do to use the dedicated tool
13) Run complex remote shell cmds over ssh, without escaping quotes

ssh host -l user $(> ~/.ssh/authorized_keys”

If you use Mac OS X or some other *nix variant that doesn’t come with ssh-copy-id, this one-liner will allow you to add your public key to a remote machine so you can subsequently ssh to that machine without a password.
17) Live ssh network throughput test

yes | pv | ssh $host “cat > /dev/null”

connects to host via ssh and displays the live transfer speed, directing all transferred data to /dev/null
needs pv installed
Debian: ‘apt-get install pv’
Fedora: ‘yum install pv’ (may need the ‘extras’ repository enabled)
18) How to establish a remote Gnu screen session that you can re-connect to

ssh -t user@some.domain.com /usr/bin/screen -xRR

Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with “Ctrl-a d” or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are “Ctrl-a c” (open new shell) and “Ctrl-a a” (alternate between shells). Read this quick reference for more screen commands: http://aperiodic.net/screen/quick_reference
19) Resume scp of a big file

rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file

It can resume a failed secure copy ( usefull when you transfer big files like db dumps through vpn ) using rsync.
It requires rsync installed in both hosts.
rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file local -> remote
or
rsync –partial –progress –rsh=ssh $user@$host:$remote_file $destination_file remote -> local
20) Analyze traffic remotely over ssh w/ wireshark

ssh root@server.com ‘tshark -f “port !22″ -w -’ | wireshark -k -i -

This captures traffic on a remote machine with tshark, sends the raw pcap data over the ssh link, and displays it in wireshark. Hitting ctrl+C will stop the capture and unfortunately close your wireshark window. This can be worked-around by passing -c # to tshark to only capture a certain # of packets, or redirecting the data through a named pipe rather than piping directly from ssh to wireshark. I recommend filtering as much as you can in the tshark command to conserve bandwidth. tshark can be replaced with tcpdump thusly:
ssh root@example.com tcpdump -w – ‘port !22′ | wireshark -k -i -
21) Have an ssh session open forever

autossh -M50000 -t server.example.com ‘screen -raAd mysession’

Open a ssh session opened forever, great on laptops losing Internet connectivity when switching WIFI spots.
22) Harder, Faster, Stronger SSH clients

ssh -4 -C -c blowfish-cbc

We force IPv4, compress the stream, specify the cypher stream to be Blowfish. I suppose you could use aes256-ctr as well for cypher spec. I’m of course leaving out things like master control sessions and such as that may not be available on your shell although that would speed things up as well.
23) Throttle bandwidth with cstream

tar -cj /backup | cstream -t 777k | ssh host ‘tar -xj -C /backup’

this bzips a folder and transfers it over the network to “host” at 777k bit/s.
cstream can do a lot more, have a look http://www.cons.org/cracauer/cstream.html#usage
for example:
echo w00t, i’m 733+ | cstream -b1 -t2
24) Transfer SSH public key to another machine in one step

ssh-keygen; ssh-copy-id user@host; ssh user@host

This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account’s ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.
25) Copy stdin to your X11 buffer

ssh user@host cat /path/to/some/file | xclip

Have you ever had to scp a file to your work machine in order to copy its contents to a mail? xclip can help you with that. It copies its stdin to the X11 buffer, so all you have to do is middle-click to paste the content of that looong file :)




====================================================================================
An almost invisible ssh connection

ssh -T user@host /bin/bash -i

sudo lsof -i :22

http://news.ycombinator.com/item?id=1536126

http://news.ycombinator.com/item?id=874666

http://news.ycombinator.com/item?id=1624010


REFERENCES
http://blog.urfix.com/25-ssh-commands-tricks/

Monday, November 22, 2010

PHP: Multidimensional Arrays --index(rows) vs associative arrays(columns)

SkyHi @ Monday, November 22, 2010
Array does not have to be a simple list of keys and values; each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.
Two-dimensional Arrays
Imagine that you are an owner of a flower shop. One-dimensional array is enough to keep titles and prices. But if you need to keep more than one item of each type you need to use something different - one of the ways to do it is using multidimensional arrays. The table below might represent our two-dimensional array. Each row represents a type of flower and each column – a certain attribute.

Title Price Number
rose 1.25 15
daisy 0.75 25
orchid 1.15 7
To store data in form of array represented by preceding example using PHP, let’s prepare the following code:

<?php
$shop = array( array("rose", 1.25 , 15),
               array("daisy", 0.75 , 25),
               array("orchid", 1.15 , 7) 
             ); 
?>
Array
(
    [0] => Array
        (
            [0] => rose
            [1] => 1.25
            [2] => 15
        )

    [1] => Array
        (
            [0] => daisy
            [1] => 0.75
            [2] => 25
        )

    [2] => Array
        (
            [0] => orchid
            [1] => 1.15
            [2] => 7
        )

)

This example shows that now $shop array, in fact, contains three arrays. As you remember, to access data in one-dimensional array you have to point to array name and index. The same is true in regards to a two-dimensional array, with one exception: each element has two indexes – row and column.

To display elements of this array we could have organize manual access to each element or make it by putting For loop inside another For loop:

<?php
echo "

Manual access to each element

"; echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]." "; echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]." "; echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]." "; echo "

Using loops to display array elements

"; echo "
    "; for ($row = 0; $row < 3; $row++) { echo "
  1. The row number $row"; echo "
      "; for ($col = 0; $col < 3; $col++) { echo "
    • ".$shop[$row][$col]."
    • "; } echo "
    "; echo "
  2. "; } echo "
"; ?>

Perhaps, instead of the column numbers you prefer to create their names. For this purpose, you can use associative arrays. The following code will store the same set of flowers using column names:
<?php
$shop = array( array( Title => "rose", 
                      Price => 1.25,
                      Number => 15 
                    ),
               array( Title => "daisy", 
                      Price => 0.75,
                      Number => 25,
                    ),
               array( Title => "orchid", 
                      Price => 1.15,
                      Number => 7 
                    )
             );
?>

Array
(
    [0] => Array
        (
            [Title] => rose
            [Price] => 1.25
            [Number] => 15
        )

    [1] => Array
        (
            [Title] => daisy
            [Price] => 0.75
            [Number] => 25
        )

    [2] => Array
        (
            [Title] => orchid
            [Price] => 1.15
            [Number] => 7
        )

)

It is easier to work with this array, in case you need to get a single value out of it. Necessary data can be easily found, if you turn to a proper cell using meaningful row and column names that bear logical content. However, we are loosing the possibility to use simple for loop to view all columns consecutively.

You can view outer numerically indexed $shop array using the for loop. Each row of the $shop array is an associative array. Hence, inside the for loop you need for each loop. Also you can get each element from associative array manually:
<?php
echo "

Manual access to each element from associative array

"; for ($row = 0; $row < 3; $row++) { echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get ".$shop[$row]["Number"]; echo " "; } echo "

Using foreach loop to display elements

"; echo "
    "; for ($row = 0; $row < 3; $row++) { echo "
  1. The row number $row"; echo "
      "; foreach($shop[$row] as $key => $value) { echo "
    • ".$value."
    • "; } echo "
    "; echo "
  2. "; } echo "
"; ?>

Three-dimensional Arrays

You don’t have to be limited by two dimensions: the same way as array elements can contain other arrays, these arrays, in their turn, can contain new arrays.

Three-dimensional array is characterized by height, width, and depth. If you feel comfortable to imagine two-dimensional array as a table, then imagine a pile of such tables. Each element can be referenced by its layer, row, and column.

If we classify flowers in our shop into categories, then we can keep data on them using three-dimensional array. We can see from the code below, that three-dimensional array is an array containing array of arrays:
<?php
$shop = array(array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   )
             );
?>

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => rose
                    [1] => 1.25
                    [2] => 15
                )

            [1] => Array
                (
                    [0] => daisy
                    [1] => 0.75
                    [2] => 25
                )

            [2] => Array
                (
                    [0] => orchid
                    [1] => 1.15
                    [2] => 7
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => rose
                    [1] => 1.25
                    [2] => 15
                )

            [1] => Array
                (
                    [0] => daisy
                    [1] => 0.75
                    [2] => 25
                )

            [2] => Array
                (
                    [0] => orchid
                    [1] => 1.15
                    [2] => 7
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => rose
                    [1] => 1.25
                    [2] => 15
                )

            [1] => Array
                (
                    [0] => daisy
                    [1] => 0.75
                    [2] => 25
                )

            [2] => Array
                (
                    [0] => orchid
                    [1] => 1.15
                    [2] => 7
                )

        )

)


As this array has only numeric indexes, we can use nested for loops to display it:
<?php
echo "
    "; for ( $layer = 0; $layer < 3; $layer++ ) { echo "
  • The layer number $layer"; echo "
      "; for ( $row = 0; $row < 3; $row++ ) { echo "
    • The row number $row"; echo "
        "; for ( $col = 0; $col < 3; $col++ ) { echo "
      • ".$shop[$layer][$row][$col]."
      • "; } echo "
      "; echo "
    • "; } echo "
    "; echo "
  • "; } echo "
"; ?>
This way of creating multidimensional arrays allows to create four- and five-dimensional arrays. Syntax rules do not limit the number of dimensions, but the majority of practical tasks logically correspond to the constructions of three or less dimensions.


REFERENCES
http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

PHP multidimensional array

SkyHi @ Monday, November 22, 2010

Step 1 - Introduction


PHP multidimensional array


As you know arrays are a list of key and value pairs. In general the key used to be a number ( or a string in case of associative arrays ) and the value is any data type. And that is important as it means that a value can be an other array as well. So if you create an aeeay where the values are arrays as well then you made a multidimensional array. Of course the values in this internal array can be again a new array and so on.

Let's see an example. Suppose you want to store user informations. In this case you have a lot of users and each user has more properties like name, age, email, website and so on.

* John
o email : john@demo.com
o website : www.john.com
o age : 22
o password : pass
* Anna
o email : anna@demo.com
o website : www.anna.com
o age : 24
o password : pass
* Peter
o email : peter@mail.com
o website : www.peter.com
o age : 42
o password : pass
* Max
o email : max@mail.com
o website : www.max.com
o age : 33
o password : pass


Step 2 - Creating the multi array


PHP multidimensional array

Now let's see how to define such an array in PHP. You can do it in some smaller parts and in this case first you define the sub arrays and then add it to the main array as follows:


$user1 = array("email" => "john@demo.com", 
               "website" => "www.john.com",
               "age" => "22",
               "password" => "pass");
 
$user2 = array("email" => "anna@demo.com",
               "website" => "www.anna.com",
               "age" => "24",
               "password" => "pass");
      
$user3 = array("email" => "peter@mail.com",
               "website" => "www.peter.com",
               "age" => "42",
               "password" => "pass");
$user4 = array("email" => "max@mail.com",
               "website" => "www.max.com",
               "age" => "33",
               "password" => "pass");
$userList = array("John" => $user1,
                  "Anna" => $user2,
                  "Peter" => $user3,
                  "Max" => $user4);

Of course you can do all of it in only one step like this:

$userList = array("John" => array(
                                                        "email" => "john@demo.com",
                                                        "website" => "www.john.com",
                                                        "age" => "22",
                                                         "password" => "pass"),
                              "Anna" => array(
                                                         "email" => "anna@demo.com",
                                                         "website" => "www.anna.com",
                                                          "age" => "24",
                  "                                       password" => "pass"),
                               "Peter" => array(
                                                          "email" => "peter@mail.com",
                                                          "website" => "www.peter.com",
                                                          "age" => "42",
                                                          "password" => "pass"),
                               "Max" => array(
                                                           "email" => "max@mail.com",
                                                           "website" => "www.max.com",
                                                           "age" => "33",
                                                           "password" => "pass")
); 


This is what is look like:
Array
(
    [John] => Array
        (
            [email] => john@demo.com
            [website] => www.john.com
            [age] => 22
            [password] => pass
        )

    [Anna] => Array
        (
            [email] => anna@demo.com
            [website] => www.anna.com
            [age] => 24
            [password] => pass
        )

    [Peter] => Array
        (
            [email] => peter@mail.com
            [website] => www.peter.com
            [age] => 42
            [password] => pass
        )

    [Max] => Array
        (
            [email] => max@mail.com
            [website] => www.max.com
            [age] => 33
            [password] => pass
        )

)


$user1 = array("email" => "john@demo.com",
               "website" => "www.john.com",
               "age" => "22",
               "password" => "pass");

$user2 = array("email" => "anna@demo.com",
               "website" => "www.anna.com",
               "age" => "24",
               "password" => "pass");

$user3 = array("email" => "peter@mail.com",
               "website" => "www.peter.com",
               "age" => "42",
               "password" => "pass");
$user4 = array("email" => "max@mail.com",
               "website" => "www.max.com",
               "age" => "33",
               "password" => "pass");
/*
$userList = array("John" => $user1,
                  "Anna" => $user2,
                  "Peter" => $user3,
                  "Max" => $user4);
*/
$userList = array();
$newuser = array("John" => $user1,
                  "Anna" => $user2,
                  "Peter" => $user3,
                  "Max" => $user4);
foreach ($newuser as $value){
        $userList[] = $value;
}


echo '
';
print_r($userList);
echo '
'; Array ( [0] => Array ( [email] => john@demo.com [website] => www.john.com [age] => 22 [password] => pass ) [1] => Array ( [email] => anna@demo.com [website] => www.anna.com [age] => 24 [password] => pass ) [2] => Array ( [email] => peter@mail.com [website] => www.peter.com [age] => 42 [password] => pass ) [3] => Array ( [email] => max@mail.com [website] => www.max.com [age] => 33 [password] => pass ) )


Step 3 - Print multidimensional array

 Displaying an element from a multidimensional array is not so complicated.  You just need to define more keys when you want to access a given array value. You can do it like this:

echo "John is ".$userList["John"]["age"]." years old.";



However in some cases it would be fine to print all information about John. You can do this as follows:
foreach ($userList["John"] as $key=>$value) {

echo "John $key - $value 
";

}

And the last variation is if you want to display all information from the array. In this case it's the best to create nested loops like this:


foreach ($userList as $key=>$value) {
     echo "The actual user is $key.
";
     foreach ($value as $iKey => $iValue) {
          echo " ---> $iKey - $iValue 
";
}

}


REFERENCES
http://www.phpf1.com/tutorial/php-multidimensional-array.html?page=1

alternate row colors in PHP/HTML

SkyHi @ Monday, November 22, 2010
<?php
function doAlt($n) {
    if ($n % 2) {
        return 'alt';
    }
}
?>

for ($i = 0; $i <= 4; $i++)  {
    echo '
Row '.$i.'
'; }


$posts = array("Monday","Tuesday","Wednesday","Thursday","Friday","Satursday","Sunday");
$i = '';
foreach($posts as $post){
        echo '
'.$post.'
'; $i++; }


include_once('../smtp/includes/mysqli_connect.php');
$query = "SELECT username, password FROM mailbox";
if ($result = mysqli_query($dbc, $query)) {
        $i='';
        while($row = mysqli_fetch_assoc($result)) {
                echo '
'.$row['username'].$row['password'].'
'; $i++; } /* free result set */ mysqli_free_result($result); } /* close connection */ mysqli_close($dbc);



<style>
.row {
background-color:#000000;
color:#FFFFFF;
width:300px;
}

.rowalt {
background-color:#C0C0C0;
color:#FF0000;
width:300px;
}
</style>


REFERENCES
http://stackoverflow.com/questions/399137/easiest-way-to-alternate-row-colors-in-php-html
http://meetdustin.com/blog/articles/alternating-a-css-class-using-the-modulus-php-arithmetic-operator/
http://www.webdesign.org/web-programming/php/alternating-row-colors.12072.html