第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我無法為不同的用戶角色啟動會話并限制注銷用戶的文件夾

我無法為不同的用戶角色啟動會話并限制注銷用戶的文件夾

PHP
精慕HU 2023-11-03 17:40:11
我正在使用登錄腳本,并且正在嘗試了解會話,但運(yùn)氣不佳。當(dāng)用戶登錄時,我希望他們重定向到相關(guān)文件夾,這是我的登錄腳本  if (isset($_POST['btnLogOn'])) {          $email     = !empty($_POST['email']) ? trim($_POST['email']) : null;        $password = !empty($_POST['password']) ? $_POST['password'] : null;           $db=DB();        $sql = "SELECT  * FROM users WHERE  email = ? LIMIT 1";        $stat = $db->prepare($sql);        $stat->bindParam(1, $email, PDO::PARAM_STR);        $stat->execute();        $row = $stat->fetch();        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':                 $_SESSION['user_role'];                 header( 'Location: admin');                 break;             case 'Advisor':                 $_SESSION['user_role'] ;                 header( 'Location: advisor');                 break;              case 'Tech':                 $_SESSION['user_role'] ;                 header( 'Location: tech');                 break;                 case 'User':                 $_SESSION['user_role'];                  // redirect to admin                 header( 'Location: dashboard');                 break;                 }我不確定上面的代碼是否正確,并且我真的很難在每個“命名”文件夾的標(biāo)題中編寫什么代碼。我在單獨(dú)的標(biāo)頭中嘗試了很多亂七八糟的代碼,但它要么不斷刷新到 ../index.php,要么讓我打開文件夾,無論我是否登錄            session_start();         $_SESSION['user_session'] = $row['user_id'];          $_SESSION['user_role'] = $row['role'];            if(empty($_SESSION['user_role'])){            header('Location: ../index.php');            }當(dāng)用戶登錄角色時我想要發(fā)生的事情確定要打開哪個文件夾,如果沒有人登錄,他們將無法打開該文件夾
查看完整描述

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)在這段代碼對攻擊者開放。


查看完整回答
反對 回復(fù) 2023-11-03
  • 1 回答
  • 0 關(guān)注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號