The implementation of PHP's simple jump prompt

Keywords: PHP Session Javascript

In PHP development, especially in MVC framework or projects, there are many jump situations, such as: jump after login success or failure, etc.

Based on the MVC framework development, the following is an example:

In the basic controller class: controller.class.php

 1 <?php
 2 
 3 /**
 4  * Basic controller class
 5  */
 6 class Controller {
 7     /**
 8      * Jump
 9      * $url Target url
10      * $info Prompt information
11      * $time Waiting time in seconds
12      */
13     protected function jump($url,$info=NULL,$time=3) {
14         //Judge whether to jump immediately or refresh jump
15         if(is_null($info)) {
16             //Jump immediately
17             header('location:'. $url);
18             die;
19         } else {
20             //Refresh jump,Give hints
21             echo <<<TIAOZHUAN
22 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
23 <title>Prompt information</title>
24 <style type='text/css'>
25     * {margin:0; padding:0;}
26     div {width:390px; height:287px; border:1px #09C solid; position:absolute; left:50%; margin-left:-195px; top:10%;}
27     div h2 {width:100%; height:30px; line-height:30px; background-color:#09C; font-size:14px; color:#FFF; text-indent:10px;}
28     div p {height:120px; line-height:120px; text-align:center;}
29     div p strong {font-size:26px;}
30 </style>
31 <div>
32     <h2>Prompt information</h2>
33     <p>
34         <strong>$info</strong><br />
35         Page in<span id="second">$time</span>It will jump or click in seconds<a id="tiao" href="$url">Jump immediately</a>
36     </p>
37 </div>
38 <script type="text/javascript">
39     var url = document.getElementById('tiao').href;
40     function daoshu(){
41         var scd = document.getElementById('second');
42         var time = --scd.innerHTML;
43         if(time<=0){
44             window.location.href = url;
45             clearInterval(mytime);
46         }
47     }
48     var mytime = setInterval("daoshu()",1000);
49 </script>
50 TIAOZHUAN;
51         die;
52         }
53     }
54 
55 }

In MVC auto loading, register Controller.class.php for auto loading

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

 1 <?php
 2 
 3 /**
 4  * Background administrator controller (login, logout, addition, deletion, modification and query of administrator, etc.)
 5  */
 6 class AdminController extends Controller {
 7     /**
 8      * Show login form actions
 9      */
10     public function loginAction() {
11         // Load the current view file
12         $this->display('login.html');
13     }
14     /**
15      * Background logout function
16      */
17     public function logoutAction() {
18         @session_start();
19         // Delete related session data
20         unset($_SESSION['adminInfo']);
21         // Delete session data area
22         session_destroy();
23         // Jump to the login page now
24         $this->jump('index.php?c=Admin&a=login','You have quit background login!');
25     }
26 }

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

Attach a rendering:

Posted by BMurtagh on Sat, 30 Nov 2019 13:15:57 -0800