What type of inheritance that PHP supports? Provide an example.
Correct Answer
PHP supports single level inheritance Inheriting a class would mean creating a new class with all functionality of the existing class in addition to some more The created class is called as a subclass of the parent class Parent is a keyword which we use to access members of a parent class Inheritance is usually defined by using the keyword ?Extend? $this is used a reference to the calling object Inheritance avoids redundancy of code Example: Class employee extends manager : Here Employee is a subclass of manager Echo $this->name can be used to echo variable name of the parent class
Correct Answer: Generating random passwords in PHP can be done in multiple ways depending on how much security the application demands:- Md5 function can be passed one parameter as uniqid() function which in turn generates a random number Passing the parameter as TRUE in uniqid() adds additional uniqueness
2. Explain how to store the uploaded file to the final location.
Correct Answer: A HTML form should be build before uploading the file The following HTML code is used for selecting the file that is to be uploaded The type attribute of input must be ?file? At the time of executing uploaderphp, the uploaded file will be stored in a temporary storage are on the webserver An associative array $_FILES['uploadedfile']['name'] is used for uploading The ?name? is the original file that is to be uploaded Another associative array $_FILES['uploadedfile']['tmp_name'] is used for placing the uploaded file in a temporary location on the server and the file should be empty and should exist with the tmp_name The following code snippet is used for uploading the file $target_path = "uploads/"; // the target location of the file $target_path = $target_path basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file " basename( $_FILES['uploadedfile']['name']) " has been uploaded"; } else { echo ?There was an error while uploading the file?; } The function move_uploaded_file()is used to place the source file into the destination folder, which resides on the server If the uploading is successful, the message ?The file filename has been uploaded Otherwise the error message ?There was an error while uploading the file? would be displayed
3. Explain how to send large amounts of emails with php.
Correct Answer: The mail() function of PHP is quite robust for sending bulk emails A SMTP server can also be directly used from the script PHPmailer class can be used for sending emails
Correct Answer: To access the global data which is originating externally, the super globals are used The superglobals are available as arrays These superglobals represents the data from URLs, HTML forms, cookies, sessions and also the web server For this purpose, $HTTP_GET_VARS, $HTTP_POST_VARS are used The super globals are pretty good enough to be used in functions The following are the list of super globals $_GET, $_POST, $_COOKIE;$_REQUEST, $_SERVER, $_SESSION, $_ENV, $_FILE Another PHP Superglobal, called $GLOBALS; is available for persisting every variable with global scope
Correct Answer: Permissions in PHP are very similar to UNIX Each file has three types of permissions ? Read, write and execute Permissions can be changed using the change mode or CHMOD command CHMOD is followed by 3 digit number specifying the permission type CHMOD 777 is for Read, Write and execute Unnecessary permissions can be stripped off using UNMASK command Unmask (777);
6. Explain when to use GET or POST. Provide example for both the ways.
Correct Answer: Both GET and POST are used to collect data from a form However, when security is desired $_POST should be used When the $_ POST variable is used, all variables used are NOT displayed in the URL Also, they have no restrictions to the length of the variables GET should be used when the interaction with the form is more like a question For eg firing a query etc POST should be used more often when the interaction is more like an order or the interaction changes the state of the resource POST example: On clicking submit the URL resembles to: https://wwwmysamplesitecom/samplephp The samplephp file can be used for catching the form data using $_POST Hello You are years old! GET EXAMPLE On clicking submit the URL resembles to : https://wwwmysamplesitecom/samplephp?name=jim&age=37 The samplephp file can be used for catching the form data using $_GET Hello You are years old!
Correct Answer: The function setcookie() is used to define a cookie that is to be sent along with HTTP headers The cookie must be sent prior to any output from the script as is the protocol restriction After setting the cookies, they can be used when the next page is loaded by using $_COOKIE or $HTTP_COOKIE_VARS arrays
Correct Answer: A PHP Session persist the user information to be used later For example, user name, password, shopping item details The session is temporary and will be removed soon after the user has left the web site The session can be persisted for long term usage on databases like MySQL Each session is identified by a unique Id number for every visitor The first step to use PHP session is to start the session The starting session must precede the operations like HTML or the other The statement session_start() starts the PHP session and registers the user?s information on the server
9. How can we encrypt the username and password using PHP?
Correct Answer: 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
10. Explain the changing file permission and ownership using PHP's chmod() function.
Correct Answer: Chmod() is used for changing permissions on a file Syntax: Chmod(file, mode) Mode here specifies the permissions as follows: The first number is always zero The second number specifies permissions for the owner The third number specifies permissions for the owner's user group The fourth number specifies permissions for everybody else Possible values (to set multiple permissions, add up the following numbers) 1 = execute permissions 2 = write permissions 4 = read permissions Example: // everything for owner, read for owner's group chmod("testtxt",0740);