Monday, 20 January 2014

send form data in email javascript or php

In web development it is very normal that we create 'Contact-Us' Form . And we want to send complete form submitted data in a mail to somebody (normally to ourself ).
There are many ways to do this .
1) using javascript
2) using any server side scripting like PHP .

To understand this first of all, we need to understand concept of sendmail utilites and mail server . for sending mail , we need to have a mail server or mail client . best example for mail client is MS-outlook and mail client is "mail" command in linux .
First way -- javascript  -->
As we know javascript is client side scripting , so it need a client to send mail i.e. mail client .
Example :-
lets take a simple form .
<form action="">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" /> 
<input type="submit" name="submit" value="submit"/>

This is simplest example . If we try to see what happens here , we are using javascript "mailto" function . "mailto" will trigger mail client to send mail that means , it will call you MS-Outlook (if installed and configured ) .This way is not used widely as it is client dependent way and we don't have any control here .

Second way -- server side scripting e.g. PHP -->
php will need a mail server configured to send a mail . As we are developers we can control and configure mail server by our own .
Example :-
<form action="abc.php" method="post">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" /> 
<input type="submit" name="submit" value="submit"/>

$name = $_POST['name'];
$phone = $_POST['phone']; 
$to = "";
$subject = "Contact details ";
$message = "The name is  $name \n\r Contact number is $phone ";



Now this is stable and trusted code to send mails . And everyone should use this way of sending mail .

There are two ways of submitting form i.e. GET and POST . Both methods have their importances .
If we submit form using get method and then all data submitted will be append in the action page url . And we know that url have its limits so we can send only small amount of data using GET method .
While in POST method data is sent in the request body so there is no limit here to send data .
Also in GET method data is visible in th url , so it is not safe . Whenever we need to create safe application we should use POST method .
GET method is fast as everything is simply appended in the url . But in using POST method every request need to maintain data in the body . So it is lsightly time consuming method .

Both methods are useful . So we need to choose wisely according to our application taht which method we should use .

Thank you . Please ask in comments if need any clarification . Suggestions are also welcome .

Thursday, 19 December 2013

php strtotime and mktime time functions

We have discussed about php's date and time functions . for refrence please see HERE
Now we are discussing two more functions . strtotime() and mktime() .


              This function will convert a string to timestamp format i.e. total number of seconds since 1st jan 1971 .
its usages are like :-

$var1 = strtotime("now");
$var2 = strtotime("10 September 2000");
$var3 = strtotime("+1 day");
$var4 = strtotime("+1 week");
$var5 = strtotime("+1 week 2 days 4 hours 2 seconds");
$var6 = strtotime("next Thursday");
$var7 = strtotime("last Monday");

echo date("Y-m-d H:i:s",$var1) . "<br/>";
echo date("Y-m-d H:i:s",$var2) . "<br/>";
echo date("Y-m-d H:i:s",$var3) . "<br/>";
echo date("Y-m-d H:i:s",$var4) . "<br/>";
echo date("Y-m-d H:i:s",$var5) . "<br/>";
echo date("Y-m-d H:i:s",$var6) . "<br/>";
echo date("Y-m-d H:i:s",$var7) . "<br/>";

explanation of above mentioned code is -
all $var1-7 variable will get timestamp according to string passed to the function  .
'now' will give current timestamp
'+1 day' will give tomorrow's timestamp i.e. +24 hours
and so on ..

also we can find the difference between two dates .
example : suppose we need to find the difference in days between '10 december 2012' and '12 july 1947' .  then we will write that program like this
$time1 = strtotime( "10 december 2012");
$time2 = strtotime( "12 july 1947");
$diff = $time1-$time2 ;
$no_of_days = $diff/86400 ;          // 1 day = 86400 seconds .

mktime :-

             This functions also create timestamp , but it take values as different parameters instead of one complete timed string .following is the syntax for mktime() function
mktime(hour,minutes,seconds,month,date,year,is_dst) ;
all parameters are optional .If we pass each parameter then it will show you the results. Just give a small try to the programs and will understand quickly , what it means . for example -
echo date("M-d-Y",mktime(0,0,0,12,36,2012)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,14,1,2010)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,1,1,1991)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,1,1,99)) . "<br>";

Hope this post will help someone . Please ask me in comments if need any more help .

Thursday, 5 December 2013

advanced linux commands for quick system analysis

There are many linux commands which helps in many ways . If you are not linux administrator then these commands can help you in many ways .
Following commands are very important .

who am i

ifconfig :- 

this command is used to setup network details . Also we can see ip address of the system . there are many uses of this command . complete usage of this command can be seen use command  "man ifconfig " .
some uses of that commands are :-
ifconfig -a   --> will show all details of networks 
ifconfig eth0 --> will show details of fist ethernet network
 ifconfig eth0 down --> will take first ethernet down  or shutdown ethernet first
 ifconfig eth0  up -->  just opposite to the down command . it will start firsth ethernet
ifconfig eth0  netmask -->  will set system ip to and netmast to

netstat :-

 netstat is very important command to use on servers . it shows details about all port activities . which port are working and how many connections on the ports at any time . complete usage of this command can be seen using command "man netstat" .
Some uses of this commands are :-
netstat -a --> will show all port activities
netstat -at --> will show all TCP port activities
netstat -au --> will show all UDP port activities
netstat -l --> ports which are listening
netstat -tl --> TCP ports which are listening
netstat -s --> statistics for all ports
netstat -st --> statistics for alls TCP ports only
netstat -n --> will show hosts in numeric form i.e. in ip address forms
netstat -c --> continuously

use many commands combinations to get your desired results . like :-
netstat -atn | grep ":8080 "  --> it will show connections on only 8080 port
 netstat -tan | grep ':80 ' | awk '{print $6}' | sort | uniq -c
1884 FIN_WAIT1

nslookup :-

this command is used to see dns entries of any domain or ip . like
nslookup --> it wil show details about , its ip and max available info
nslookup --> will show details about this ip address
nslookup --> will show details of from dns

 traceroute :-

this command is used to trace route between your system to destination . use :-
traceroute --> will show all nodes of connectivity between your system and servers .

iostat :-

this command is used to get all input and output statistics over all hard disks. to install this command in system use  "yum install sysstat " .uses :-
iostat --> will show average of input and output operation on every disk partition .
iostat 2 --> will show continuously current io statistics .


mpstat shows output stats of every processor available .
mpstat , mpstat -P all --> will how all processors statistics .
mpstat -P 0 --> will show just first processor output statistics .

uptime :-

it will show uptime of your server . that means from how much time your server is running . This will reset after restart of server . to use it just write uptime in shell and hit enter

wall :-

this command is used to send a message to all shells of that system . Suppose many people are logged in on that server . If any one want to convey any message to all people then use wall .like :-
wall "dont delete anyfile from directory /home/parveen" --> the message will appear to every one logged in the server

w,who,whoami ,who am i :-

these three commands will provide you details about how many user are logged in the system and from how much time .
w --> will show details of all logged in users .
who -- > similar to w but show less details .
whoami --> will show you your username .
who am i --> will show you more details about your login .

top :-

top is most basic and mostly used command . it can provide complete overview of your system . This will show CPU usage , RAM usage , load on system ,which process is using maximum system .etc .

htop :-

slightly modified version of top command . It show use individually of every CPU .

rsync :- 

this command is used to make two directoris in sync with each other . This can be used over two different systems or on the same system itself .

rsynch -uar /home/parveen/   /home/backup/ --> this will send all updated file from /home/parveen/ directory to /home/backup directory .
rsync --rsh='ssh -p22' -aur root@    /home/parveen/images/ --> this command will get all files from different machine to current machine .

lsof :- 

it will provide list of open files . there are many uses of this command . like :-
lsof --> list all opened files on the system
lsof /home/parveen/parveen.log --> will show open statistics for specific file  /home/parveen/parveen.log
lsof +D  /home/parveen/  --> will show files open under directory /home/parveen/
lsof -u root --> will show files opened by user root .
lsof -p 10 --> will specify opened file by specific process .i.e process id 10 .

find :- 

this command is used to search any file in the system .this can be used like that :-
find /home/ -name abc.txt --> this command will try to search file abc.txt under directory /home/ .
find / -name abc.txt --> this command will find abc.txt in the whole system .
find . -name abc.txt --> this command will find abc.txt in the current directory .

whereis :-

this command will search the location of commands . like:-
whereis cp --> will show the location of cp command . This may also search is binary files .

alias :-

this command is used to create aliasing for any command. this command is helping to create shortcuts of commands .like :-
alias pp="ls -lhtr" --> after this if you run pp command this it will show results equivalent to ls -lhtr


this command will remove the alias created .like:-
unalias pp --> now pp will no longer be any alias .

df :-

this command is used to show storage usage of all hard disk partitions . like :-
df -k --> this will show all partitions witgh there usage and free memory available in bits.
df -kh --> this will show size in MB KB and GB format .

du :-

this command is used to show disk usage by directories and files . this can be used in maby ways .
du --> will show directories which are using some space that means files which are non empty .
du -a --> this comamnd will show all files . also will show empty files
du -ah --> will show memory in human readle form like KB MB GB .
du -s --> will show summarised memory .
du -sh * --> will show memory taken by all files and directory at your current location with human readable form .


this command is used to find difference between two files . for comparison of two files this is well structured command .
diff file1 file2 --> will print differences between this two files  .

Thursday, 21 November 2013

Basics commands of Unix / Linux


I will explain all basics of unix /linux here . Firstly basic directories of unix operating system .

** /bin  --> this directory contains all essential files for correct functioning of operating system .all users have permissions of these files .
** /home  in this directory all user's home directory resides . This is very commonly used directory .
** /var this directory contains all variables files like  logs , db directory etc .
** /etc all system configuration files are stored here .
** /dev  additional devices like cd-rom , pendrive , hard disk will found here .
** /sbin binary files , generaly super user uses these files .
** /tmp  all temporary files stay here .

These locations are by default . For our ease we can change them also and we can use it .

Linux is case sensitive .

Permissions :-  
As linux is multi-user operating system . Every file and directory have permissions defined for every user . Every file and directory have their owner . owner is a user . and also a group . group is group of users .

So we have three different type of permissions .
1) permissions given to owner
2) permissions given to group
3) permissions given to all other users .

And every file have three types of permissions
1) read permission -- those users who have read permission can read the file
2) write permission -- those users who have read permission can write into the file
3) execute permission -- user with this permission can execute the file .

Do  ls –l    in any directory
Then you will see something like
-rw-r--r--    1 root   root     13 June  5  11:21 file1
Here following will explain this .
r:readable,  w:writable,  x: executable

we can use two commands  chmod and chown to change permissions and owner of the file respectively .

Now about commands of unix .
One command consists of three parts  i.e command name , options , parameters .
example   # grep -i "abcds" file.txt
here ,
grep --> command name
i --> option
abcds and file.txt  are the parameters of the command .

Options will always start with - mark .

basic commands are :-
command name                     function

ls                                     show files in current position
 cd                                  change directory
 cp                                  copy file or directory
 mv                                 move file or directory
 rm                                 remove file or directory
 pwd                              show current position
 mkdir                           create directory
 rmdir                            remove directory
 less, more, cat          display file contents
 man                              display online manual
su                                   switch user
passwd                        change password
useradd                      create new user account
userdel                        delete user account
mount                         mount file system
umount                       unmount file system
df                                   show disk space usage
shutdown                   reboot or turn off machine
sort                            this command is used to sort 

to understand use of any command use man command
man <comand name >
e.g  man cp  -->> this comand will explain everything about cp

run some commands in your linux/unix machine then you will understand the difference .

ls a
ls la
ls -Fa  
ls  .bash_profile
cp  .bash_profile  sample.txt
less  sample.txt  (note: to quit less, press q)
rm  sample.txt
mkdir linux
 cd linux
rmdir linux
ls  .bash_profile
 cp  .bash_profile  sample.txt
 less  sample.txt  (note: to quit less, press q)
 rm  sample.txt
df -k
df -hk

 Paths:-   relative and absolute path
Absolute Path :- 
Absolute pathe is address from root path . like 

Relative path :-
this path is relative to your current path 
. /    your current location
../   one directory above your current location
pwd    it will show you your current directory 

Run following command one-by-one and you will understand 
cd .
 cd ..
 cd ..
  mkdir mydir
 cd /Users/invite
 cd /Users
 cd /
 cd /Users/invite
cd ~/mydir 

Redirect, Append and Pipe 
We know output of a command is shown on the screen . i f we want to process the output then we will need some special parts .
Pipe  "|" :-
some commands need input from another command or any file .
"|" pipe will help u in giving output of one command as input to another command 

Append and redirect :-

'>' this sign will redirect output of a command to a file .
         '>>' this sign will append all output to the end of the file.

Please ask in comments if you need any more clearifications .