chp2 - Sem6 Bca
chp2 - Sem6 Bca
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
• PHP $_POST
• PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also
widely used to pass variables.
• <html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
• PHP $_GET
• PHP $_GET can also be used to collect form data after
submitting an HTML form with method="get".
• $_GET can also collect data sent in the URL.
• <html>
<body>
<a href="test_get.php?
subject=PHP&web=W3schools.com">Test $GET</a>
</body>
</html>
• <html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
Self Processing Forms
1. It is easy to process forms in PHP as the form parameters are
available in the $_GET and $_POST arrays.
2. there are two HTTP methods that a client can use to pass
form data to the server: GET and POST.
3. A GET request encodes the form parameters in the URL, in
what is called a query string:
4. A POST request passes the form parameters in the body of
the HTTP request, leaving the URL untouched.
5. The most visible difference between GET and POST is the
URL line.
6. Because all of a form's parameters are encoded in the URL
with a GET request, users can bookmark GET queries. They
cannot do this with POST requests.
• Self processing Pages:
• One PHP page can be used to both generate a form and
process it.
• PHP and Apache normally take care of the headers for you,
identifying the document as HTML, calculating the length of the
HTML page, and so on.
• <?php
• header('Location:http://www.example.com/elsewhere.html');
exit( );
• ?>
• If you provide a partial URL the redirection is handled
internally by the web server.
• Expiration
• A server can explicitly inform the browser, and any proxy
caches that might be between the server and browser, of a
specific date and time for the document to expire.
• Proxy and browser caches can hold the document until that
time or expire it earlier.
• Repeated reloads of a cached document do not contact the
server.
• However, an attempt to fetch an expired document does
contact the server.
• To set the expiration time of a document, use the Expires
header:
• header('Expires: Fri, 18 Jan 2002 05:30:00 GMT');
• To expire a document three hours from the time the page was
generated, use time( ) and gmstrftime( ) to generate the
expiration date string:
• Example:
• $now = time( );
• $then = gmstrftime("%a, %d %b %Y %H:%M:%S GMT",
$now + 60*60*3);
• header("Expires: $then");
• The gmstrftime() function formats a GMT/UTC time and/or
date according to locale settings.
• gmstrftime(format,timestamp);
• format Required. Specifies how to return the result:
• %a - abbreviated weekday name
• %A - full weekday name
• %b - abbreviated month name
• %B - full month name
• %c - preferred date and time representation
• %d - day of the month (01 to 31)
• %D - same as %m/%d/%y
• %y - year without a century (range 00 to 99)
• %Y - year including the century
• To indicate that a document "never" expires, use the time a
year from now:
• $now = time( );
• $then = gmstrftime("%a, %d %b %Y %H:%M:%S
GMT", $now + 365*86440);
• header("Expires: $then");
• Example:
• domain The browser will return the cookie only for URLs
within this domain. The default is the server hostname.
• secure The browser will transmit the cookie only over https
connections.
• When a browser sends a cookie back to the server, you can
access that cookie through the $_COOKIE array.
• The key is the cookie name, and the value is the cookie's value
field. For instance, the following code at the top of a page
keeps track of the number of times the page has been accessed
by this client:
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
• <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>
• Destroying a PHP Session
• A PHP session can be destroyed by session_destroy()
function.
• This function does not need any argument and a single call can
destroy all the session variables.
• If you want to destroy a single session variable then you can
use unset() function to unset a session variable.