I am having issuing with session such after setting the session in my controller method, I be able to access in view but I cannot be able to access in model.
Here is the code:
public function get_username_ticket()
{
$user = $this->db->dbprefix('users');
$kq=$this->session->t_bys;
$sql = "SELECT $user.*
FROM $user
WHERE $user.deleted=0 AND $user.id = $kq";
return $this->db->query($sql)->row();
}
You should retrieve by:
$this->session->userdata('sessionVariable');
Related
I'm trying to force an authentication without login with Laravel 5.7 like that:
public function login()
{
$cpf = Request::only('cpf');
$user = new User;
$user = $user->where('cpf', $cpf)->get();
Auth::loginUsingId($user->id);
return redirect('/perfil');
}
And I get this error: Property [id] does not exist on this collection instance.
When I debug the model User, all the attributes are there. But when I try to get the attributes, I get this error. What I'm doing wrong?
If there's any other way to authenticate without password, It would be helpful!
If cpf is a unique column, then the following will return the object you're looking for:
$user = User::where('cpf', $cpf)->first();
If you do ->get() as in your code, you'll get a Collection with exactly one object in it, and will need to do $user->first()->id.
https://laravel.com/api/5.6/Illuminate/Database/Eloquent/Builder.html#method_get
You aren't using $user for anything else in the method, so you don't really need that object. You can use the value() method instead to get the id directly from the query.
public function login()
{
$cpf = Request::only('cpf');
// value() fetches the value of the specified column from the first row of query results
$id = User::where('cpf', $cpf)->value('id');
Auth::loginUsingId($id);
return redirect('/perfil');
}
I'm not really sure about the authentication part, though.
public function login()
{
$cpf = Request::only('cpf');
$user = User::where('cpf', $cpf)->first();
auth()->loginUsingId($user->id);
return redirect('/perfil');
}
I want to ask this question if how can we make a session in code igniter specially in logging in and logging out on the account. I want to know the step by step following the MVC of code igniter.
At the login time after executing query set session data in set_userdata function and passing data array whos you want to set.
$this->session->set_userdata('session data here');
And at the time of logout you have to call unset_userdata function and passing array of array whos you have to set at login time.
$this->session->unset_userdata('session data here');
using my code as an example you can do this i have a controller called iris.php and a model called script.php. i use the iris to call and make use of the script model.
class Iris extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('script');
$this->load->model('alert');
}
public function index()
{ $this->load->view('index');
}
public function login_in()
{
$login = $this->script->check_login();
if($login->num_rows() == 1){
foreach ($login->result_array() as $row) {
$newdata = array(
'fullname' => $row['fullname'],
'email' => $row['email'],
'member_id' => $row['member_id'],
'transtatus'=>$row['transtatus']
);
$this->session->set_userdata($newdata);
}
redirect('iris/user_home');
}else
{
$data = array('alert'=>$this->alert->log_alert());
$this->load->view('common/header');
$this->load->view('login',$data);
$this->load->view('common/footer');
}
}`
i first load the model script model under the constructor and in the login function of the iris controller i called the function in the script $login=$this->script->check_login();
in the script.php we have the following code.
{public function check_login(){
$email = $this->input->post('email');
$password = $this->input->post('password');
$query = "SELECT * FROM `iris_user`
WHERE`email`=? AND`password`= ? ";
$result = $this->db->query($query, array($email, $password));
return $result;
}
remember you have to have loader the session class helper form the application/config/autoload.php file in the CIfolder
$autoload['libraries'] = array('database', 'session');
the session is alway start once it has been autoloaded, but can be destroyed when maybe creating a logout function.
also note when adding to the session data variable to access the session variable you will have to use the name that was used when declaring the session variable. e.g to access the fullname you would do this in code
echo $_SESSION['fullname'];
In the controller load library session :
$this->load->library('session');
Use below sentence for session create :
$this->session->set_userdata("session_name",session_value);
For Session Unset:
$this->session->unset_userdata("session_name");
Hi good morning to everyone here, I have the following code which always returns me the value 'direccion' on the view, but I would like to retrieve all the values of the vase of data and put it to the meeting, as I can do that, they are thank you in advance.
Controller:
$authAdapter = new Zend_Auth_Adapter_DbTable();
$authAdapter
->setTableName('credential')
->setIdentityColumn('email')
->setCredentialColumn('password')
->setIdentityColumn('direccion');
$authAdapter
->setIdentity($form->getValue('email'))
->setCredential($form->getValue('password'))
->setIdentity('San marcos');
$select = $authAdapter->getDbSelect();
$select->where('status = "1"');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
View:
if (Zend_Auth::getInstance()->hasIdentity()) {
$username = Zend_Auth::getInstance()->getIdentity();
$profile = 'Welcome, ' . var_dump($username) . ' logout';
} else {
You can write your own LoginStorage class for the current user on zend_auth.
In this way, when you authenticate the user, write you LoginStorage with your custom values on Zend_auth.
Something like this:
<?php
//LoginStorage custom class
class LoginStorage {
public function __construct ($direccion){
$this->direccion = $direccion;
}
public $direccion;
}
So when you are running auth:
...
if (Zend_Auth::getInstance()->authenticate($myAuthAdapter)->isValid()) {
//Instance of UNIQUE auth -> get session writer to record authentication rowset
Zend_Auth::getInstance()->getStorage()->write(new LoginStorage($myDireccionForThisUser));
...
Now, when you get Zend_Auth::getInstance()->getStorage()->read(), there you be a LoginStorage object ready for you.
I am currently writing a login script because I am trying to learn PDO using OOP. I have a index.php page which only contain a login form. Then I have a User class, it looks like this:
<?php
include_once('database.php');
session_start();
class User{
public $id;
public $username;
public $password;
public $firstname;
public $lastname;
public function Login($username, $password) {
$db = new Database;
$db = $db->dbConnect();
$query = "SELECT * FROM users WHERE username = ? AND password = ?";
$statement = $db->prepare($query);
$statement->bindParam(1, $username);
$statement->bindParam(2, $password);
$statement->execute();
$rows = $statement->rowCount();
$data = $statement->fetchAll();
if( $rows == 1 ) {
$this->id = $data[0]['id'];
$this->username = $data[0]['username'];
$this->password = $data[0]['password'];
$this->firstname = $data[0]['firstname'];
$this->lastname = $data[0]['lastname'];
$_SESSION['SESSID'] = uniqid('', true);
header("location: dashboard.php");
}
}
}
?>
When the user is signed-in he/she goes to dashboard.php. I want to access the current User class from there, so I can use echo $user->username from there. But in dashboard.php, I have to declare the User class as new, so it doesn't keep all the variables.
Do you have any ideas on how i can access the User class variables in Dashboard.php which was declared in the Login-function?
Sorry for the bad explanation, but I hope you understand. Thank you in advance!
First off put your user class definition in another file and load it in like you do your database.php. In there you want only your class definition none of the session start business... <?php class User {....} ?> (the closing ?> is optionial).
so what you have now on your pages that need access to the user object is
<?php
include_once('database.php');
include_once('user.php');
session_start();
Then after a user has successfully logged you tuck the user in the session.
$_SESSION["user"] = $user;
Then when you want to get at it just say
$user = $_SESSION["user"];
echo $user->username;
What you could do is, put your user object into the session:
$obj = new Object();
$_SESSION['obj'] = serialize($obj);
$obj = unserialize($_SESSION['obj']);
or you could create a singleton, check out this link:
Creating the Singleton design pattern in PHP5
You have 2 options:
a) You store all the login info in a session.
b) You only store the user ID and some sort of identifier that the user has / is logged in, and create another method that will load the information from the database each time you load the page (bad idea really)
For example, you could add the following methods to your class in order to implement the above mentioned functionality and some more:
function createUserSession(array $userData) {
// Create / save session data
}
function readActiveUserSession() {
// Read current user information
}
function destroyActiveUserSession() {
// Call to destroy user session and sign out
}
Of course, you will have to add the appropriate code to the methods.
According to the cakebook section on the Auth component, I can implement simple authentication by using the following Users controller:
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Auth'); // Not necessary if declared in your app controller
/**
* The AuthComponent provides the needed functionality
* for login, so you can leave this function blank.
*/
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
I would like to be able to something like the following into my view:
<?php
$username = $auth->user('username');
echo "Welcome " . $username;
?>
Is there a simple way to do this, or do I need to overwrite the login function and store the username to the session?
Update
Alexander's answer is exactly what I wanted. However, I will add the following in case someone else gets confused like I did.
It took me a while to understand that if you change the model that Auth uses (for example, you might have a 'persons' table instead of 'users'), then you need to use something like:
$persondata = $session->read('Auth.Person');
Actually this information is easily available from the session. You use the session helper to grab it. I believe the correct syntax is :
$userdata = $session->read('Auth.User');
$username = $session->read('Auth.User.username');
EDIT:
For CakePHP 2.X and on the syntax is:
$userdata = $this->session->read('Auth.User');
$username = $this->session->read('Auth.User.username');
Check out AuthComponent-Methods in the CakePHP manual....
You can access an user info after a user has logged in from the session via $this->Auth->User(). So if you want the username, just use this in the controller.
$this->set('username', $this->Auth->User('username'));
You can now use $username in the view.
Add a method in your AppController
function beforeFilter() {
$ath = $this->Auth->user();
$this->set('userDetails', $ath['User']);
}
And then you can access it from your views and/or layouts via $userDetails
To access Auth vars in views just do it:
echo $session->read('Auth.User.id');