1 / 25

ECA 236

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

ahanu
Télécharger la présentation

ECA 236

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. getdate( ) cont … Open Source Server Side Scripting

  23. 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

  24. 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

  25. 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

More Related