PHP XML Ajax 7-19 (means to learn Java, alas, to support ...) )

Source: Internet
Author: User
Tags change settings echo date fread php language php session readfile setcookie types of filters

1. Multidimensional arrays

2, date,
    • D-Represents the day of the month (01-31)
    • M-Denotes month (01-12)
    • Y-Represents the year (four digits)
    • 1-Indicates the day of the week
©2010-<?php echo Date ("Y"); Use the date () function to automatically update the version year on your site
    • H-12 hour format with first zero
    • I-minutes with first zero
    • S-Seconds with First zero (00-59)
    • A-lowercase noon and afternoon (AM or PM)
Maketime () Creation date strtotime () use a string to create the date 3, include, through the include or require statement, you can insert the contents of the PHP file into another PHP file (before the server executes it), The Require statement produces a fatal error termination script, and include generates a warning message that the script continues. Syntax: include ' filename ', this menu file is used for all pages in the site. The specific approach is (we use a <div> element so that we can easily set the style through CSS in the future), and then, if we reference this "vars.php" file, we can use those variables in the call file, is to refer to which of the variables inside him can be used together; 4. php file processing, you must be very careful when you manipulate files. If you make a mistake, it can cause very serious damage. Common mistakes are: editing the wrong file, filling the hard disk with garbage, accidentally deleting the contents of the file, if all you want to do is open a file and reader content, then the ReadFile () function is useful. Add some directories. 5, read open file on the server, the better way to open the file is through the fopen () function. This function gives you more options than the ReadFile () function. The first parameter of fopen () contains the file name that is opened, and the second parameter specifies the mode in which the file is opened. If the fopen () function fails to open the specified file, the following example generates a message, <?php
$myfile = fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!"); Echo fread ($myfile, FileSize ("Webdictionary.txt")); fclose ($myfile);? >

The Fread () function reads the open file. The first parameter of Fread () contains the file name of the file to be read, and the second parameter specifies the maximum number of bytes to be read. The fclose () function is used to close open files.

The fgets () function is used to read a single line from a file.

The feof () function checks to see if "End-of-file" (EOF) has been reached.

Feof () is useful for traversing data with unknown lengths.

The fgetc () function is used to read a single character from a file.

6, PHP file creation write, fopen () function is also used to create files. It may be a bit confusing, but in PHP, the function used to create the file is the same as the open file. The fwrite () function is used to write to a file. The first parameter of Fwrite () contains the file name of the file to be written, and the second argument is the string to be written.

7, File Upload,<input> tag type= "file" attribute specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box. By using PHP's global array $_files, you can upload files from a client computer to a remote server. For IE, the type of the recognized JPG file must be pjpeg, and for FireFox, it must be JPEG.

8, PHP Cookies,cookie is often used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it also sends a cookie. With PHP, you can create and retrieve the value of a cookie. The Setcookie () function is used to set cookies. The Setcookie () function must precede the

Setcookie (name, value, expire, path, domain);
Setcookie ("User", "Alex Porter", Time () +3600);
When a cookie is sent, the value of the cookie is automatically URL-encoded and automatically decoded upon retrieval (to prevent URL encoding, use Setrawcookie () instead). PHP's $_cookie variable is used to retrieve the value of the COOKIE. We use the Isset () function to confirm whether a cookie has been set. When you delete a cookie, you should change the expiration date to a past point in time.

Setcookie ("User", "", Time ()-3600); If your application involves browsers that do not support cookies, you will have to take other steps to pass information from one page to another in your application. One way to do this is to pass data from the form (about the form and what the user entered, as we've already covered in this tutorial earlier).
9. The PHP session,php session variable is used to store information about a user's session or to change settings for a user session. The Session variable holds information that is single-user and can be used by all pages in the application. The Session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.
Before you store user information in a PHP session, you must first start the session.
<?php session_start ();?>
The correct way to store and retrieve session variables is to use the PHP $_session variable:
If you want to delete some session data, you can use the unset () or Session_destroy () function. Session_destroy () Resets the session and you will lose all stored session data.
10, PHP send e-mail, PHP mail () function is used to send e-mail from the script. Mail (to,subject,message,headers,parameters).
11, PHP Security e-mail, if the user in the form in the input box to add these text, what will happen?
[Email Protected]%0acc:[email protected]
%0abcc:[email Protected],[email protected],
[Email Protected],[email protected]
%0abto:[email protected]

As always, the mail () function puts the above text in the header of the message, and now the head has an extra Cc:, BCC: And to: Fields. When the user clicks the Submit button, the e-mail will be sent to all the addresses above!

The best way to prevent e-mail injection is to validate the input. We used a PHP filter to verify the input:
    • Filter_sanitize_email remove illegal characters from a string in an e-mail message
    • Filter_validate_email Verifying email addresses
12, in PHP, the default error handling is very simple. A message is sent to the browser with a file name, line number, and a message that describes the error. We will explain the different error handling methods: Simple "die ()" Statement custom error and error trigger error report

Creating a custom error handler is straightforward. We have simply created a special function that can be called when an error occurs in PHP.

The function must be capable of handling at least two parameters (Error level and error message), but can accept up to five parameters (optional: File, Line-number, and error context): Error_function ( Error_level,error_message,

Error_file,error_line,error_context);

Parameters Description
Error_level

Necessary. Specifies the error reporting level for user-defined errors. Must be a number of values.

See the table below: Error reporting levels.

Error_message Necessary. Specifies error messages for user-defined errors.
Error_file Optional. Specifies the file name in which the error occurred.
Error_line Optional. Specifies the line number where the error occurred.
Error_context Optional. Specifies an array that contains each variable that was used when the error occurred and their value.
Source: >  

value Constants Description
2 E_warning A non-fatal run-time error. Script execution is not paused.
8 E_notice

Run-time notice.

The script discovers that an error may have occurred, but it may also occur when the script is running correctly.

256 E_user_error A fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error ().
512 E_user_warning A non-fatal user-generated warning. This is similar to the e_warning that programmers use to set the PHP function Trigger_error ().
1024 E_user_notice User-generated notifications. This is similar to the e_notice that programmers use to set the PHP function Trigger_error ().
4096 E_recoverable_error A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (see Set_error_handler ())
8191 E_all

All errors and warnings, except level e_strict.

(In PHP 6.0,e_strict is part of E_all)

The location of the user input data in the script is useful for triggering errors when the user's input is not valid. In PHP, this task is done by Trigger_error (). You can trigger an error anywhere in the script, and by adding a second parameter, you can specify the level of error that is triggered. Error logging, by default, based on the Error_log configuration in php.ini, PHP sends error records to the server's error logging system or file. By using the Error_log () function, you can send an error record to a specified file or remote destination.
Error_log ("Error: [$errno] $errstr", 1, "[email protected]", "from: [email protected]");
13, exception handling, exception (Exception) is used to change the normal process of the script when the specified error occurs. Throwing an exception without capturing him can create a serious error. Correct for the Try,throw,catch. function to write exception, call inside Try,catch. However, to follow the principle that each throw must correspond to a catch, you can set up a top-level exception handler to handle the missing error. Creating a custom exception handler is straightforward. We have simply created a special class that can call its function when an exception occurs in PHP. The class must be an extension of the exception class. This custom exception class inherits all the properties of the PHP exception class, and you can add custom functions to it. You can use multiple exceptions for a script to detect multiple situations. You can use multiple if: else code block, or a switch code block, or nested multiple exceptions. These exceptions can use different exception classes and return different error messages, and sometimes when exceptions are thrown, you might want to handle them in a different way than the standard. You can throw an exception again in a "catch" code block. The script should hide the system error from the user. For programmers, system errors may be important, but users are not interested in them. To make it easier for users to use, you can again throw exceptions with a friendly message to the user. Set the top-level exception: the Set_exception_handler () function sets a user-defined function that handles all uncaught exceptions. In the above code, there is no "catch" code block, but the top-level exception handler is triggered. You should use this function to catch all uncaught exceptions.
14, PHP filter, PHP filter is used to verify and filter data from non-secure sources, such as user input. Input filtering is one of the most important application security issues.
What is external data? Input data from the form Cookies server variable database query results. To filter variables, use one of the following filter functions: Filter_var ()-Filters a single variable by a specified filter Filter_var_array ()-Filters multiple variables by the same or different filters Filter_input-Gets a Input variable and filter it Filter_input_array-get multiple input variables and filter them by the same or different filters.

There are two types of filters:

Validating Filter:

    • Used to validate user input
    • Strict formatting rules (e.g. URL or e-mail authentication)
    • Returns the expected type if successful, or FALSE if it fails
Sanitizing filter: Used to allow or disallow characters specified in a string without a data format rule always returns a string. By using the Filter_callback filter, you can invoke a custom function and use it as a filter. In this way, we have full control over the data filtering. 15. In this chapter we will show you how to encode and decode JSON objects using the PHP language.
Json_encode JSON Encoding of variables
Json_decode Decodes a JSON-formatted string into a PHP variable
Json_last_error Returns the last error that occurred

PHP XML Ajax 7-19 (means to learn Java, alas, to support ...) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.