Is PHP limited to process GET and POST request methods only?


Is PHP limited to process GET and POST request methods only?

No, it is possible to handle any request method, e.g. CONNECT. Proper response status can be sent with header(). If only GET and POST methods should be handled, it can be achieved with this Apache configuration:

<LimitExcept GET POST>
Deny from all
</LimitExcept>

How do I add my PHP directory to the PATH on Windows?


How do I add my PHP directory to the PATH on Windows?

On Windows NT+ and Windows Server 2000+:

  • Go to Control Panel and open the System icon (Start -> Settings -> Control Panel -> System, or just Start -> Control Panel -> System for Windows XP/2003+)
  • Go to the Advanced tab
  • Click on the ‘Environment Variables’ button
  • Look into the ‘System Variables’ pane
  • Find the Path entry (you may need to scroll to find it)
  • Double click on the Path entry
  • Enter your PHP directory at the end, including ‘;’ before (e.g. ;C:\php)
  • Press OK

On Windows 98/Me you need to edit the autoexec.bat file:

  • Open the Notepad (Start -> Run and enter notepad)
  • Open the C:\autoexec.bat file
  • Locate the line with PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;….. and add: ;C:\php to the end of the line
  • Save the file and restart your computer

Note: Be sure to reboot after following the steps above to ensure that the PATH changes are applied.

The PHP manual used to promote the copying of files into the Windows system directory, this is because this directory (C:\WindowsC:\WINNT, etc.) is by default in the systems PATH. Copying files into the Windows system directory has long since been deprecated and may cause problems.

Is it possible to send HTML mail with php?


Is it possible to send HTML mail with php?

Yes, using the mail() function of PHP, HTML emails can be sent. The content type header must be mentioned.

<?php
// multiple recipients
$to = ‘example@example.com’
// subject
$subject = ‘HTML email’;

// message
$message = ‘
<html>
<head>
<title>HTML EMAIL</title>
</head>
<body>
<p>Sample of HTML EMAIL!</p>
</body>
</html>
‘;

// To send HTML mail, the Content-type header must be set

$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

// Additional headers
$headers .= ‘To: Joe <joe@example.com>, Kelly <kelly@example.com>’ . “\r\n”;
$headers .= ‘From: HTML EMAIL <sample@example.com>’ . “\r\n”;
$headers .= ‘Cc: samplecc@example.com’ . “\r\n”;
// Mail it
mail($to, $subject, $message, $headers);
?>

Explain how to generate a random number from php.


Random number is generated by using the function rand(),mt_rand(). If the function is used without sending a parameter, a random number between 0 and RAND_MAX is returned.

Ex. echo rand();

To return a random number between 120 and 200 inclusive, the code snippet is

echo rand(120,200);

For returning a random number using the Mersenne Twister algorithm, use mt_rand().

The function srand() is used to seed the random number, and it must be used once in a script and before the rand() function. If the randomness of the seed is greater, more random numbers can be generated.

Ex.
srand((double)microtime()*1000000);
echo rand(0,100);

PHP Superglobals


PHP Superglobals.

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.

 

  • $_COOKIE – Represents data available to the PHP script via HTTP cookies.
  • $_GET – Represents data sent to the PHP script using HTTP get method.
  • $_POST – Represents data sent to the PHP script using HTTP post method.
  • $_REQUEST – It is a combined array containing values from the $_GET, $_POST, and $_COOKIES superglobal arrays.
  • $_ENV – Represents data available to the PHP script from the environment in which PHP is running.
  • $_FILES – Represents data sent to the PHP script from HTTP POST of uploaded files.
  • $_SERVER – It contains variables made available by the web server server.
  • $GLOBALS – It contains all the global variables associated with the current script.

How Session Work


Session are handled in PHP by using the function session_start(). This function should be invoked before any HTML tags in the script.

Ex.
<?php
session_start( );
?>
<html>
<head> ……. etc

A random session id is generated by the function session_start(). This session id is persisted in a cookie on the client / user computer. To refer the cookie, the reference variable $PHPSESSID is used. This is the default session id. The session id can be changed in the PHP configuration files on the server.

Even the user returns this page after visiting some pages, PHP keeps track about this session and its progress. Subsequent to this process, the session can be used as follows –

print $PHPSESSID;
session_register(“username”);
print $username;

The user name is registered and by using session variable name username and used it for display.

Creating Your First PHP Cookie


 

Creating Your First PHP Cookie.

Cookie in PHP is used to identify a user.

Create a cookie:

Setcookie() can be used for this purpose. It appears before the <html> tag.
setcookie(name, value, expire, path, domain);

Example:
Here, a cookie name user is assigned a value Sample. The cookie expires after an hour
setcookie(“user”, “Sample”, time()+3600);

Retrieve a cookie:
The cookie can be retrieved using PHP $_COOKIE variable

Echo the value:
<?php
echo $_COOKIE[“user”];
?>

Creating Your First PHP Cookie.

A cookie is created by using setCookie() function. This function takes 3 arguments:

– Name – The name of the cookie to be used to retrieve the cookie.

– Value – The value is persisted in the cookie. Usually, user names and last visit date / time are used

– Expiration – The date until which the cookie is alive. After the date the cookie will be expired. If the expiry date is not set, then the cookie is treated as a default cookie and will expire when the browser restarts.

Example code:

<?php

//Set 60 days as the life time for the cookie.
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie(‘lastVisit’, date(“G:i – m/d/y”), $inTwoMonths);

?>