gpt4 book ai didi

php - 如何在php中根据 session 显示菜单?

转载 作者:行者123 更新时间:2023-11-29 19:08:50 25 4
gpt4 key购买 nike

I have 2 tables admin and superadmin. I have same login page for both users. i can login according to query.i had create sessions for both users. each table have column name roleID. for superadmin roleID is 1, and for admin roleID is 2. Below is my code for login where i create session. i used print_r and my session are working. Below is my code for login page.

if (isset($_REQUEST['submit'])) 
{
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '".$username."' AND `acc_pass` = '".$password."'");
$data = mysqli_fetch_array($sql);
$_SESSION['role0']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$sql1 = mysqli_query($conn,"SELECT * FROM `superadmin` where `username` = '".$username."' AND `password` = '".$password."'");
$data1 = mysqli_fetch_array($sql1);
$_SESSION['role1']=$data1['roleId'];
if ($data>0)
{
header('Location: societyList.php');
}
elseif ($data1>0) {
header('Location: home.php');
}

else
{
header('Location: index.php');
echo 'incorrect login';
}
}

Now on home.php i have some menus to show accoording to roleID my code for menu.

<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="index01.php">Create Society</a></li>
</ul>
</li>
<li><a><i class="fa fa-home"></i> Master <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="index1.php">Units</a></li>
<li><a href="index2.php">Members</a></li>
<li><a href="index2.php">Parking Lots</a></li>
<li><a href="index2.php">Charges</a></li>
<li><a href="index2.php">Chart of Account</a></li>
<li><a href="index2.php">Interest Penalties</a></li>
<li><a href="form_buttons.php">Billing Templates</a></li>
<li><a href="form_buttons.php">Tax Structure</a></li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Transactions <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Bill</a></li>
<li><a href="form_advanced.php">Collection</a></li>
<li><a href="form_validation.php">Expenses</a></li>
<li><a href="form_wizards.php">Journal</a></li>
<li><a href="form_upload.php">Bank Reco</a></li>
<li><a href="form_buttons.php">Drop Box</a></li>
<li><a href="form_upload.php">Online Payment</a></li>
</ul>
</li>
<li><a><i class="fa fa-desktop"></i> Reports <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="general_elements.php">Income & Expenses</a></li>
<li><a href="media_gallery.php">Balance Sheet</a></li>
<li><a href="typography.php">Cash Flow</a></li>
<li><a href="icons.php">Interest Calculation</a></li>
</ul>
</li>
<li><a href="tables.php"><i class="fa fa-table"></i> Notices <span class="fa fa-chevron-down"></span></a>
</li>
<li><a><i class="fa fa-bar-chart-o"></i> Registers <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="chartjs.php">Form-I</a></li>
</ul>
</li>
<li><a><i class="fa fa-clone"></i> Forum <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="fixed_sidebar.php">Cultural Activity</a></li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Domestic Help <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Request for Plumber</a></li>
<li><a href="form_advanced.php">Request for Maid</a></li>
<li><a href="form_validation.php">Request for House Cleaner</a></li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Emergency <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Ambulance</a></li>
<li><a href="form_advanced.php">Fire Brigade</a></li>
<li><a href="form_validation.php">Police</a></li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Helpdesk <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Request NOC for Tenancy</a></li>
<li><a href="form_advanced.php">Request NOC for Home Loan</a></li>
<li><a href="form_validation.php">Request NOC for Mortgage as collateral</a></li>
<li><a href="form_wizards.php">Request for Vehicle Parking</a></li>
<li><a href="form_upload.php">Application for Transfer</a></li>
<li><a href="form_buttons.php">Request for waiver of interest</a></li>
<li><a href="form_buttons.php">Request for waiver of a charge</a></li>
<li><a href="form_buttons.php">Suggestion</a></li>
<li><a><i class="fa fa-edit"></i><span class="fa fa-chevron-down"></span>Complaint</a>
<ul class="nav child_menu">
<li><a href="form_buttons.php">About leakage</a></li>
<li><a href="form_buttons.php">About tenants issues</a></li>
<li><a href="form_buttons.php">About parking nuisance</a>
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> CFO Desk Assists <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Tenant Rating</a></li>
<li><a href="form_advanced.php">Owners Rating</a></li>
<li><a href="form_validation.php">Your Reviews</a></li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Masters <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a href="form.php">Auto Bank reconcilliation</a></li>
<li><a href="form_advanced.php">AMC Masters</a></li>
<li><a href="form_validation.php">Auto Adjust pending reference</a></li>
<li><a href="form_wizards.php">Default GL for defined transactions</a></li>
</ul>
</li>
</ul>
</div>
</div>

正如您所看到的列表。我希望 if $_SESSION['role0']=$data['roleId']; 在 session 中,因此前 4 个列表仅对他可见。如果 $_SESSION['role1']=$data['roleId']; 在 session 中,则所有列表的其余部分将显示给他。如何做到这一点,请帮助我。我用了 if 条件喜欢if{$_REQUEST($_SESSION['role0'])
echo '一些列表';
}
如果我像这样使用 if 条件,则不会显示任何内容。

最佳答案

session 数据存储在 $_REQUEST 中。

$_SESSION['role0']=$data1['roleId'];
$_SESSION['role1']=$data1['roleId'];

“role0”和“role1”将具有相同的值。可以简化为:

$_SESSION['role']=$data1['roleId'];

然后使用:

if( $_SESSION['role'] === 1) {
echo 'admin role 1';
echo 'Show first half of menu';
} elseif ($_SESSION['role'] === 2) {
echo 'Show second half of menu';
} else {
echo 'Other or missing admin value. show no menu';
}

按照目前的编写方式,SQL 注入(inject)是可能的。接下来添加准备好的语句将是一件好事。

关于php - 如何在php中根据 session 显示菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com