關于構造函數死循環的問題
經測試,adminController.class.php控制類文件的構造函數會造成初次登錄時死循環,你想啊,每次訪問domain/admin.php?controller=admin&method=login這個頁面,都會執行一次構造函數,而構造函數內部檢測不到?$_SESSION['auth']) 這個變量,勢必會
執行$this->showmessage('請登錄后在操作!', 'admin.php?controller=admin&method=login');
這個函數,所以,不斷alert不斷跳回登錄界面,然后就PHP 程序就GG了。
public?function?__construct(){ session_start(); if(!(isset($_SESSION['auth']))&&(PC::$method!='login')){ $this->showmessage('請登錄后在操作!',?'admin.php?controller=admin&method=login'); }else{ $this->auth?=?isset($_SESSION['auth'])?$_SESSION['auth']:array(); } }
因為是構造函數,所以應該每次訪問admin控制器的時候,都應該其$_SESSION['auth'](或者登錄成功時設置$_SESSION['username'])變量是否存在,如果存在則轉入相應的方法頁面(方法為login則進入index頁面),否則允許進入原方法頁面(控制器自己執行無需干擾)
所以建議改成如下:
<?php class?adminController {? ??public?$auth; ??function?__construct() ??{ ????session_start(); ??} ??function?login() ??{ ????if(!isset($_POST['submit'])&&empty($_SESSION['auth'])) ????{?? ??????VIEW::display('admin/login.html'); ????} ????else ????{?//POST請求,驗證其信息 ??????$this->checkLogin(); ????} ??} ??function?checkLogin() ??{ ????$authObj?=?M('auth'); ??????if($authObj->loginsubmit())//驗證登錄信息 ??????{? ????????$this->showMsg('登錄成功','admin.php?controller=admin&method=index'); ??????} ??????else ??????{ ????????$this->showMsg('賬號或密碼錯誤,請重新登錄','admin.php?controller=admin&method=login'); ??????} ????} ????function?showMsg($info,?$url){ ??????echo?"<script>alert('$info');window.location.href='$url'</script>"; ??????exit; ????} ????function?logout() ????{ ??????unset($_SESSION['auth']); ??????$url?=?'http://'.$_SERVER['HTTP_HOST'].'/cmvc/admin.php?controller=admin&method=login'; ??????header('Location:?'.$url);? ????} ????function?index(){ ??????if(isset($_SESSION['auth'])) ??????{ ????????$newsobj?=?M('news'); ????????$newsnum?=?$newsobj->count(); ????????VIEW::assign(array('newsnum'=>$newsnum)); ????????VIEW::display('admin/index.html'); ??????} ??????else ??????{ ????????echo?"會話超時,請重新登錄!"; ????????$url?=?'http://'.$_SERVER['HTTP_HOST'].'/cmvc/admin.php?controller=admin&method=login'; ????????header('Location:?'.$url);? ??????} ????} ??} ???>