Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.

Similar presentations


Presentation on theme: "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates."— Presentation transcript:

1 Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates

2 Open Source Server Side Scripting 2 ECA 236 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

3 Open Source Server Side Scripting 3 ECA 236 include( ) cont … header.inc main document Hello,

4 Open Source Server Side Scripting 4 ECA 236 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

5 Open Source Server Side Scripting 5 ECA 236 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

6 Open Source Server Side Scripting 6 ECA 236 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

7 Open Source Server Side Scripting 7 ECA 236 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

8 Open Source Server Side Scripting 8 ECA 236 HTTP Headers cont …  HTTP requires an absolute URI as an argument to Location <?php header( “Location: http://www.headerExample.com” ); ?>

9 Open Source Server Side Scripting 9 ECA 236 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); ?>

10 Open Source Server Side Scripting 10 ECA 236 HTTP Headers cont …  Remember, header( ) must be called before any other output is sent. The following will generate an error:

11 Open Source Server Side Scripting 11 ECA 236 HTTP Headers cont …  headers_sent( ) checks if headers have been sent

12 Open Source Server Side Scripting 12 ECA 236 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

13 Open Source Server Side Scripting 13 ECA 236 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 );

14 Open Source Server Side Scripting 14 ECA 236 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” ); 13 th September 2003

15 Open Source Server Side Scripting 15 ECA 236 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 )

16 Open Source Server Side Scripting 16 ECA 236 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” ); 13 th September 179 3001e 03epmSat, 13 Sep 2003 13:17:19 -0400 2003

17 Open Source Server Side Scripting 17 ECA 236 date( ) cont …  2 nd 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

18 Open Source Server Side Scripting 18 ECA 236 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 );

19 Open Source Server Side Scripting 19 ECA 236 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

20 Open Source Server Side Scripting 20 ECA 236 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 ) ;

21 Open Source Server Side Scripting 21 ECA 236 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

22 Open Source Server Side Scripting 22 ECA 236 getdate( ) cont … Key DescriptionExample 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 yearExamples: 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 EpochSystem Dependent

23 Open Source Server Side Scripting 23 ECA 236 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

24 Open Source Server Side Scripting 24 ECA 236 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

25 Open Source Server Side Scripting 25 ECA 236 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


Download ppt "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates."

Similar presentations


Ads by Google