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
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 ]]]]]] )
6:35 PM
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().
6:27 PM
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
6:26 PM
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']);.
6:29 PM
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];
6:28 PM
$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].
Array sorting functions in PHP:
asort()
arsort()
ksort()
krsort()
uksort()
sort()
natsort()
rsort()