gpt4 book ai didi

php - PHP函数中的 session

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:50 25 4
gpt4 key购买 nike

我是 php 的新手,我从登录我的 session 的地方创建了一个函数,现在我想多次使用这个函数,比如管理员登录,我如何在多个 session 中使用这个函数。我按照 PacMan 的说明更新了我的答案。现在我是这样做的:

用户和管理员登录代码:

//Check Wheather User Logged in or Not

if (isset($_SESSION['LoggedID']) && !empty($_SESSION['LoggedID'])){
$tableName = "prd_rgistration";
$id = $_SESSION['UserLogin'];
}else{
$tableName = "user_register";
$id = $_SESSION['AdminID'];
}
//Check Wheather User Logged in or Not
function Logged(){
if(LoggedIn()){
global $conn;
return $conn->query(sprintf('SELECT * FROM ' + $tableName + ' WHERE id = \'%d\'',$id))->fetchObject();
}else{
return (object)array();
}
}
function LoggedIn(){
return isset($id) && intval($id);
}
function redirect($Location){
header(sprintf('Location: %s',$Location),true,301);
exit;
}

if(LoggedIn()){
$User = Logged();
}

管理员登录页面代码

if($email_err == "" && $password_err == ""){
$Code = 0;
try{
$get_data = $conn->query("SELECT * FROM `user_register` WHERE `reg_email` = '$email' AND `reg_pass` = '$password' LIMIT 1");
if($get_data->rowCount() > 0){
$_SESSION['AdminID'] = $get_data->fetchObject()->id;
redirect('/domain.com/temp/admin/index.php');
}else{
$error = '<div class="alert alert-info">Invalid Email/Password</div>';
}
}catch(PDOException $E){
$Code = $E->getCode();
}
}

用户登录页面代码

if($email_err == '' && $user_pass_err == ''){
$Code = 0;
try{
$get_data = $conn->query("SELECT * FROM `prd_rgistration` WHERE `email_db` = '$email' AND `password_db` = '$user_pass' LIMIT 1");
if($get_data->rowCount() > 0){
$_SESSION['UserLogin'] = $get_data->fetchObject()->id;
redirect('/domain.com/temp/index.php');
//redirect('/domain.com/index.php');
}else{
$error = '<div class="alert alert-info">Invalid Email/Password</div>';
}
}catch(PDOException $E){
$Code = $E->getCode();
}
}

现在它给出错误 Undefined AdminID 并且也没有登录用户。

最佳答案

我可以建议您按照此过程进行操作,它可能会有所帮助,方法是将表名更改为变量

if (isset($_SESSION['LoggedID']) && !empty($_SESSION['LoggedID'])){
$tableName = "prd_rgistration";
$id = $_SESSION['LoggedID'];
}else if(isset($_SESSION['adminID']) && !empty($_SESSION['adminID'])){
$tableName = "admin";
$id = $_SESSION['adminID']
}
//Check Wheather User Logged in or Not

function Logged(){
if(LoggedIn()){
global $conn;
return $conn->query(sprintf('SELECT * FROM '$tableName' WHERE id = \'%d\'',$id))->fetchObject();
}else{
return (object)array();
}
}

这里是您只为用户管理员和用户登录设置了一个函数参数

关于php - PHP函数中的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38131308/

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