php用静态类实现的页面跳转
时间:2021-07-01 10:21:17
帮助过:22人阅读
/** - * Miscellaneous utility methods.
- */
- final class Utils {
- private function __construct() {
- }
- /**
- * Redirect to the given page.
- * @param type $page target page
- * @param array $params page parameters
- */
- public static function redirect($page, $sub_page = null, array $params = array()) {
- header('Location: ' . self::createLink($page, $sub_page, $params));
- die();
- }
/** - * Generate link.
- * @param string $page target page
- * @param array $params page parameters
- */
- public static function createLink($page, $sub_page = null, array $params = array()) {
- if ($sub_page) {
- $params = array_merge(array('sub_page' => $sub_page), $params);
- }
- if (!$page) {
- return "#";
- }
- $param_str = http_build_query($params);
- if (trim($param_str) == "") {
- return $page . '.php';
- } else {
- return $page . '.php?' . $param_str;
- }
- }
- }
- ?>
调用示例:
- if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) {
- UserLogin::delUserInfo();
- }
- else if (UserLogin::isLogin()){
- Utils::redirect('welcome');
- }
- ?>
|