PHP

PART I : The PHP Language

In this section we will cover:

  1. PHP Tags
  2. File inclusion
  3. Data types & typecasting
  4. Variables and constants
  5. Operators
  6. Conditionals
  7. Iteration
  8. Functions
  9. Objects
Tags
Tags “drop” you out of HTML and into PHP mode
PHP recognizes several types of tags:
Short tags: <? ?>
Special tags: <?= ?>
Regular tags: <?php ?>
ASP tags: <% %>
HTML script tags.
File Inclusion
External files can be included in a script using either include() or require()

Auto Page Refresh using PHP

For refreshing a PHP page after a pre-defined amount of time for what ever reason like automatically refreshing the home page (or subpages) for updated news, just add the following script at the very beginning of your page. Any other header sent before this header function will make it cause an error.

<?php
header("Refresh: 60;");
?>

NOTE: The above code will make the current page automatically reloaded after 1 minutes, here the time is expressed in seconds.

You can also redirect to another page of your site or an external site after pre-determined time.

The following code will send the visitor to the blog (assuming you have the blog running under the folder "blog") after 10 seconds.

<?php
header("Refresh: 10; url=/blog/");
?>

This will send the visitor to www.php.net after 5 seconds.

<?php
header("Refresh: 5; url=http://www.php.net");
?>

How to organize the code for web application

Here I will discuss a few ways to organize the code in your web application. Although I cannot present you with every possible way of organizing code, I can at least discuss some of the most common ways.

One Script Serves All
One script serves all stands for the idea that one script, usually index.php,handles all the requests for all different pages. Different content is passed as parameters to the index.php script by adding URL parameters such as ?page=register.

In this application, a specific file and class can handle the request. You can imagine that, in case you have many different modules, the switch case will grow large, so it might be worthwhile to do it dynamically by loading a number of modules from a dedicated directory, like the following (pseudo code):

foreach (directory in "modules/") {
    if file_exists("definition.php") {
        module_def = include "definition";
        register_module(module_def);
    }
}

Fetch a URL with Cookies in PHP

Incase if you need to retrieve a page that needs a cookie to be sent along with the request for the page, just use the cURL extension and the CURLOPT_COOKIE option:

$c = curl_init('http://www.example.com/needscookies.php');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_COOKIE, 'user=kiran; activity=climbing');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);

If you don't want cURL, you can try the addHeader( ) method in the PEAR HTTP_Request class:

require 'HTTP/Request.php';
$r = new HTTP_Request('http://www.example.com/needs-cookies.php');
$r->addHeader('Cookie','user=ellen; activity=swimming');
$r->sendRequest();
$page = $r->getResponseBody();

Cookies are sent to the server in the Cookie request header. The cURL extension has a cookie-specific option, but with HTTP_Request, you have to add the Cookie header just as

Prevent Multiple Form Submission in PHP

To prevent multiple form submission just generate a unique identifier and store the token as a hidden field in the form. Immediately before form processing, check to see if that token has already been submitted. If it hasn't, you can proceed; if it has, you should generate an error.

While creating the form, use uniqid( ) to get a unique identifier:

<?php
$unique_id = uniqid(microtime(),1);
...
?>

Then, when processing, look for this ID:

$unique_id = $dbh->quote($_GET['unique_id']);
$sth = $dbh->query("SELECT * FROM database WHERE unique_id = $unique_id");
if ($sth->numRows( )) {
    // already submitted, throw an error
} else {
   // act upon the data
}

For numerous reason, users may resubmit a form. Usually it may be a slip-of-the-mouse: double-clicking the Submit button. They may hit their web browser's Back button to edit or recheck
information, but then they re-hit Submit instead of Forward.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks