首页 > 解决方案 > PHP 如果未登录,停止加载视图的正确方法

问题描述

我有一个 PHP 网站和一个登录页面。如果未创建会话“Caisse_login”,则在构造类时加载。我看到了一个我用 exit() 修复的错误,但我想知道是否有其他方法可以做到这一点。

这是我的课的开始:

类 caisses 扩展控制器{

    公共函数 __Construct(){

        $this->caisseModel = $this->model('caisse');

        if(!isset($_SESSION['Caisse_login']) 或 $_SESSION['Caisse_login']==FALSE){
            
            $this->login();
            出口();
        }
    }

每次输入 url 时,它都会调用相关的方法。所以如果我使用以下网址:

caisselive/en/caisses/showTable/3

因此,它将在 caisses 之上创建类,以及以下方法:

public function showTable($tableId = 0){

    // control if there is at least one table
    if(!$this->caisseModel->countTable()){

        flash('Transaction', 'No any table have been created', 'alert alert-warning');
        redirect('caisses/transaction');

    }else if($tableId==0){
        $data = [
            'table' => $this->caisseModel->getAllTable(),
        ];

        $this->view('caisses/tableShow', $data);
        


    }else{

    // control if the ID provided is an existing table
        if(!$this->caisseModel->getTempTablebyID($tableId)){

            flash('Transaction', 'The table does not exist', 'alert alert-warning');
            redirect('caisses/showTable');
        }else{

    //Display the table
                $totalPrice = 0;

                $productsQuery = $this->caisseModel->getTempTableProducts($tableId);

                $tempTable = $this->caisseModel->getTempTablebyID($tableId);
                
                $i=0;                   
                $k=0;
                $totalPricePaid=0;
                $productsPaid = null;
                foreach($productsQuery as $products_1){
                // control if it has already been paid
                    if($products_1->paid==0){
                        for($j=0;$j<$products_1->qty;$j++){

                            $products[$i]['productName'] = $products_1->product_name;
                            $products[$i]['productID'] = $products_1->id_product;
                            $products[$i]['transactionId'] = $products_1->id;
                            $products[$i]['rebate'] = $products_1->percentage;
                            $products[$i]['rebateID'] = $products_1->id_rebate;
                            $products[$i]['rebateName'] = $products_1->name;
                            $products[$i]['qty'] = 1;
                            $products[$i]['price'] = $products_1->price_s;
                            $products[$i]['priceTotInt'] = number_format($products_1->price_s * 1 * (1-($products_1->percentage/100)), 2);

                            $totalPrice = $totalPrice + $products[$i]['priceTotInt'] ;

                            $i++;
                        }
                //create the list of what it has already have been paid
                    }else{
                        $productsPaid[$k]['productName'] = $products_1->product_name;
                        $productsPaid[$k]['productID'] = $products_1->id_product;
                        $productsPaid[$k]['transactionId'] = $products_1->id;
                        $productsPaid[$k]['rebate'] = $products_1->percentage;
                        $productsPaid[$k]['rebateID'] = $products_1->id_rebate;
                        $productsPaid[$k]['rebateName'] = $products_1->name;
                        $productsPaid[$k]['qty'] = $products_1->qty;
                        $productsPaid[$k]['price'] = $products_1->price_s;
                        $productsPaid[$k]['priceTotInt'] = number_format($products_1->price_s * $products_1->qty * (1-($products_1->percentage/100)), 2);

                        $totalPricePaid = $totalPricePaid + $productsPaid[$k]['priceTotInt'] ;
                        
                        $k++;
                    }
                }

                $data = [
                    'products' => $products,
                    'productsPaid' => $productsPaid,
                    'dateTable' => $tempTable->date,
                    'nameTable' => $tempTable->nameTable,
                    'tableId' => $tableId,
                    'rebate' => $this->caisseModel->getAllRebate(),
                    'totalPrice' => number_format($totalPrice, 2),
                    'totalPricePaid' => number_format($totalPricePaid, 2)
                ];

                $this->view('caisses/tableShowDetail', $data);
                
        }
    }
}

如果这个人没有登录会发生什么,它将加载 2 个视图,正如解释的那样,我在构建类时用 exit() 修复了它,但我想知道是否没有更好的方法呢?

标签: phpauthentication

解决方案


有不同的选择。如果您使用框架,则可以使用一种方法将此请求重定向到另一个控制器。另一个选项是设置一个布尔值来指示该用户未登录并且您在控制器中处理此参数。您已经实现的最简单的解决方案,只是一个退出,但是如果您需要在控制器中的每个操作结束时执行某些操作,这可能会导致一些问题。


推荐阅读