Selaginella: features, morphology ,anatomy and reproduction.
Chapter 1.Web Techniques_Notes.pptx
1. CHAPTER 1.
INTRODUCTION TO
WEB TECHNIQUES
Prepared By- Mrs. Shital V. Ghotekar
Asst. Prof.
Computer Science Department
MAEER’S MIT Art's, Commerce & Science College, Alandi(D), Pune
2. Contents
HTTP Basics
Variables
Server Information
Processing Forms
Setting Response Header
Maintaining State
PHP Error Handling
3. HTTP Basics
This protocol governs how web browsers request files from web servers and how the servers
send the files back.
When a web browser requests a web page, it sends an HTTP request message to a web server.
The request message always includes some header information, and it sometimes also includes
a body.
The first line of an HTTP request looks like this:
GET /index.html HTTP/1.1
This line specifies an HTTP command, called a method , followed by the address of a
document and the version of the HTTP protocol being used.
In this case, the request is using the GET method to ask for the index.html document using
HTTP 1.1
4. HTTP Basics
After this initial line, the request can contain optional header information that gives the server
additional data about the request.
For example:
User-Agent: Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]
Accept: image/gif, image/jpeg, text/*, */*
The User-Agent header provides information about the web browser, while the Accept header
specifies the MIME types that the browser accepts.
5. HTTP Basics
The web server responds with a reply message, which always includes header information and
usually contains a body.
The web server receives the request, processes it, and sends a response.
The first line of an HTTP response looks like this:
HTTP/1.1 200 OK
This line specifies the protocol version, a status code, and a description of that code.
In this case, the status code is "200", meaning that the request was successful (hence the
description "OK").
6. HTTP Basics
There are three basic features that make HTTP a simple but powerful protocol:
• HTTP is connectionless: The HTTP client, i.e., a browser initiates an HTTP request and after a
request is made, the client waits for the response. The server processes the request and sends a
response back after which client disconnect the connection. So client and server knows about
each other during current request and response only. Further requests are made on new
connection like client and server are new to each other.
• HTTP is media independent: It means, any type of data can be sent by HTTP as long as both
the client and the server know how to handle the data content. It is required for the client as
well as the server to specify the content type using appropriate MIME-type.
• HTTP is stateless: As mentioned above, HTTP is connectionless and it is a direct result of
HTTP being a stateless protocol. The server and client are aware of each other only during a
current request. Afterwards, both of them forget about each other. Due to this nature of the
protocol, neither the client nor the browser can retain information between different requests
across the web pages.
7. HTTP Methods
The two most common HTTP methods are GET and POST.
The GET method is designed for retrieving information, such as a document, an image, or the
results of a database query, from the server.
The POST method is meant for posting information, such as a credit-card number or
information that is to be stored in a database, to the server.
The GET method is what a web browser uses when the user types in a URL or clicks on a link.
When the user submits a form, either the GET or POST method can be used, as specified by the
method attribute of the form tag.
8. Variables
Server configuration and request information—including form parameters and cookies—are
accessible in three different ways from your PHP scripts.
Collectively, this information is referred to as EGPCS (environment, GET, POST, cookies, and
server).
If the register_globals option in php.ini is enabled, PHP creates a separate global variable for every
form parameter, every piece of request information, and every server configuration value.
PHP creates six global arrays that contain the EGPCS information.
The global arrays are:
$HTTP_COOKIE_VARS ($_COOKIE) Contains any cookie values passed as part of the request,
where the keys of the array are the names of the cookies.
$HTTP_GET_VARS ($_GET)Contains any parameters that are part of a GET request, where the
keys of the array are the names of the form parameters.
$HTTP_POST_VARS ($_POST) Contains any parameters that are part of a POST request, where
the keys of the array are the names of the form parameters.
9. Variables
$HTTP_POST_FILES ($_FILES ) Contains information about any uploaded files.
$HTTP_SERVER_VARS ($_SERVER) Contains useful information about the web server.
$HTTP_ENV_VARS ($_ENV) Contains the values of any environment variables, where the
keys of the array are the names of the environment variables.
PHP provides shorter aliases: $_COOKIE, $_GET, $_POST, $_FILES, $_SERVER, and
$_ENV.
These short variables are the recommended way to access EGPCS values.
The $_REQUEST array is also created by PHP if the register_globals option is on; however,
there is no corresponding $HTTP_REQUEST_VARS array. The $_REQUEST array contains
the elements of the $_GET, $_POST, and $_COOKIE arrays.
PHP also creates a variable called $PHP_SELF, which holds the name of the current script,
relative to the document root (e.g., /store/cart.php). This value is also accessible as
$_SERVER['PHP_SELF']. This variable is useful when creating self-referencing scripts.
10. Server Information
The $_SERVER array contains a lot of useful information from the web server.
SERVER_SOFTWARE A string that identifies the server (e.g., "Apache/1.3.22 (Unix)
mod_perl/1.26 PHP/4.1.0").
SERVER_NAME The hostname, DNS alias, or IP address for self-referencing URLs (e.g.,
"www.example.com").
GATEWAY_INTERFACE The version of the CGI standard being followed (e.g., "CGI/1.1").
SERVER_PROTOCOL The name and revision of the request protocol (e.g., "HTTP/1.1").
SERVER_PORT The server port number to which the request was sent (e.g., "80").
REQUEST_METHOD The method the client used to fetch the document (e.g., "GET").
PATH_INFO Extra path elements given by the client (e.g., "/list/users").
PATH_TRANSLATED The value of PATH_INFO, translated by the server into a filename
(e.g., "/home/httpd/htdocs/list/users").
11. Server Information
SCRIPT_NAME The URL path to the current page, which is useful for self-referencing scripts
(e.g., "/~me/menu.php").
QUERY_STRING Everything after the ? in the URL (e.g., "name=Fred+age=35").
REMOTE_HOST The hostname of the machine that requested this page (e.g., "dialup-192-
168-0-1.example.com"). If there's no DNS for the machine, this is blank and REMOTE_ADDR
is the only information given.
REMOTE_ADDR A string containing the IP address of the machine that requested this page
(e.g., "192.168.0.250").
AUTH_TYPE If the page is password-protected, this is the authentication method used to
protect the page (e.g., "basic").
REMOTE_USER If the page is password-protected, this is the username with which the client
authenticated (e.g., "fred"). Note that there's no way to find out what password was used.
12. Server Information
The two most common and useful headers are: HTTP_USER_AGENT The string the browser
used to identify itself (e.g., "Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]")
HTTP_REFERER The page the browser said it came from to get to the current page (e.g.,
"http://www.example.com/last_page.html")
13. Processing Forms
There are two HTTP methods that a client can use to pass form data to the server: GET and
POST.
Parameters-
Use the $_POST , $_GET, and $_FILES arrays to access form parameters from your PHP
code.
Example-
Design HTML Page to accept username & password.
Write PHP script to display it in as an output.
14. Processing Forms
The $ _FILES array contains following properties −
$_FILES['file']['name'] - The original name of the file to be uploaded.
$_FILES['file']['type'] - The mime type of the file.
$_FILES['file']['size'] - The size, in bytes, of the uploaded file.
$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded
file was stored on the server.
$_FILES['file']['error'] - The error code associated with this file upload.
15. The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only if method="post"
17. PHP Error Handling
An error is a unwanted result of program.
Error is a type of mistake.
An error can also be defined as generate wrong output of program caused by a fault.
An error message display with error message, filename and line number.
An error can be categorized in :
• Syntax Error
• Logical Error
• Run-time Error
18. PHP Error Handling
In PHP there are four types of errors.
• Notice Error
• Fatal Error
• Waring Error
• Parse Error
PHP – Notice Error
Notice error occurs when we use a variable in program which is not defined then it produce
notice error.
Notice error code execution of script does not stop.
19. PHP – Notice Error
<?php
for($i=1;$i<=10;$i++)
{
$series=$series. " ".$i;
}
echo "The No = ".$series;
?>
Output-
Notice: Undefined variable: series in
/var/www/html/Sem-2/isset.php on line 29
The No = 1 2 3 4 5 6 7 8 9 10
In the given example we use $series variable
but it is not defined, so it will produce notice
error but the execution of script does not stop.
20. PHP – Notice Error
<?php
$a=5;
echo “The result = ” . $b;
?>
Output-
In this example we declare a variable $a, but
we use another variable $b in program which
is not defined, so it will also generate notice
error like below.
Notice: Undefined variable: b in
/var/www/html/Sem-2/isset.php on line 3
21. Fatal error is critical error and it is stop the
execution of script immediately.
Fatal error occurs when we call undefined
functions or class.
Ex-
<?php
function sum()
{ $sum=5+6;
echo "The sum= ".$sum; }
mul();
?>
Output-
In the given example we declined “sum()”
function but in program we call “mul()”
function which is not defined in program, so
it will produce fatal error and stop the
execution of script.so it will also generate
notice error like below.
Fatal Error: Call to undefined function mul( )
in /var/www/html/Sem-2/isset.php on line 7
PHP – Fatal Error
22. A warning error does not stop the execution
of script. warning error not a critical error.
A warning error occurs when we pass wrong
parameter in function or we include external
file using include() function but the file
doesn’t exist then display warning error.
Warning Error Example-
<?php
include("wrongfile.php");
$sum=5+6;
echo "".$sum;
?>
Output-
Warning:include(wrongfilr.php):failed to
open stream:No such file or directory in
/var/www/html/Sem-2/isset.php on line 3
The sum=11
PHP – Warning Error
23. A Parse error occurs if there is a mistake in a
syntax.
When parse error occurs the execution of
script will be terminated.
Parse Error Example
<?php
$a=10
echo "The A = ".$a;
?>
Output-
Parse error : syntax error , unexpected
‘echo’(T_ECHO) in /var/www/html/Sem-
2/isset.php on line 5
PHP – Parse Error
24. Error Handling
Error handling is the process of catching errors raised by your program and then taking
appropriate action.
Ways to handle PHP Errors:
• Using die() method
• Custom Error Handling and error triggers
• Error reporting
25. Error Handling
<?php
// Php code showing default error handling
$file = fopen(“demo.txt", "w");
?>
Runtime Error: (demo.txt file not present)
PHP Warning: fopen(demo.txt): failed to open stream: Permission denied in /var/www/html/isset.php on line 2
To prevent /avoid this error use die() function.
The die() function print a message and exit from current script.
It is equivalent to exit() function in PHP.
Syntax:
die( $message )
26. Error Handling
Example-
<?php
if( !file_exists(“demo.txt") ) {
die("File is not present");
}
else {
$file = fopen(“demo.txt", "w");
}
?>
Note: If demo.txt file not present then it will display output.
Output : File is not present
27. Error Handling
Custom Error handling:
Creating a custom error handler is quite simple.
Create a special function that can be called when an error occurs in PHP.
This function must be able to handle a minimum of two parameters (error level and error message) but
can accept up to five parameters
Syntax:
error_function( $error_level, $error_message, $error_file, $error_line, $error_context)
$error_level: It is required parameter and it must be an integer. There are predefined error levels.
$error_message: It is required parameter and it is the message which user want to print.
$error_file: It is optional parameter and used to specify the file in which error has been occurred.
$error_line: It is optional parameter and used to specify the line number in which error has been occurred.
$error_context: It is optional parameter and used to specify an array containing every variable and their value
when error has been occurred.
28. Error Handling
error_level: These are the possible error level which are listed below:
• 1 : .E_ERROR :fatal runtime error execution of script has been halted
• 2 : E_WARNING :non fatal runtime error execution of script has been halted
• 4 : E_PARSE :compile time error it is generated by the parser
• 8 :E_NOTICE :The script found something that might be an error
• 16 :E_CORE_ERROR :Fatal errors that occurred during initial startup of script
• 32 :E_CORE_WARNING :Non fatal errors that occurred during initial startup of script
• 8191 :E_ALL :All errors and warning
29. Error Handling
CUSTOM FUNCTION
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
The code above is a simple error handling function.
When it is triggered, it gets the error level and an error
message. It then outputs the error level and message
and terminates the script.
30. Error Handling
SET ERROR HANDLER
Since we want our custom function to handle all errors, the set_error_handler() only
needed one
parameter, a second parameter could be added to specify an error level.
set_error_handler("customError");
31. EXAMPLE
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
?>
OUTPUT - Error: [8] Undefined variable: test
32. Error Handling
<?php
function myerror($error_no, $error_msg) {
echo "Error: [$error_no] $error_msg ";
echo "n Now Script will end"; // When error occurred script has to be stopped
die();
}
set_error_handler("myerror");
$a = 10;
$b = 0;
// This will generate error
echo($a / $b);
?> Output : Error: [2] Division by zero
Now Script will end
33. Trigger An Error
In a script where users can input data it is useful to trigger errors when an illegal input occurs.
In PHP, this is done by the trigger_error() function.
<?php
$test=2;
if ($test>=1)
{
trigger_error("Value must be 1 or below");
}
?>
OUTPUT
Notice: Value must be 1 or below in /var/www/html/Sem-2/error.php on line 6
34. ERROR TYPES
E_USER_ERROR - Fatal user-generated run-time error. Errors that can not be recovered from.
Execution of the script is halted
E_USER_WARNING - Non-fatal user-generated run-time warning. Execution of the script is
not Halted.
E_USER_NOTICE - Default. User-generated run- time notice. The script found something that
might be an error, but could also happen when running a script normally
35. EXAMPLE
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
//set error handler
set_error_handler("customError",E_USER_WARNIN
G);
//trigger error
$test=2;
if ($test>=1)
{
trigger_error("Value must be 1 or
below",E_USER_WARNING);
}
?>
36. EXPLANATION
E_USER_WARNING occurs if the "test" variable is bigger than "1".
If an E_USER_WARNING occurs we will use our custom error handler and
end the script:
OUTPUT-
Error: [512] Value must be 1 or below
Ending Script
37. ERROR LOG
By default, PHP sends an error log to the server’s logging system or a file, depending
on how the error_log configuration is set in the php.ini file.
By using the error_log() function you can send error logs to a specified file or a remote
destination.
Sending error messages to yourself by e-mail can be a good way of getting notified of
specific errors.
38. EXAMPLE
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Webmaster has been notified";
error_log("Error: [$errno] $errstr",1,
"svghotekar@gamil.com","From:
vaibhavghotekar@gmail.com");
}
//set error handler
set_error_handler("customError",E_USER_
WARNING);
//trigger error
$test=2;
if ($test>=1)
{
trigger_error("Value must be 1 or
below",E_USER_WARNING);
}
?>
39. OUTPUT
Error: [512] Value must be 1 or below Vaibhavghotekar has been notified
And the mail received from the code above looks like this:
Error: [512] Value must be 1 or below
Editor's Notes
NOTE:
To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.