I am trying to set the company name in a session and retrieve the same to connect to a different database, i am setting the session using $this->session->set_userdata($newdata); and retrieving the session using $companyName = $this->session->userdata['newdata']['company']; but somehow retrieval is not happening and i am unable to load the correct db for updating logout information i.e it simply does not update the pr_system_attendance table by connecting to a different db. I am getting the correct value if i echo $company; after $company = $row1->company; this is FYI
My model code is as follows:
function check_admin_login(){
$this->db->where('username', trim($this->input->post('username')));
$this->db->where('userpass ', sha1(trim($this->input->post('userpass'))));
$this->db->where('status', '1');
$this->db->where('deleted', '0');
$this->db->select('*');
$query = $this->db->get($this->myTables['users']);
if($query->num_rows() > 0){
$row = $query->row();
$this->db->where('userid', $row->id);
$this->db->select('firstname,lastname,profileimage,company');
$query1 = $this->db->get($this->myTables['users_details']);
$row1 = $query1->row();
$newdata = array(
'is_admin_logged_in' => true,
'admin_user_name' => $row->username,
'admin_userpass' => $row->userpass,
'admin_id'=>$row->id,
'admin_lastlogin'=>date("d-m-Y H:i:s",$row->lastlogin),
'admin_lastloginip'=>$row->lastloginip,
'lastrefresh'=>time(),
'company'=>$row1->company
);
$company = $row1->company;
$this->session->set_userdata($newdata);
$companyName = $this->session->userdata['newdata']['company'];
$this->update_admin_login_time($this->session->userdata('admin_id'));
$this->admin_init_elements->set_global_user($row->username,$row->userpass);
if($this->input->post('remember'))
{
$cookie = array('name' => 'username','value' => $row->username,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie);
}
$name = $row1->firstname.' '.$row1->lastname;
$cookie1 = array('name' => 'name','value' => $name,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie1);
$cookie2 = array('name' => 'image','value' => $row1->profileimage,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie2);
return 'Login Successful';
}else{
return 'Incorrect Username or Password.';
}
}
function logout()
{
global $USER;
$companyName = $this->session->userdata['newdata']['company'];
$otherdb = $this->load->database("$companyName", TRUE);
$this->db->from("$companyName"."pr_users");
$query1 = $this->db->query("Select * from pr_system_attendance where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE()");
date_default_timezone_set('Asia/Calcutta');
if($query1->num_rows() > 0)
{
$row = $query1->row();
$sql1 = "UPDATE pr_system_attendance set logout_time = '".date('Y-m-d H:i:s')."' where userid = '".$USER->id."' and DATE(`login_time`) = CURDATE()";
$query2 = $this->db->query($sql1);
}
$sql="UPDATE `".$this->myTables['users']."` SET
`if_online` = '0'
WHERE `id` = '".$USER->id."'" ;
$query=$this->db->query($sql);
}
Get session data with below line of code
//$companyName = $this->session->userdata['newdata']['company'];
$companyName = $this->session->userdata('company');
Related
I've a webpage to a webinar with id 217, and to to verify if a user can watch the webinar, I use that function:
function user_verify($idlive, $register){
require("connect.php");
$query = "SELECT * FROM `users` WHERE idlive = 217 && register = '$register'";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);
mysqli_close($conn);
if(isset($user)){
return 1;
} else {
return 0;
}
}
If return 1, I call the function to access the webinar:
function login($register){
require("connect.php");
$data_hora_inscricao = date('d/m/y H:i:s');
$query = "SELECT * FROM `users` WHERE register = '$register' && idlive = 217";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);
mysqli_close($conn);
if(isset($user)){
$data = [
'u_id' => $user['id'],
'u_name' => $user['nome'],
'u_email' => $user['email'],
'u_level' => $user['level'],
'u_time' => 0
];
$dadosUserCookie = serialize($data);
setcookie('d_user_217', $dadosUserCookie, time() + (86400 * 1), "/");
return 1;
} else {
return 0;
}
}
If it returns 1 (success), I set a cookie to request user data on other pages. It always worked well, until yesterday. I don't know what's going on, but my AWS Lightsail database shows me that this code is using 100% of database capacity. What I can do to solve this?
I am getting a PHP error undefined variable in my code. I am using PDO to json_encode the output. When I test my code I am getting that error. How can I fix and avoid this in the future? Is my code structure ok? or do I need to improve it in order to avoid this problem
Undefined variable: ar
$sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
$sql->bindValue(":contactid", $ContactID);
$sql->bindValue(":lastchecked", $LastChecked);
$sql->bindValue(":deleted", 1);
if($sql->execute()){
$count = $sql->rowCount();
if($count > 0){
while ($row = $sql->fetch()) {
$decr = CryptRC4(FromHexDump($row['UsrPassword']), $key);
if($row['ServerUpdate'] > $row['ClientUpdate']){
$lupdate = date("Y-m-d h:i:s", strtotime($row['ServerUpdate']));
}
else{
$lupdate = date("Y-m-d h:i:s", strtotime($row['ClientUpdate']));
}
$ar[] = array(
'UserID' => $row['UserID'],
'UsrPassword' => $decr,
'ContactID' => $row['ContactID'],
'UserTypeID' => $row['UserTypeID'],
'UserStatus' => $row['UserStatus'],
'Deleted' => $row['Deleted'],
'LastUpdated' => $lupdate
);
}
}
}
else{
$ar[] = array(
"Message" => $sql->errorInfo(),
"sql" => $sql
);
}
print json_encode($ar);
you need to declare $ar variable as a array before if statement so the scope of variable is not limited and you can access this outside If else.
$sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
$sql->bindValue(":contactid", $ContactID);
$sql->bindValue(":lastchecked", $LastChecked);
$sql->bindValue(":deleted", 1);
$ar = array();
if(){
// You code goes here
}
else{
// You code goes here
}
print_r($ar);
seems if($count > 0){ condition is not satisfied ,Declare ar before this,hope this helps
$sql = $conn->prepare("SELECT UserID, UsrPassword, ContactID, UserTypeID, UserStatus, ClientUpdate, ServerUpdate, Deleted FROM tblUsers WHERE ContactID = :contactid AND ServerUpdate > :lastchecked AND Deleted != :deleted", $cursor);
$sql->bindValue(":contactid", $ContactID);
$sql->bindValue(":lastchecked", $LastChecked);
$sql->bindValue(":deleted", 1);
$ar = array();
if($sql->execute()){
$count = $sql->rowCount();
if($count > 0){
while ($row = $sql->fetch()) {
$decr = CryptRC4(FromHexDump($row['UsrPassword']), $key);
if($row['ServerUpdate'] > $row['ClientUpdate']){
$lupdate = date("Y-m-d h:i:s", strtotime($row['ServerUpdate']));
}
else{
$lupdate = date("Y-m-d h:i:s", strtotime($row['ClientUpdate']));
}
$ar[] = array(
'UserID' => $row['UserID'],
'UsrPassword' => $decr,
'ContactID' => $row['ContactID'],
'UserTypeID' => $row['UserTypeID'],
'UserStatus' => $row['UserStatus'],
'Deleted' => $row['Deleted'],
'LastUpdated' => $lupdate
);
}
}
}
else{
$ar[] = array(
"Message" => $sql->errorInfo(),
"sql" => $sql
);
}
print_r(json_encode($ar));
I need select Name from databese where Email = $email;
if ($result) {
$Name = $this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$sess_array = array(
'Name' => $Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
You need to fetch Name from database then assign to your session
if ($result) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email',$Email);
$query=$this->db->get();
$result=$query->row(); // fetch single data Name
$sess_array = array(
'Name' => $result->Name // set Name to array
);
$this->session->set_userdata('logged_in',$sess_array);
}
Try this,
if ($result) {
$this->db->select('Name');
$query = $this->db->get_where('users',array('Email' => $Email));
$result = $query->row();
$sess_array = array(
'Name' => $result->Name
);
$this->session->set_userdata('logged_in',$sess_array);
print_r($sess_array);
}
also refer CodeIgniter Select Query
I am so confused here, how can i select table where schedule_date_time = $_GET['date_time'];
<?php
require 'core/init.php';
$schedule_id = $_GET['id'];
$schedule_date_time = $_GET['date_time'];
$session_id = $_GET['session'];
$user_id = $_GET['user'];
$user_email = $_GET['email'];
$user_name = $_GET['username'];
$registry_id = $_GET['registry'];
$check1 = DB::getInstance()->query("SELECT * FROM chat_schedule WHERE schedule_date_time = $schedule_date_time");
if ($check1->count()) {
$insert = DB::getInstance()->insert('chat_booked', array(
'schedule_id' => $schedule_id,
'schedule_date_time' => $schedule_date_time,
'session_id' => $session_id,
'user_id' => $user_id,
'user_email' => $user_email,
'user_name' => $user_name,
'registry_id' => $registry_id
));
if ($insert = true) {
echo "Insert was Successful";
}
} else {
echo "No Match Found, INSERT FAILED!";
}
Do i need to parse the date or something. I can not figure this out :(
change the $schedule_date_time to '$schedule_date_time' like:
"SELECT * FROM chat_schedule WHERE schedule_date_time = '$schedule_date_time' "
My else statement is not getting executed when it should. There are no errors in the code. It simply has to do with the flow of my query and foreach statement but I cannot seem to figure this out. Any ideas?
$id = $this->input->post('id');
$session_id = $this->input->post('session_id');
$q1 = $this->db->query("SELECT *
FROM default_cart
WHERE cookie_id = '$session_id'
AND product_id = '$id' LIMIT 1");
foreach($q1->result() as $row) {
if($row->cookie_id != $session_id && $row->product_id != $id) {
echo json_encode(array('error_code' => 'e100'));
} else {
$data = array('product_id' => $id,
'active' => 'Yes',
'cookie_id' => $session_id
);
$this->db->insert('default_cart', $data);
echo json_encode(array('success' => true));
}
}
First, I would like to suggest that there is no sense of looping through the query when there is only one record LIMIT 1 and no sense of if check while you have already checked the all parameters in the query WHERE cookie_id = '$session_id' AND product_id = '$id' just fetch the row returned by the query and check for empty or not.
$id = $this->input->post('id');
$session_id = $this->input->post('session_id');
$q1 = $this->db->query("SELECT * FROM default_cart
WHERE cookie_id = '$session_id' AND product_id = '$id' LIMIT 1");
$row=$q1->row();
if(!empty($row)) {
$data = array('product_id' => $id,
'active' => 'Yes',
'cookie_id' => $session_id);
$this->db->insert('default_cart', $data);
echo json_encode(array('success' => true));
}else{
echo json_encode(array('error_code' => 'e100'));
}