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é.
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é.
How can I refresh a page using PHP periodically? If I can not do it by PHP, what is the best recommended scenario?
asked Sep 12, 2012 at 7:43
AanAan
11.7k35 gold badges85 silver badges144 bronze badges
1
You can do it with PHP:
header["Refresh:0"];
It refreshes your current page, and if you need to redirect it to another page, use following:
header["Refresh:0; url=page2.php"];
answered Jan 2, 2014 at 7:48
4
In PHP you can use:
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header["Refresh: $sec; url=$page"];
Or just use JavaScript's window.location.reload[]
.
answered Sep 12, 2012 at 7:44
AboQutieshAboQutiesh
1,6562 gold badges9 silver badges14 bronze badges
4
You sure can refresh a page periodically using PHP:
This will refresh the page every three seconds.
answered Jul 31, 2013 at 14:49
131131
1,3341 gold badge12 silver badges32 bronze badges
0
That is simply possible with header[] in PHP:
header['Refresh: 1; url=index.php'];
answered Jul 31, 2014 at 9:44
KamleshKamlesh
6348 silver badges21 bronze badges
I've found two ways to refresh PHP content:
1. Using the HTML meta
tag:
echo[""]; //Refresh by HTTP 'meta'
2. Using PHP refresh rate:
$delay = 0; // Where 0 is an example of a time delay. You can use 5 for 5 seconds, for example!
header["Refresh: $delay;"];
answered Nov 4, 2014 at 16:24
ThanosThanos
3012 silver badges4 bronze badges
1
Besides all the PHP ways to refresh a page, the page will also be refreshed with the following HTML meta tag:
See Meta refresh - "automatically refresh the current web page or frame after a given time interval"
You can set the time within the content
value.
answered Sep 12, 2012 at 7:45
MathlightMathlight
6,22516 gold badges61 silver badges104 bronze badges
5
header['Location: .'];
seems to refresh the page in Chrome, Firefox, Edge, and Internet Explorer 11.
answered Apr 21, 2017 at 9:02
Ben GuestBen Guest
1,4382 gold badges15 silver badges28 bronze badges
2
Echo the meta
tag like this:
URL is the one where the page should be redirected to after the refresh.
echo "";
answered Sep 22, 2014 at 4:50
1
You can refresh using JavaScript. Rather than the complete page refresh, you can give the contents to be refreshed in a div. Then by using JavaScript you can refresh that particular div only, and it works faster than the complete page refresh.
answered Sep 12, 2012 at 8:13
AnoopAnoop
1302 silver badges10 bronze badges
1
PHP is server-side language, so you can not refresh the page with PHP, but JavaScript is the best option to refresh the page:
location.reload[];
The visit Location reload[] method.
answered Sep 12, 2012 at 7:44
PatriksPatriks
1,0061 gold badge9 silver badges28 bronze badges
8
You cannot do it in PHP. Once the page is loaded, PHP dies and is out of control.
You have a few options:
- Use JavaScript
- Use the refresh meta tag,
I think that the refresh meta tag is the easiest and most convenient.
answered Sep 12, 2012 at 7:46
JvdBergJvdBerg
21.6k8 gold badges35 silver badges54 bronze badges
7
Adding this meta tag in PHP might help:
echo '';
answered Mar 9, 2017 at 11:28
2
One trick is to add a random number to the end of the URL. That way you don't have to rename the file every time. E.g.:
echo " answered Sep 19, 2017 at 21:39
0x450x45
6793 gold badges7 silver badges24 bronze badges
1
Add the following function to your project:
function redirect[$filename] {
if [!headers_sent[]]
header['Location: '.$filename];
else {
echo '';
echo 'window.location.href = \''.$filename.'\';';
echo '';
echo '';
echo '';
echo '';
}
exit[];
}
function call:
redirect[$_SERVER['REQUEST_URI']];
answered Jan
13 at 14:41