1 回答

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊
這完全取決于您想要實(shí)現(xiàn)的目標(biāo)。如今,很容易使用一些框架或微框架來覆蓋引擎蓋下的一些基本內(nèi)容,這使您不必再次重新創(chuàng)建自行車。這就是你獲得優(yōu)勢的地方。在你的情況下,如果這是一個模板項目,我會這樣做:
項目根目錄/index.php
...
? ? if (isset($_POST['btnLogOn'])) {??
? ??
? ? ? ?...//get find user if exist
? ??
? ? ? ?$domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
? ? ? ?if ($row && password_verify($password, $row['password'])) {?
? ? ? ? ? if (isset($_POST['remember'])) {
? ? ? ? ? ? ?setcookie('email', $email, time() + 60 * 60 * 7);
? ? ? ? ? ? ?setcookie('password', $password, time() + 60 * 60 * 7);
? ? ? ? ? }
? ? ? ? ? $_SESSION['user_session'] = $row['user_id'];
? ? ? ? ? $_SESSION['user_role'] = $row['role'];
? ? ? ? ? switch($_SESSION['user_role']){
? ? ? ? ? ? ?case 'Admin':
? ? ? ? ? ? ? ? $dir = 'admin';
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case 'Advisor':
? ? ? ? ? ? ? ? $dir = 'advisor';
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case 'Tech':
? ? ? ? ? ? ? ? $dir = 'tech';
? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ?case 'User':
? ? ? ? ? ? ? ? $dir = 'dashboard';
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? $dir = 'index.php';
? ? ? ? ? ?}
? ? ? ? ? ?header( 'Location: ' . $domain . DIRECTORY_SEPARATOR . $dir);
? ? ? ? }
? ? }
...
和這里:
根目錄/admin/index.php
$domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
//check if user exist it is admin
if(!isset($_SESSION['user_session']) || (isset($_SESSION['user_role']) && $_SESSION['user_role'] != 'admin')){
? ? //it is not, redirect
? ? header( 'Location: ' . $domain . DIRECTORY_SEPARATOR . 'index.php');
}
但它應(yīng)該更加安全。因為現(xiàn)在這段代碼對攻擊者開放。
- 1 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報