Hướng dẫn dùng refresh ph trong PHP

Thường thì mình hay dùng Javascript để refresh [làm mới] lại trang hay còn gọi là F5 lại trang, ở bài này mình sẽ hướng dẫn bạn refresh lại trang bằng PHP.

Bài toán mình đặt ra như sau: Khi người dùng truy cập vào một trang nào đó không tồn tại mình sẽ xuất một thông báo sau: "Xin chào bạn, trang này không tồn tại, bạn sẽ được
chuyển hướng tự động về trang chủ sau 5 giây".

Trong PHP để chuyển hướng ta sẽ dùng hàm header[ $string ]

Trường hợp 1:

echo "Xin chào bạn 
"; echo "Trang của bạn sẽ được tải lại sau 5s"; echo header["refresh: 5"]; exit[];

Ở trường hợp này, người dùng sẽ bị load lại trang sau 5 giây

Trường hợp 2:

echo "Xin chào bạn 
"; echo "Trang này không tồn tại, bạn sẽ được chuyển hướng về google sau 5s"; echo header["refresh: 5; url = //google.com"]; exit[];

Ở trường hợp này, người dùng sẽ được chuyển hướng về Google.com sau 5 giây. Bạn có thể thay url bằng bất cứ url nào bạn muốn.

Hi vọng ở bài học này bạn sẽ vận dụng trong dự án của bạn ở hiện tại và tương lai

Cám ơn bạn đã đọc bài viết, nếu có thắc mắc gì thì bạn hãy comment ở bình luận bên dưới nhé.

mjt at jpeto dot net

13 years ago

I strongly recommend, that you use

header[$_SERVER["SERVER_PROTOCOL"]." 404 Not Found"];

instead of

header["HTTP/1.1 404 Not Found"];

I had big troubles with an Apache/2.0.59 [Unix] answering in HTTP/1.0 while I [accidentially] added a "HTTP/1.1 200 Ok" - Header.

Most of the pages were displayed correct, but on some of them apache added weird content to it:

A 4-digits HexCode on top of the page [before any output of my php script], seems to be some kind of checksum, because it changes from page to page and browser to browser. [same code for same page and browser]

"0" at the bottom of the page [after the complete output of my php script]

It took me quite a while to find out about the wrong protocol in the HTTP-header.

Marcel G

12 years ago

Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header[ 'Location: ...' ] as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:



Hth someone

Dylan at WeDefy dot com

14 years ago

A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header[].



The HTTP status code changes the way browsers and robots handle redirects, so if you are using header[Location:] it's a good idea to set the status code at the same time.  Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely.  Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header['Location:'] defaults to 302.

mandor at mandor dot net

16 years ago

When using PHP to output an image, it won't be cached by the client so if you don't want them to download the image each time they reload the page, you will need to emulate part of the HTTP protocol.

Here's how:

Chủ Đề