Wednesday, November 26, 2008

PHP Code for sending email in HTML view

<?php

$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

// wordwrap mail message.
$message=wordwrap($message,70);


mail($to,$subject,$message,$headers);
?>

Show inside directory file and sub directory with PHP code

//Open images directory
$dir = opendir("images");

//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "
";
}

//Resets the directory stream
rewinddir($dir);

//Code to check for changes

closedir($dir);
?>

Code for copy one file to another file with same directory in php

  1. php
  2. $text = file_get_contents('client.html');
  3. $paste = file_put_contents('client2.html',$text);
  4. if($paste)
  5. {
  6. echo "File copied correctly\n";
  7. } else {
  8. echo "There was a problem copying the file\n";
  9. }
  10. ?>

Wednesday, November 12, 2008

Question: What is the difference between the functions unlink and unset?

Question: What is the difference between the functions unlink and unset?
Answer:
unlink will delete a file and unset would remove a value from a session variable.

Question: What is meant by urlencode and urldecode?

Question: What is meant by urlencode and urldecode?

Answer:
string urlencode(str) where str contains a string for example “hello world” and the return value will be URL encoded and can be use to append with URLs‚ normaly used to appned data for GET like someurl.com?var=hello%world string urldocode(str) this will simply decode the GET variable’s value for example

echo (urldecode($_GET_VARS[var])) will output “Hello world”

Question: What is the functionality of the function strstr and stristr?

Question: What is the functionality of the function strstr and stristr?
Answer:
string strstr (string str1‚ string str2) this function search the string str1 for the first occurrence of the string str2 and returns the part of the string str1 from the first occurrence of the string str2. This function is case-sensitive and for case-insensitive search use stristr() function.


Example of strstr: $email = ’name@example.com’;
$domain = strstr($email‚ ’@’); echo $domain; // prints @example.com $user = strstr($email‚ ’@’‚ true); echo $user; // prints name ?>

Example of stristr: $email = ’USER@EXAMPLE.com’; echo stristr($email‚ ’e’); // outputs ER@EXAMPLE.com echo stristr($email‚ ’e’‚ true); // outputs US ?>

Question: What are the different types of errors in PHP?

Question: What are the different types of errors in PHP?

Answer:
Three are three types of errors:

1) Fatal errors.

2) Parser errors.

3) Startup errors.

Question: How can we encrypt the username and password using PHP?

Question: How can we encrypt the username and password using PHP?

Answer:
PHP has an md5 function that would normally suffice. Also‚ since the password function in mysql can change between versions‚ using PHP’s md5 function would be better here. As an aside‚ concatonating the record id (or something else handy) before hashing will stop different users with the same password getting the same hash.

Question: What is meant by nl2br()?

Question: What is meant by nl2br()? Answer:
nl2br() inserts html in string.

Example: echo nl2br(”god bless \n you”);
output: god bless you

Question: Suppose your Zend engine supports the mode Then how can u configure your PHP Zend engine to support mode?

Question: Suppose your Zend engine supports the mode Then how can u configure your PHP Zend engine to support mode?
Answer:
In php.ini file: set short_open_tag=on

Question: How can I execute a PHP script using command line?

Question: How can I execute a PHP script using command line?
Answer:
For command line‚ we have to use php CLI(command line interface).

Question: Can we use include ("abc.php") two times in a php page "makeit.php"?

Question: Can we use include ("abc.php") two times in a php page "makeit.php"?
Answer:
Yes‚ and we can use it more then two times.

Question: What are the differences between require and include‚ include_once?

Question: What are the differences between require and include‚ include_once?
Answer:
require_once()‚include_once() both the functions include and evalute the specified file only once and if the specified file is opened previous to the present call occurrance‚ it will not be done again. But require() and include() will do it as many times they are asked to do. The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement‚ with the only difference being that if the code from a file has already been included‚ it will not be included again. The major difference between include() and require() is that in failure include produces a warning message whereas require produces a Fatal errors.

Question: How can we create a database using php and mysql?

Question: How can we create a database using php and mysql?

Answer: mysql_create_db()

Example:

$link = mysql_connect(’localhost’‚ ’mysql_user’‚ ’mysql_password’);

if (!$link)

    {

        die(’Could not connect: ’ . mysql_error());

    }

$sql = ’CREATE DATABASE my_db’;

if (mysql_query($sql‚ $link))

    {

        echo "Database my_db created successfully\n";

    }

else

    {

        echo ’Error creating database: ’ . mysql_error() . "\n";

    }

?>

Monday, November 10, 2008

Question: What is the difference between mysql_fetch_object and mysql_fetch_array?

Question: What is the difference between mysql_fetch_object and mysql_fetch_array?
Answer:
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array. mysql_fetch_object() is similar to mysql_fetch_array()‚ with one difference - an object is returned‚ instead of an array.
Indirectly‚ that means that you can only access the data by the field names‚ and not by their offsets.
MySQL_fetch_array() - Fetches a result row as an associative array‚numeric array.
MySQL_fetch_object() - Fetches a result row as an object.
MySQL_fetch_row() - Fetches a result set as a numeric array().

Question: How many ways we can retrieve the date in result set of mysql using php?

Question: How many ways we can retrieve the date in result set of mysql using php?
Answer:
- As individual objects.

- As a Single record.
- As a set or arrays.

Question: How can we submit from without a submit button?

Question: How can we submit from without a submit button?
Answer:
Trigger the JavaScript code on any event (like onselect of drop down list box‚ onfocus‚ etc) The javscript code to submit the form is: document.myform.submit();

Question: What are the differences between Get and post methods in form submitting?

Question: What are the differences between Get and post methods in form submitting?
Answer:
A. In the get method the data made available to the action page (where data is received) by the URL so data can be seen in the address bar. Not advisable if you are sending login info like password etc. In the post method the data will be available as data blocks and not as query string in case of get method.


The key difference between GET and POST is that POST has side-effects‚ and thus browsers will present a confirmation dialog to the user on refresh or "back".

Question: What is the difference between md5()‚ crc32() and sha1() crypto on PHP?

Question: What is the difference between md5()‚ crc32() and sha1() crypto on PHP?
Answer:
All these functions generate a hash code and the basic difference among them is the length of the generated hash. 1) crc32() - Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the string. This is usually used to validate the integrity of data being transmitted. Generates 10-character hexadecimal number. 2) sha1() - Calculates the sha1 hash of a string using the "US Secure Hash Algorithm 1". Generates 40-character hexadecimal number. 3) md5() - Calculates the MD5 hash of a string using the "RSA Data Security‚ Inc. MD5 Message-Digest Algorithm" and returns that hash. Generates 32-character hexadecimal number.

Question: Who is the father of php?

Question: Who is the father of php?
Answer: Rasmus Lerdorf

Question: What is difference between PHP and HTML?

Question: What is difference between PHP and HTML? Answer:
HTML files are requested by browser‚ and returned by server. PHP files are requested by browser‚ and executed by the server to output a plain HTML that is returned to the browser.

What is PHP?

Question: What is PHP?
Answer: * PHP stands for PHP Hypertext Preprocessor.
* PHP scripts run inside Apache server or Microsoft IIS.
* PHP and Apache server are free.
* PHP is the most used server side scripting language.
* PHP files contain PHP scripts and HTML.
* PHP files have the extension “php”‚ “php3”‚ “php4”‚ or “phtml”.

Friday, November 7, 2008

What is the purpose of the following files having extensions: frm, myd, and myi

What is the purpose of the following files having extensions: frm, myd, and myi? What these files contain? In MySQL, the default table type is MyISAM.Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type.The '.frm' file stores the table definition.The data file has a '.MYD' (MYData) extension.The index file has a '.MYI' (MYIndex) extension,

What is the difference between the functions unlink and unset?

What is the difference between the functions unlink and unset?
unlink() is a function for file system handling. It will simply delete the file in context.unset() is a function for variable management. It will make a variable undefined.

How can we register the variables into a session?

How can we register the variables into a session?

session_register($session_var);$_SESSION['var'] = 'value';

How can we submit form without a submit button?

How can we submit form without a submit button?
We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form.

What’s the output of the ucwords function in this example?

What’s the output of the ucwords function in this example?
$formatted = ucwords("CENTER IS COLLECTION OF INTERVIEW QUESTIONS");print $formatted;
What will be printed is CENTER IS COLLECTION OF INTERVIEW QUESTIONS.ucwords() makes every first letter of every word capital, but it does not lower-case anything else. To avoid this, and get a properly formatted string, it’s worth using strtolower() first.

What’s the difference between htmlentities() and htmlspecialchars()?

What’s the difference between htmlentities() and htmlspecialchars()?
htmlspecialchars only takes care of <, >, single quote ‘, double quote " and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.

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

How can we destroy the session, how can we unset the variable of a session?
session_unregister() - Unregister a global variable from the current sessionsession_unset() - Free all session variables

Tuesday, November 4, 2008

What is password shadowing?

Question: What is password shadowing?
Answer: Password shadowing is a security system where the encrypted password field of /etc/passwd is replaced with a special token and the encrypted password is stored in a separate file (or files) which is not readable by normal system users

what is substr_replace?

Question: what is substr_replace?
Answer: It is a function in php it takes four arguments and is used for substituting the a string

Difference between hashing and encryption?

Question: What is the difference between hashing and encryption?

Answer:
Encryption is a scheme where an intelligible text (plaintext in crypto terms) is made unintelligible (ciphertext in crypto terms) using a secure key. Block and stream ciphers and public key systems do this work. The security of the ciphers reside in the key length and decryption process is a difficult without proper knowledge of the key. But in hashing‚ they are one-way functions that compress arbitrary length strings into fixed short strings (message digests). Hash Functions can be designed using block ciphers using a secret key as a parameter along with the message that has to be hashed or with out them (dedicated hash functions MD4‚MD5‚SHA-1 etc..).

security header validate

  HTTP Security Headers Check Tool - Security Headers Response (serpworx.com)