$ _ S E R V E R F I R S T O F F, W H AT I S A S U P E R G L O B - - PowerPoint PPT Presentation

s e r v e r f i r s t o f f w h at i s a s u p e r g l o
SMART_READER_LITE
LIVE PREVIEW

$ _ S E R V E R F I R S T O F F, W H AT I S A S U P E R G L O B - - PowerPoint PPT Presentation

S U P E R _ G L O B A L S : $ _ S E R V E R F I R S T O F F, W H AT I S A S U P E R G L O B A L ? ? ? A super global is always available to the PHP code for a page. They are built-in variables. Examples: $_GET & $_POST


slide-1
SLIDE 1

$ _ S E R V E R

S U P E R _ G L O B A L S :

slide-2
SLIDE 2

F I R S T O F F, W H AT I S A S U P E R G L O B A L ? ? ?

  • A super global is always

available to the PHP code for a page.

  • They are built-in variables.
  • Examples: $_GET &

$_POST (look familiar??)

Image from http://www.php5dp.com/design-patterns-scope-globals-and-superglobals-in-php/

slide-3
SLIDE 3

W H AT I S $ _ S E R V E R ?

  • $_SERVER is a Superglobal array that holds

headers, paths, and script locations.

  • In other words it holds the information that

you could find in a header, the url, and secure web server type things (https).

  • The information is provided by the Web

Server so sometimes you won’t get what you are looking for.

  • There are many elements you can find in

$_SERVER, for example the IP address, or what revision of CGI the server is using.

slide-4
SLIDE 4

E X A M P L E # 1

<!DOCTYPE html> <html> <body> <?php echo $_SERVER['PHP_SELF']; echo "<br>"; echo $_SERVER['SERVER_NAME']; echo "<br>"; echo $_SERVER['HTTP_HOST']; echo "<br>"; echo $_SERVER['HTTP_REFERER']; echo "<br>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<br>"; echo $_SERVER['SCRIPT_NAME']; ?> </body> </html> /php/demo_global_server.php www.w3schools.com www.w3schools.com http://www.w3schools.com/php/ showphp.asp? filename=demo_global_server Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/ 537.36 /php/demo_global_server.php

Result: Code:

From http://www.w3schools.com/php/showphp.asp?filename=demo_global_server

slide-5
SLIDE 5

W H E N D O W E U S E I T ? W H Y ?

  • We can use it to gather and display needed

data about the website and server.

  • We can use it to check for, and then create a

secure connection.

  • We can use it to create better security and

fix bugs.

slide-6
SLIDE 6

S Y N TA X …

To assign it to a Variable: $variable = $_SERVER [‘ELEMENT’] To use it with filter input: $variable = filter_input(INPUT_SERVER, ‘ELEMENT’);

slide-7
SLIDE 7

S O M E E L E M E N T S O F $ _ S E R V E R …

Y O U C A N F I N D A L L O F T H E M O N T H E P H P. N E T W E B S I T E .

  • ‘REQUEST_TIME’: The timestamp of

the start of the request. Available since PHP 5.1.0.

  • ‘REQUEST_TIME_FLOAT’: The

timestamp of the start of the request, with microsecond precision. Available since PHP 5.4.0.

  • ‘QUERY_STRING’: The query string, if

any, via which the page was accessed.

  • ‘DOCUMENT_ROOT’: The document

root directory under which the current script is executing, as defined in the server's configuration file.

  • ‘HTTP_ACCEPT’: Contents of the

Accept: header from the current request, if there is one.

  • ‘HTTP_ACCEPT_CHARSET’: Contents
  • f the Accept-Charset: header from

the current request, if there is one. Example: 'iso-8859-1,*,utf-8'.

  • ‘HTTP_ACCEPT_ENCODING’:

Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.

  • ‘HTTP_ACCEPT_LANGUAGE’:

Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.

slide-8
SLIDE 8

E X A M P L E # 2 - R E D I R E C T I N G T O S E C U R E C O N N E C T I O N

<?php $https = filter_input (INPUT_SERVER, ‘HTTPS’); if (!$https) { $host = filter_input (INPUT_SERVER, ‘HTTP_HOST’); $uri = filter_input (INPUT_SERVER, ‘REQUEST_URI’); $url = ‘https://' . $host . $uri; header (“Location: “ . $url); exit(); } ?>

Code:

From Murach’s PHP and MySQL 2nd Edition pg. 691

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15