in Magento controller, can't get session variable which was set before redirect on this action - session

in rewrited customer controller is set:
if(!empty($customer_data)){
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// or $session = $this->_getSession();
$session->setEmail($email);
// or $email = $session->setData('email', $email);
Mage::getSingleton(‘core/session’)->setMySessionVariable($email);
$this->_redirectSuccess($this->_getUrl('*/*/customerChoice/'));
...
}
then, on other customer ontroller's action, in which i redirect from the code above, i do:
public function customerChoiceAction()
{
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// or $session = $this->_getSession();
$email = $session->getEmail();
// or $email = $session->getData('email');
but i get null in result, where to dig and hove to solve? thanks!

This was an issue in CHROME only, in Firefox, everything works fine.

Related

PHP Codeigniter 3.1.10: Unable to update session data

Happen to be stuck since almost a day. Scoured in google searching as to why this isn't working as expected, and also couple of answered questions in stackoverflow, but couldn't figure out as to why it's not working.
Basically i am setting session data during login, like
foreach($response as $item) {
$sess_array = array(
'user_id' => $item->id,
'photo' => $item->user_pic,
);
}
// Create session
$this->session->set_userdata('logged_in', $sess_array);
And now I am attempting to update a particular variable named 'photo'.
$this->session->set_userdata('photo', $new_name);
And when I try to display the value of the session variable 'photo' in my view, it still shows the old value and not the updated value.
Below are the entries from config.php
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = BASEPATH . 'cache/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
Codeigniter version 3.1.10
OS Windows 10
Please help.
First of all just check in you autoload.php did you load the session library or not.
or else load session library in your controller. $this->load->library('session');
foreach($response as $item) { // looping your obj $response
$sess_array = array( // set an array
'user_id' => $item->id,
'photo' => $item->user_pic,
);
}
print_r($sess_array); you will get the last element of you $response obj here.
// Create session
$this->session->set_userdata('logged_in', $sess_array); // here you are setting the last obj of $response in you session logged_in
`$this->session->set_userdata('photo', $new_name);` // here you store an variable $new_name in session `photo`
redirect from your controller current function to new function after setting the session.
for example.
class session extend CI_Controller{
function set_session(){
$new_name = 'xyz';
$this->session->set_userdata('photo', $new_name);
return redirect('session/get_session');
}
function get_session(){
$this->load->view('sample');
}
}
In your view sample.php
<body><h1>
<?php
echo $this->session->userdata('photo');
// here you got out put 'xyz'
?>
</h1></body>
Hope you can find the issue where you go wrong.

redirect uri segment after delete

I created a project , I have a question about the direct url segment ...
For example:
this my URL
http://10.88.25.131/instrumentdev/instrument/instrument/detail/CT-BSC-001
when I want delete the data and to direct to the url such as the example above
this my controllers
public function detail(){
$id = $this->uri->segment(4);
$data = array(
'detail'=>$this->mcrud->get_detail($id),
'lihat' =>$this->mcrud->lihat_komentar($id),
'isi' =>'instrument/read_views');
$this->load->view('layout/wrapper', $data);
}
function deletecom() {
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect('???**this my problem**???');
}
Use session
$newdata = array(
'data' => $u
);
$this->session->set_userdata($newdata);
redirect('controller/method');
then in next method
$u = $this->session->userdata('data');
Use
$this->load->helper('url');/*if not already*/
redirect(base_url('controller/method/parametersIfAny'));
TRY this :
function deletecom() {
echo $this->uri->segment(4);die(); //what does this print ??
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect(base_url('instrument/instrument/detail/'.$u));
}

How to retain Magento customer session throughout all pages?

I am creating customer login using custom php code. I am using API calls for creating the frontend, so I need to maintain the logged in session values throughtout all the pages once the customer has logged in.
Code in a.php
require_once "../magento/app/Mage.php";
Mage::app('default');
umask(0);
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->start();
if (!empty($email) && !empty($password )) {
try {
$a = $session->login($email, $password );
$session->setCustomerAsLoggedIn( $session->getCustomer() );
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
}
}
I am able to log in using this code but I need to maintain this logged in session throughout all the pages.
I tried to get the logged in user information from other page. But I can't.
Code in b.php
require_once "../magento/app/Mage.php";
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$session->start();
var_dump(Mage::getSingleton('customer/session')->isLoggedIn());
if($session->isLoggedIn()) {
echo 'User logged In';
} else {
echo 'User not logged In';
}
After calling the page a.php and when I call b.php in the same browser, I need the customer information of the user logged in using a.php? Is that possible? Can anyone help me in this?
Try this one
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
echo $customerData->getId();
}
<?php
require_once "../magento/app/Mage.php";
Mage::app('default');
umask(0);
Mage::getSingleton('core/session', array('name' => 'frontend'));
var_dump(Mage::getSingleton('customer/session')->isLoggedIn());
// print true
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
echo $customerData->getId();
// return customer's id
}

Magento customer login from outside magento

I am trying to login as a customer.
I am requesting the Following code (AJAX) and this outside magento store.
<?php
require_once "../app/Mage.php";
umask(0);
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$user = $_POST['user'];
$password = $_POST['password'];
$session = Mage::getSingleton('customer/session');
$flag = 0;
$resultArr = array();
function customerLogin($user,$password){
try{
$session = Mage::getSingleton('customer/session');
$result = $session->login($user,$password);
$customer = $session->getCustomer();
$session->setCustomerAsLoggedIn($customer);
$resultArr['flag'] = 1;
$resultArr['msg'] ='Logged in as '.$customer->getName();
$jsonReturn = json_encode($resultArr);
return $jsonReturn;
}catch(Exception $e){
$resultArr['flag'] = 0;
$resultArr['msg'] = $e->getMessage();
$jsonReturn = json_encode($resultArr);
return $jsonReturn;
}
}
echo customerLogin($user,$password);
?>
I found the above code is Creating the session files at var/session directory successfully but unable to write customer entry in log_customer DB table . Anybody know whats the problem here.
Thanks :)
UPDATED
Okie the following updated code(customerLogin.php) is working under a condition
<?php
function customerLogin($user,$password){
require_once "./app/Mage.php";
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$user = $_POST['user'];
$password = $_POST['password'];
$flag = 0;
$resultArr = array();
$session = Mage::getSingleton('customer/session');
try{
$result = $session->login($user,$password);
//$customer = $session->getCustomer();
$session->setCustomerAsLoggedIn($session->getCustomer());
$resultArr['flag'] = 1;
$resultArr['msg'] ='Logged in as '.$session->getCustomer()->getName();
$cusArr = array(
'isLoggedIn' => true,
'name' => $session->getCustomer()->getName(),
'id' => $session->getId(),
'email' =>$session->getCustomer()->getEmail(),
);
$resultArr['customerData'] = $cusArr;
$jsonReturn = json_encode($resultArr);
return $jsonReturn;
}catch(Exception $e){
$resultArr['flag'] = 0;
$resultArr['msg'] = $e->getMessage();
$jsonReturn = json_encode($resultArr);
return $jsonReturn;
}
}
echo customerLogin($user,$password);
?>
if i follow the directory structure like:
|-- app
|-- **customerLogin.php**
|-- downloader
|-- install.php
|-- js
|-- lib
|-- media
|-- var
But its not working if I place code one directory level down like :
|-- app
|-- **customerLogin**
`--**customerLogin.php**
|-- downloader
|-- install.php
|-- js
|-- lib
|-- media
|-- var
Anybody knows whats the problem.
I am wondering why magento is unable to write session entry in log_customer table when i place my code one level down.
Get your customer id then use the below code.
Mage::getSingleton('customer/session')->loginById($customerId);
try this code to login from outside of magento, create demo.php file in magento root and put following code.
include('app/Mage.php');
Mage::app();
if($_POST && $_POST["email"] && $_POST["password"])
{
$email=$_POST["email"];
$password=$_POST["password"];
$session = Mage::getSingleton('customer/session');
try {
$log = $session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
$customer_id=$session->getCustomerId();
$send_data["success"]=true;
$send_data["message"]="Login Success";
$send_data["customer_id"]=$customer_id;
}
catch (Exception $ex) {
$send_data["success"]=false;
$send_data["message"]=$ex->getMessage();
}
}
else
{
$send_data["success"]=false;
$send_data["message"]="Enter both Email and Password";
}
echo json_encode($send_data);
store id and website id here set custom
you will change accordance to u
<?php
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require_once "app/Mage.php";
umask(0);
Mage::app();
$firstname = "krishana";
$lastname = "singh";
$email = "krish.bhati#gmail.com";
$password = "12345678";
// Website and Store details
$websiteId = Mage::app()->getWebsite()->getId('1');
$store = Mage::app()->getStore('31');
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';
$app = Mage::app ( $mageRunCode, $mageRunType );
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$session->start();
$customer->loadByEmail($email);
$customer_id= $customer->getId();
if($customer_id){
Mage::getSingleton('customer/session')->loginById($customer_id);
Mage_Core_Model_Session_Abstract_Varien::start();
try{
$session->login($email, $password);
}
catch(Exception $e) {}
$session->setCustomerAsLoggedIn($session->getCustomer());
echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
}
?>
Easily open your account & forgot password for your Magento without loading any pages through GSD Ajax login. Below URL is very useful.
http://www.magentocommerce.com/magento-connect/easy-ajax-login-by-smartshore.html
use below code in magento controller it will work inside controller because custom PHP file can not generate magento visitor log
public function customerLoginSession($customer_id){
/** #var $session Mage_Customer_Model_Session */
$session = Mage::getSingleton( 'customer/session' );
Mage::app()->getStore()->setWebsiteId(1);
try
{
//$session->login( $user, $pass);
$session->loginById($customer_id);
$customer = $session->getCustomer();
$session->setCustomerAsLoggedIn($customer);
$res = json_encode(array('status' => 'valid', 'userData' => $customer->getId()));
}
catch( Exception $e )
{
$res = json_encode(array('status' => 'invalid', 'userData' => $e->getMessage()));
}
return $res;
}

Checking for Magento login on external page

I'm hitting a wall here while trying to access items from Magento on an external page (same server, same domain, etc, etc). I want to see if the user is logged into Magento before showing them certain parts on the site.
Keep in mind that this code exists outside of Magento.
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
if (empty($session))
{
$session = Mage::getSingleton("customer/session");
}
if($session->isLoggedIn())
echo "hi";
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo $cart;
$cart returns 0, where I definitely have products in my cart. isLoggedIn() also returns false. What am I doing wrong here? Is there an option in Magento that I need to turn on or off to be able to access this information outside of Magento?
Using the code above, I created a php file in the Magento folder. From there, added the number of items in the cart and whether you were logged in or not to an array and encoded it as json. I used some jquery on my external page to grab the file and pull the data I needed.
Not quite the ideal situation, but it works for now.
Are you including Mage.php (which defines getSingleton)?
require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
What does $session have in it after the getSingleton() call?
print_r ($session);
EDIT: I tried this on my end and was not able to get accurate isLoggedIn() or getItemsCount() data. When I dumped out $session its showing all the fields as 'protected.'
I'm not interested in requiring the user to log in...I merely want to access data from the already logged in session.
Anyone else have any thoughts on this? All the examples out there seem to be pre 1.4.x.
require_once "app/Mage.php";
umask(0);
Mage::app();
// require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";
// Customer Information
$firstname = "krishana";
$lastname = "singh";
$email = "krish.bhati#gmail.com";
$password = "myverysecretpassword";
// Website and Store details
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';
$app = Mage::app ( $mageRunCode, $mageRunType );
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$session->start();
$customer->loadByEmail($email);
$customer_id= $customer->getId();
if($customer_id){
Mage_Core_Model_Session_Abstract_Varien::start();
$session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
}

Resources