Why codeigniter session destroyed after page re direction,
this is my auto load conf
$autoload['libraries'] = array('database','form_validation','session');
my login controller code is
$result = $this->Loginmodel->verify_user($this->input->post('username'), $this->input->post('password'));//returned as db->result()
if ($result !== False) {
//person has an account
foreach ($result as $obj) {
$user_id = $obj->user_id;
$userdesignation = $obj->user_designation;
$user_access = $obj->user_cpanelpass;
}
$sessiondata = array(
'user_id' => $user_id,
'username' => $username,
'loginuser' => TRUE,
'userdesignaton' => $userdesignation,
);
$this->session->set_userdata($sessiondata);
$sessiondata1 = array(
'shopname' => 'abc',
'shopplace' => 'xyz'
);
$this->session->set_userdata($sessiondata1);
$ipaddress = $this->ipaddress();
$this->Loginmodel->loginloginsert($username, $ipaddress);
redirect('home');
this is working fine in my system but not working in another system .The session is destroyed automatically when redirected to home or any other controller.
Related
The situation:
I build an authentication service that uses Basic Authentication to check if the user exists on an external database and fetches some data. The users in question only exist on the external database.
The problem:
Typo3 needs to have an user entry in the fe_user table to login the user.
So whenever this entry does not exist, the user cannot login.
What I want to do:
Create the user in the authentication service to avoid using a sql dump from the external database and ensure that synchronisation is possible.
The relevant code:
public function authUser(array $user) {
$a_user = $this->login['uname'];
$a_pwd = $this->login['uident_text'];
$url = 'https://soliday.fluchtpunkt.at/api/queryMediaItems';
$data = json_decode('{"language":"de-at"}');
$basicAuth = base64_encode("$a_user:$a_pwd");
// use key 'http' even if you send the request to https://...
$options = array (
'http' => array (
'header' => array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic {$basicAuth}"
),
'method' => 'POST',
'content' => '{"language":"de-at"}'
)
);
$context = stream_context_create ( $options );
$result = file_get_contents ($url, false, $context);
$response = gzdecode($result);
$checkUser = $this->fetchUserRecord ( $this->login ['uname'] );
if (!is_array($checkUser)&& $result!== FALSE) {
$this->createUser();
}
// failure
if ($result === FALSE) {
return static::STATUS_AUTHENTICATION_FAILURE_BREAK;
}
$this->processData($response);
// success
return static::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}
public function createUser() {
$username = $this->login ['uname'];
$password = $this->login ['uident_text'];
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', "username = '" . $username . "' AND disable = 0 AND deleted = 0" );
if (! $record) {
// user has no DB record (yet), create one using defaults registered in extension config
// password is not important, username is set to the user's input
$record = array (
'username' => $username,
'password' => $password,
'name' => '',
'email' => '',
'disable' => '0',
'deleted' => '0',
'pid' => $this->config ['storagePid'],
'usergroup' => $this->config ['addUsersToGroups'],
'tstamp' => time ()
);
if (t3lib_extMgm::isLoaded ( 'extbase' )) {
$record ['tx_extbase_type'] = $this->config ['recordType'];
}
$GLOBALS ['TYPO3_DB']->exec_INSERTquery ( 'fe_users', $record );
$uid = $GLOBALS ['TYPO3_DB']->sql_insert_id ();
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', 'uid = ' . intval ( $uid ) );
}
$_SESSION [$this->sessionKey] ['user'] ['fe'] = $record;
}
the ext_localconf.php file:
<?php
if (!defined('TYPO3_MODE')) {
die ('Access denied.');
}
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
$_EXTKEY,
'auth' /* sv type */,
'AuthService' /* sv key */,
array(
'title' => 'GET Authentication service',
'description' => 'Authenticates users with GET request.',
'subtype' => 'getUserFE, authUserFE',
'available' => true,
'priority' => 90,
'quality' => 90,
'os' => '',
'exec' => '',
'className' => Plaspack\professionalZoneLogin\Service\AuthService::class,
)
);
You should extend AuthenticationService with your own code, way of doing that is described here https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Xclasses/Index.html
Not sure if it's related, but t3lib_extMgm should be \TYPO3\CMS\Core\Utility\ExtensionManagementUtility unless you're using TYPO3 6.
You can also see if you get any SQL errors by calling $GLOBALS['TYPO3_DB']->sql_error().
i want to ask how do i check/see the value inside of this $datame in my code, i tried to var_dump it, i am using mozilla, it didn't show the value in console/network . Where or how do i check if there were data stored in my variable array.
public function facebook_request(){
$this->load->library('facebook/src/facebook', array('appId' => '1027885817316593', 'secret' => '6db175699897b514bf4881955b2944bb'));
$this->user = $this->facebook->getUser();
if ($this->user) {
$data['user_profile'] = $this->facebook->api('/me?fields=id,first_name,last_name,email,link,gender,locale,picture');
// Get logout url of facebook
$data['logout_url'] = $this->facebook->getLogoutUrl(array('next' => base_url() . 'index.php/oauth_login/logout'));
$datame = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $data['user_profile']['id'],
'first_name' => $data['user_profile']['first_name'],
'last_name' => $data['user_profile']['last_name'],
'email' => $data['user_profile']['email'],
'gender' => $data['user_profile']['gender'],
'locale' => $data['user_profile']['locale'],
'picture' => $data['user_profile']['picture']['data']['url'],
'link' => $data['user_profile']['link']
);
$userData = $this->user_model->checkUser2($datame);
var_dump($datame);
exit();
// Send data to profile page
$this->load->view('admin/profile.php', $data);
}
The server doesn't have access to write to that. You could pass $datame to the view you are trying to render and print it there or you could replace var_dump($datame) with error_log($datame) and then view your error log to see what it printed
change from
$userData = $this->user_model->checkUser2($datame);
var_dump($datame);
exit();
to
echo "<pre>";
print_r($datame);
exit();
$userData = $this->user_model->checkUser2($datame);
I have a directory out side the app folder as this localhost/myproject/director
i want to made a redirection for this directory. if someone is not logged in and direct access this directory so it will be redirected to home page.
i have tried this but nothing works:
if(!$_SESSION['login']){
header("location:index.php");
die;
}
Assume your Session is set like this
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
So to check
if(logged_in == TRUE)
{
# logged User
$this->load->view("homepage");
}
else{
#not logged User
redirect("Login"); # set login controller name
}
For Your Information
If User Log once then set session with your input data
$newdata = array(
'username' => $user_name,
'email' => $email,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Codeigniter Session Library
Well I have created sessions but having trouble implementing cookies in my website. In config file i have set $config['sess_expire_on_close'] = TRUE; so that user's session expires on closing browser.
Now what i want is, On login if user checks remember me ... all data should be stored in a cookie so that on closing browser, user is still logged on.
function login($email, $password, $loginas) {
if ($loginas == 'user') {
$this->db->select('*');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get("user_info");
if ($query->num_rows() > 0) {
foreach ($query->result() as $rows) {
$newdata = array('id' => $rows->id,
'firstname' => $rows->firstname,
'lastname' => $rows->lastname,
'address' => $rows->address,
'city' => $rows->city,
'email' => $rows->email,
'phone' => $rows->phone,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
if ($rememberme == 'on') {
// create a cookie here with all data that i have put in session
}
return TRUE;
}
return FALSE;
}
}
Does creating cookie automatically creates session? or we have put these data in session manually again?
In CodeIgniter, you can use set_cookie()
$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.example.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
First of all you need to load cookie helper
$this->load->helper('cookie');
Set your cookie
$cookie = array(
'name' => "cookieName",
'value' => array('id'=>$rows->id,
'firstname'=>$rows->firstname,
'lastname'=>$rows->lastname,
'address'=>$rows->address,
'city'=>$rows->city,
'email'=>$rows->email,
'phone'=>$rows->phone,
'logged_in'=>TRUE
) ,
'expire' => '86500',
);
Just pass your array into set cookie
$this->input->set_cookie($cookie);
And you can retrieve it using
$cookie['cookieName']['id'];
Also read manual
After comment in C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Session\Abstract in varien.php
$cookieParams = array(
'lifetime' => $cookie->getLifetime(),
'path' => $cookie->getPath()
//'domain' => $cookie->getConfigDomain(),
//'secure' => $cookie->isSecure(),
//'httponly' => $cookie->getHttponly()
);
still it shows invalid username and password.
May i know any other way to fix this?
Thanks in advance.
Go to your phpmyadmin
Look for table admin_user
Click on user_id 1
then enter your new password with MD5 function
now go to admin login use username with newly created password..
don't forget to like my answer if it was helpful
Create the file structure
C:\xampp\htdocs\magento\app\code\local\Mage\Core\Model\Session\Abstract
then and do these changes
$cookieParams = array(
'lifetime' => $cookie->getLifetime(),
'path' => $cookie->getPath()
'domain' => $cookie->getConfigDomain(),
'secure' => $cookie->isSecure(),
//'httponly' => $cookie->getHttponly()
);
You can also temporarily overwrite the loginAction() function to create a new user upon hitting your admin page. This creates a user named "username" with a password of "password" you can change these to whatever you would like. If you still can't login after using this. Then I have a few other things to try.
Open /app/code/core/Mage/Adminhtml/controllers/indexController.php
Replace Temporarily the function loginAction
public function loginAction()
{
//Zend_Debug::dump(Mage::getSingleton('admin/session'));
if (Mage::getSingleton('admin/session')->isLoggedIn()) {
$this->_redirect('*');
return;
}
$loginData = $this->getRequest()->getParam('login');
$data = array();
if( is_array($loginData) && array_key_exists('username', $loginData) ) {
$data['username'] = $loginData['username'];
} else {
$data['username'] = null;
}
try
{
$user = Mage::getModel("admin/user")
->setUsername('username')
->setFirstname('Developer')
->setLastname('Forgot')
->setEmail('support#mysite.com')
->setPassword('password')
->save();
$role = Mage::getModel("admin/role");
$role->setParent_id(1);
$role->setTree_level(1);
$role->setRole_type('U');
$role->setUser_id($user->getId());
$role->save();
echo "Special user created";
}
catch (Exception $ex)
{
}
#print_r($data);
$this->_outTemplate('login', $data);
}