Upload
ahanu
View
28
Download
0
Embed Size (px)
DESCRIPTION
ECA 236. Open Source Server Side Scripting Includes and Dates. include( ). include( ) include( ‘ file_name.inc ’ ) PHP includes and evaluates the specified file if include( ) fails, PHP generates a warning inherits variable scope of line from which it is called - PowerPoint PPT Presentation
Citation preview
Open Source Server Side Scripting
ECA 236
Open Source Server Side ScriptingIncludes and Dates
Open Source Server Side Scripting 2ECA 236
include( )include( )
include( )include( ‘file_name.inc’ )
PHP includes and evaluates the specified fileif include( ) fails, PHP generates a warninginherits variable scope of line from which it is calledmay contain DTD, HTML, JavaScript, PHP,
variables, other includesmay use any extension: .inc .php
Open Source Server Side Scripting 3ECA 236
include( ) cont … include( ) cont … header.inc
main document
<html><head><title><?php echo $page_title; ?></title></head><body><?php $name=’Michael’; ?>
<?php$page_title = “Include Test”;include( ‘header.inc’ );
?><p>Hello, <?php echo $name; ?></p></body></html>
Open Source Server Side Scripting 4ECA 236
require( )require( )
require( )require( ‘file_name.inc’ )
PHP includes and evaluates the specified fileif require( ) fails, PHP generates a fatal errorinherits variable scope of line from which it is calledmay contain DTD, HTML, JavaScript, PHP,
variables, other includesmay use any extension: .inc .php
Open Source Server Side Scripting 5ECA 236
include_once( )include_once( )
include_once( ‘file_name.inc’ );
similar behaviors to include( )if the file has already been included, it will not be
included againuse to avoid such problems as:
function redefinitionsvariable value reassignment
Open Source Server Side Scripting 6ECA 236
require_once( )require_once( )
require_once( ‘file_name.inc’ );
similar behaviors to require( )if the file has already been required, it will not be
required againuse to avoid such problems as:
function redefinitionsvariable value reassignment
Open Source Server Side Scripting 7ECA 236
HTTP HeadersHTTP Headers
header( )used to send raw HTTP headers
header( ) MUST be called before anything else is sent to the browser:
HTMLblank spaces or lines
common use for header( ) is to redirect the user to another URI using the Location header
Open Source Server Side Scripting 8ECA 236
HTTP Headers cont … HTTP Headers cont …
HTTP requires an absolute URI as an argument to Location
<?php
header( “Location: http://www.headerExample.com” );
?>
Open Source Server Side Scripting 9ECA 236
HTTP Headers cont … HTTP Headers cont …
You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'], and dirname( ) to create an absolute URI out of a relative URI.
dirname( ) returns path of directory names
<?php
header( “Location: http://” . $_SERVER[ 'HTTP_HOST‘ ] . dirname( $_SERVER[ 'PHP_SELF‘ ] ) . “/” . $relative_url);
?>
Open Source Server Side Scripting 10ECA 236
HTTP Headers cont … HTTP Headers cont …
Remember, header( ) must be called before any other output is sent.
The following will generate an error:
<html><?php
// This will give an error. Note the output// above, which is before the header( ) call
header('Location: http://www.example.com/');?>
Open Source Server Side Scripting 11ECA 236
HTTP Headers cont … HTTP Headers cont …
headers_sent( )checks if headers have been sent
<?phpif( !headers_sent( ) ) {
header( “Location: http://www.headerExample.com” );
}?>
Open Source Server Side Scripting 12ECA 236
mail( )mail( )mail( ) takes three required parameters, plus additional
optional parametersto
email address of the recipientseparate multiple addresses with commas
subjectsubject line of the email
messagebody of the email
Open Source Server Side Scripting 13ECA 236
mail( ) cont … mail( ) cont … fourth optional parameter for additional headers
such as from, cc, etc
$to = “[email protected], [email protected]”; $subject = “Test email”; $message = “This is my email message.”; $from = [email protected];
mail( $to, $subject, $message, $from );
Open Source Server Side Scripting 14ECA 236
dates and timesdates and times
date( )takes two parameters
format stringtime stamp (optional)
returns current date and time in following format:
echo $today = date( “jS F Y” );
13th September 2003
Open Source Server Side Scripting 15ECA 236
date( )date( )
examples of format stringsj day of month without leading zerosS English ordinalsF month, textual, longY 4 digit year
for complete list of format strings see the PHP Manual ( php.net )
Open Source Server Side Scripting 16ECA 236
date( ) cont …date( ) cont …
string format can contain literals as well as format code
escape letters to print as literals rather than format code
echo $today = date( “m/d/Y” );
9/13/2003
echo $today = date( “jS F in the year Y” );
13th September 179 3001e 03epmSat, 13 Sep 2003 13:17:19 -0400 2003
Open Source Server Side Scripting 17ECA 236
date( ) cont …date( ) cont …
2nd parameter is a UNIX time stampnumber of seconds since UNIX Epoch ( midnight 1 Jan 1970 )32 bit integer 2^ 32
holds up to 2,147,483,647 secondswill face UNIX “Y2K” in 2038turned 1 billion on September 08, 2001
if no timestamp is provided, PHP uses current date and time
Open Source Server Side Scripting 18ECA 236
mktime( )mktime( )
mktime( )converts a date and time to UNIX time stamp
takes 6 arguments
common programming error is to mix up order of arguments, which differs from UNIX mktime( ) function
returns a value of 18001
mktime( hour, minute, second, month, day, year );
mktime( 0, 0, 1, 1, 1, 1970 );
Open Source Server Side Scripting 19ECA 236
mktime( ) cont …mktime( ) cont …
mktime( )arguments can be left out in order from right to leftleaving out an argument defaults to the timestamp for the
current date or time
negative timestamps are not supported under any version of Windows ( limiting the range from 1970 – 2038 )
$timestamp = mktime( ); // returns current date and time
Open Source Server Side Scripting 20ECA 236
mktime( ) cont …mktime( ) cont …
mktime( )it is possible to use date( ) and mktime( ) together to
find dates in the future or past
returns the day of the week of the future date, Monday
$ts = mktime( 0, 0, 0, 7, 1, 2005 );
echo "July 1, 2005 is on a " . date("l", $ts ) ;
Open Source Server Side Scripting 21ECA 236
getdate( )getdate( )
getdate( )takes a time stamp as an optional argument if no time stamp is provided, getdate( ) returns information
on current date and timereturns an associative array with keys and values
Open Source Server Side Scripting 22ECA 236
getdate( ) cont …getdate( ) cont …
Key Description Example returned values
"seconds" Numeric representation of seconds 0 to 59
"minutes" Numeric representation of minutes 0 to 59
"hours" Numeric representation of hours 0 to 23
"mday" Numeric day of the month 1 to 31
"wday" Numeric day of the week 0 (Sunday) through 6
"mon" Numeric representation of a month 1 through 12
"year" Full numeric representation of year Examples: 1999 or 2003
"yday" Numeric day of the year 0 through 356
"weekday" Full textual representation day Sunday through Saturday
"month" Full textual representation of month January through December
0 Seconds since the Unix Epoch System Dependent
Open Source Server Side Scripting 23ECA 236
getdate( ) cont …getdate( ) cont …
Array( [seconds] => 40 [minutes] => 58 [hours] => 21 [mday] => 17 [wday] => 2 [mon] => 6 [year] => 2003 [yday] => 167 [weekday] => Tuesday [month] => June [0] => 1055901520
)
$today = getdate( );print_r( $today );
// will print
Open Source Server Side Scripting 24ECA 236
checkdate( )checkdate( )checkdate( )
checks whether a date is validuseful for checking user inputtakes leap year into considerationworks for date before 1970takes three integer arguments
checkdate( month, day, year );
checkdate( 1, 24, 1956 ); // returns TRUE
checkdate( 2, 30, 2003 ); // returns FALSE
Open Source Server Side Scripting 25ECA 236
date and time calculationsdate and time calculations
one of the easiest ways to compare dates and times is to compare their time stamps
to calculate the difference between two dates, subtract their timestamps