250 likes | 414 Views
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
E N D
ECA 236 Open Source Server Side Scripting Includes and Dates Open Source Server Side Scripting
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 • may contain DTD, HTML, JavaScript, PHP, variables, other includes • may use any extension: .inc .php Open Source Server Side Scripting
include( ) cont … <html> <head> <title><?php echo $page_title; ?></title> </head> <body> <?php $name=’Michael’; ?> header.inc main document • <?php $page_title = “Include Test”; include( ‘header.inc’ );?><p>Hello, <?php echo $name; ?></p></body></html> Open Source Server Side Scripting
require( ) • require( ) require( ‘file_name.inc’ ) • PHP includes and evaluates the specified file • if require( ) fails, PHP generates a fatal error • inherits variable scope of line from which it is called • may contain DTD, HTML, JavaScript, PHP, variables, other includes • may use any extension: .inc .php Open Source Server Side Scripting
include_once( ) include_once( ‘file_name.inc’ ); • similar behaviors to include( ) • if the file has already been included, it will not be included again • use to avoid such problems as: • function redefinitions • variable value reassignment Open Source Server Side Scripting
require_once( ) require_once( ‘file_name.inc’ ); • similar behaviors to require( ) • if the file has already been required, it will not be required again • use to avoid such problems as: • function redefinitions • variable value reassignment Open Source Server Side Scripting
HTTP Headers • header( ) used to send raw HTTP headers • header( ) MUST be called before anything else is sent to the browser: • HTML • blank spaces or lines • common use for header( ) is to redirect the user to another URI using the Location header Open Source Server Side Scripting
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
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
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
HTTP Headers cont … • headers_sent( ) checks if headers have been sent <?php if( !headers_sent( ) ) { header( “Location: http://www.headerExample.com” ); }?> Open Source Server Side Scripting
mail( ) • mail( ) takes three required parameters, plus additional optional parameters • to • email address of the recipient • separate multiple addresses with commas • subject • subject line of the email • message • body of the email Open Source Server Side Scripting
mail( ) cont … • fourth optional parameter for additional headers such as from, cc, etc $to = “mbarath@neo.rr.com, leland@palmer.com”; $subject = “Test email”; $message = “This is my email message.”; $from = josie@packard.com; mail( $to, $subject, $message, $from ); Open Source Server Side Scripting
dates and times • date( ) • takes two parameters • format string • time stamp (optional)returns current date and time in following format: echo $today = date( “jS F Y” ); 13th September 2003 Open Source Server Side Scripting
date( ) • examples of format strings • j day of month without leading zeros • S English ordinals • F month, textual, long • Y 4 digit year • for complete list of format strings see the PHP Manual ( php.net ) Open Source Server Side Scripting
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
date( ) cont … • 2nd parameter is a UNIX time stamp • number of seconds since UNIX Epoch ( midnight 1 Jan 1970 ) • 32 bit integer 2^ 32 • holds up to 2,147,483,647 seconds • will face UNIX “Y2K” in 2038 • turned 1 billion on September 08, 2001 • if no timestamp is provided, PHP uses current date and time Open Source Server Side Scripting
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( ) functionreturns a value of 18001 mktime( hour, minute, second, month, day, year ); mktime( 0, 0, 1, 1, 1, 1970 ); Open Source Server Side Scripting
mktime( ) cont … • mktime( ) • arguments can be left out in order from right to left • leaving 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
mktime( ) cont … • mktime( ) • it is possible to use date( ) and mktime( ) together to find dates in the future or pastreturns 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
getdate( ) • getdate( ) • takes a time stamp as an optional argument • if no time stamp is provided, getdate( ) returns information on current date and time • returns an associative array with keys and values Open Source Server Side Scripting
getdate( ) cont … Open Source Server Side Scripting
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
checkdate( ) • checkdate( ) • checks whether a date is valid • useful for checking user input • takes leap year into consideration • works for date before 1970 • takes three integer arguments checkdate( month, day, year ); checkdate( 1, 24, 1956 ); // returns TRUE checkdate( 2, 30, 2003 ); // returns FALSE Open Source Server Side Scripting
date 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 Open Source Server Side Scripting