用戶(hù)登錄功能時(shí)往往需要重定向頁(yè)面,那么在PHP中如何重定向頁(yè)面?下面本篇文章給大家介紹三種重定向網(wǎng)頁(yè)跳轉(zhuǎn)頁(yè)面的方法。
第一種:利用header()函數(shù)進(jìn)行重定向,這也是我用的較多的。(注意!locationhe和“:”之間不能有空格,否則無(wú)作用!)
<?php header('content-type:text/html;charset=uft-8); //重定向頁(yè)面 header('location:index.php'); ?>
第二種:利用HTML 頭部中的 meta標(biāo)簽,定義http-equiv=refresh 和content=”跳轉(zhuǎn)花費(fèi)的時(shí)間(秒為單位);url=跳轉(zhuǎn)地址”
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> //跳轉(zhuǎn)頁(yè)面,跳轉(zhuǎn)時(shí)間:3秒鐘,目標(biāo)地址:index.php <meta http-equiv="refresh" content="3;url=index.php"> <title>skip...</title> </head>
或者
<?php header('content-type:text/html;charset=utf-8'); $url='index.php'; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> //跳轉(zhuǎn)頁(yè)面,跳轉(zhuǎn)時(shí)間:2秒鐘,目標(biāo)地址:index.php <meta http-equiv="refresh" content="2;url=<?php echo $url; ?>"> <title>skip...</title> </head>
第三種:利用javascript進(jìn)行跳轉(zhuǎn)
<?php header('content-type:text/html;charset=utf-8'); $url='index.php'; //立即跳轉(zhuǎn)至目標(biāo)頁(yè)面 echo <script>window.location.href='$url';</script>; ?>
推薦學(xué)習(xí):《PHP視頻教程》