search
HomeBackend DevelopmentPHP Tutorialajax2—solve caching problems—php (28)

5. ajax quick start

ajax code:

Php program:

6. Send data

When making an Ajax get request, the data to be sent needs to be appended to the end of the url

When making an Ajax post request, the data to be sent needs to be placed in the parameters of the send method

Ajax code:

Php code:

Deliberately write the requested URL wrong. We found that even if a URL that does not exist is requested, the status code of the ajax object will eventually be equal to 4, and the corresponding statement will be executed, and we hope that if the request is an error page , then the corresponding statement should not be executed

Make some corresponding modifications

Use ajax to calculate the sum of two numbers

7. Solve the cache problem

Above questions:

Change addition in php to subtraction

If the previous request parameter is used, it is still found to be addition. If it is a new request parameter, it is subtraction.

Solution 1: Random numbers

Math.random();

Generate a js random number after the url, so that the URL requested this time is unique, so the latest data will be returned every time you request it

However, we know that this method does not fundamentally solve the caching problem, it just generates a large number of cache files

One more thing: random numbers do not guarantee that this URL is absolutely unique

Solution 2:

time

new Date().getTime(); //Get the millisecond timestamp

The above method directly appends the timestamp to the url to make the current url unique. In terms of execution, this method will always be unique

However, it will still generate a large number of cache files under temporary files.

Solution 3. Set request header

setRequestHeader("If-Modified-Since","0");

Principle: Use the ajax object to set the http request header information before sending the http request, indicating that the last modification time of the resource currently being requested is "0". After the server gets this time, it will be compared with the last modification of the file on the server. The time is compared, and if they are different, the latest execution result is returned.

This way, there is always only one cache file in the end.

Solution4. Set response header

header("Cache-Control: no-cache, must-revalidate");

Use PHP's header function to write data to the response header. What is written is to tell the client: Do not cache this result.

This approach can fundamentally solve the caching problem without generating any cache files.

Example question:

Verify whether the username can be used

The above introduces ajax2 - solving cache problems - php (28), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment