Sample code for implementing simple jump prompts in PHP

不言
Release: 2023-04-05 16:00:01
forward
2956 people have browsed it

The content of this article is about the sample code for implementing simple jump prompts in virtual PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In PHP development, especially in MVC frameworks or projects, you will encounter many jump situations, such as jumps after successful or failed login, etc.

The following is based on the MVC framework development, with examples:

In the basic controller class: Conrtoller.class.php

<?php

/**
 * 基础控制器类
 */
class Controller {
    /**
     * 跳转
     * $url 目标url
     * $info 提示信息
     * $time 等待时间(单位秒)
     */
    protected function jump($url,$info=NULL,$time=3) {
        //判断是立即跳转还是刷新跳转
        if(is_null($info)) {
            //立即跳转
            header(&#39;location:&#39;. $url);
            die;
        } else {
            //刷新跳转,给出提示
            echo <<<TIAOZHUAN
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>提示信息</title>
<style type=&#39;text/css&#39;>
    * {margin:0; padding:0;}
    div {width:390px; height:287px; border:1px #09C solid; position:absolute; left:50%; margin-left:-195px; top:10%;}
    div h2 {width:100%; height:30px; line-height:30px; background-color:#09C; font-size:14px; color:#FFF; text-indent:10px;}
    div p {height:120px; line-height:120px; text-align:center;}
    div p strong {font-size:26px;}
</style>
<div>
    <h2>提示信息</h2>
    <p>
        <strong>$info</strong><br />
        页面在<span id="second">$time</span>秒后会自动跳转,或点击<a id="tiao" href="$url">立即跳转</a>
    </p>
</div>
<script type="text/javascript">
    var url = document.getElementById(&#39;tiao&#39;).href;
    function daoshu(){
        var scd = document.getElementById(&#39;second&#39;);
        var time = --scd.innerHTML;
        if(time<=0){
            window.location.href = url;
            clearInterval(mytime);
        }
    }
    var mytime = setInterval("daoshu()",1000);
</script>
TIAOZHUAN;
        die;
        }
    }

}
Copy after login

In the automatic loading of MVC, Automatically load and register Controller.class.php

After inheriting the above Controller.class.php basic controller class through different controller classes, you can call the defined jump prompt.

<?php

/**
 * 后台管理员控制器(登录、注销、管理员的增删改查等)
 */
class AdminController extends Controller {
    /**
     * 展示登录表单动作
     */
    public function loginAction() {
        // 载入当前的视图文件
        $this->display(&#39;login.html&#39;);
    }
    /**
     * 后台注销功能
     */
    public function logoutAction() {
        @session_start();
        // 删除相关会话数据
        unset($_SESSION[&#39;adminInfo&#39;]);
        // 删除会话数据区
        session_destroy();
        // 立即跳转到登录页面
        $this->jump(&#39;index.php?c=Admin&a=login&#39;,&#39;您已退出后台登录!&#39;);
    }
}
Copy after login

Of course, this is implemented in MVC, you can also use jump() separately.

Attached is a rendering:

The above is the detailed content of Sample code for implementing simple jump prompts in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!