dsf5632 2013-02-18 22:10
浏览 21
已采纳

优化功能,用于加速应用程序

I use the following code to check some of the cases described in the comments. Unfortunately, it has come down page load speed. To improve the function and use them properly, what do you suggest, what do I do?

function insert() {
    if ($this - > session - > userdata('logged_in')) { //This is for test whether the user is logged in or not?
        if ($this - > siran - > access_level_active()) { //This is because the user name manager is active or not?
            if ($this - > siran - > access_level('1', '5')) { //This is for whether or not permission to access this page?
if($_POST){
                $this - > load - > view('admin/onepage');
}else{
        $this->session->set_flashdata('message_p', $this->lang->line('nopost'));
        redirect($this->session->userdata('previous_page'));
        }
            } else {
                $this - > session - > set_flashdata('message', $this - > lang - > line('notaccess'));
                redirect($this - > session - > userdata('previous_page'));
            }
        } else {
            $this - > session - > set_flashdata('message', $this - > lang - > line('noactive'));
            redirect('login', 'refresh');
        }
    } else {
        $this - > session - > set_flashdata('message', $this - > lang - > line('plslogin'));
        redirect('login', 'refresh');
    }
}

These three functions in library codeigniter:

public function access_level($mp, $access){
    //$mp = '1';
    $CI = &get_instance();
            $result = $CI->db->get_where('access_level', array('mainpage'=>$mp,'id_relation'=>$CI->session->userdata('id_relation')));
    $data = array();
    $out = array();
    foreach($result->result() as $row){
        $dv = json_decode($row->subpage);       
        $flat = array();        
        foreach ( $dv as $item ) {
            $flat = array_merge( $flat, explode( ',', $item ) );
        }       
        $out = array_merge( $out, $flat );
    }
    if(in_array($access, $out) || in_array('110', $out)){
        return  true;
    }else{
        return false;
    }
}
public function access_level_active(){
    //$mp = '1';
    $CI = &get_instance();
            $result = $CI->db->get_where('users', array('id'=>$CI->session->userdata('id')));
    if ($result->num_rows() > 0){
        $row = $result->row();
        return ($row->active == 1 ? true : false );
    }else{
        return false;
    }
}
function previous_page(){
    $CI = &get_instance();
    if ($CI->session->userdata('logged_in')) {
        if (isset($_SERVER['HTTP_REFERER']))
        {
            $CI->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
        }else
        {
            $CI->session->set_userdata('previous_page', base_url());
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duanqiao1949 2013-02-19 03:01
    关注

    Your code doesn't look particularly intensive, the only thing that I would change is to limit the amount of times you nest if{}else{} statements within themselves, and move any repetition of code out to another function. For example I would personally rewrite your insert() code as follows (mainly because it just looks a lot cleaner).

    Insert function:

    function insert() {
    
        if (!$this->session->userdata('logged_in')) {
            $this->_flashRedirect('login', $this->lang->line('plslogin'));  
        }
    
        if (!$this->siran->access_level_active()) {
            $this->_flashRedirect('login', $this->lang->line('noactive'));  
        }
    
        if (!$this->siran->access_level('1', '5')) {
            $this->_flashRedirect($this->session->userdata('previous_page'), $this->lang->line('notaccess'));       
        }
    
        if($_POST){
            $this->load->view('admin/onepage');
        } else {
            $this->_flashRedirect($this->session->userdata('previous_page'), $this->lang->line('nopost'), 'message_p'); 
        }
    
    }
    

    Create a new function _flashRedirect() in your controller:

    public function _flashRedirect($page, $message, $message_type = 'message') {
        $this->session->set_flashdata('message', $message);
        redirect($page, 'refresh');
    }
    

    As for speed, it sounds a lot more likely that your queries (mysql) are the cause of your performance issues. How many rows are you returning, how big are your tables?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?