How to set cookies?How can we know that a session is started or not?How can we encrypt the username and password using PHP?How can we destroy the session, how can we unset the variable of a session?How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP?What are the different functions in sorting an array? ~ Interview Questions & Answers

Tuesday, June 3, 2008

How to set cookies?

setcookie('variable','value','time');
variable - name of the cookie variable
value - value of the cookie variable
time - expiry time
Example: setcookie('Test',$i,time()+3600);
Test - cookie variable name
$i - value of the variable 'Test'
time()+3600 - denotes that the cookie will expire after an one hour

Syntax:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

How can we know that a session is started or not?

A session starts by session_start() function.
This session_start() is always declared in header portion. it always declares first. then we write session_register().

Monday, March 3, 2008

How can we encrypt the username and password using PHP?

User names and passwords in PHP can be encrypted using md5 function.
MD5 function calculates the md5 hash of a string. It is basically used for encryption. It is also used for digital signature applications, where a large file must be "compressed" in a secure manner.
Example:
Md5($str);
Crypt() function can also be used to encrypt a string,. It used MD5, DES or blow fish algorithms for encryption.
Syntax:
Crypt(str, salt)
Salt is an optional parameter used to increase the number of characters encoded, to make the encoding more secure

How can we destroy the session, how can we unset the variable of a session?

session_unset() function frees all session variables currently registered.
If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable.
i.e. unset ($_SESSION['varname']);.

Sunday, February 3, 2008

How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?

We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern.
For example:
preg_match("/.*@(.*)$/","http://info@abc.com",$data);
echo $data[1];

How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP?

$text = "mailto:info@abc.com?subject=Feedback";
preg_match('|.*@([^?]*)|', $text, $output);
echo $output[1];
Note that the second index of $output, $output[1], gives the match, not the first one, $output[0].

Saturday, September 8, 2007

What are the different functions in sorting an array?

Array sorting functions in PHP:
asort()
arsort()
ksort()
krsort()
uksort()
sort()
natsort()
rsort()