rajesh.bakade
rajesh.bakade

Reputation: 19

How to save values in session?

I am working with cakephp.Recently I am facing problem in saving data in session. I have created login page which will send value to controller/action. it will receives like this.

function ajaxCall() {
            $this->autoRender = false;
            $this->layout = 'ajax';
            $arrData = $this->params['url'];

            if(!empty($arrData)){
                if($arrData['submit']=='Y'){
                    $userObj = new Api(); // create an instance of the user class
                    $userInfo = $userObj->login($arrData['email'],$arrData['password']); // call the api login user methods
                    $xml = simplexml_load_string($userInfo);
                    $userId = $xml->message->id;
                    if($userId != "0" && $userId != ""){
                        $this->setCurrentUserId($userId);
                        echo "success";
                    }
                    else{
                        echo "no"; 

                    }
                }  
            }
        }

public function setCurrentUserId($userId)
        {

            //Is session alive 
            //if not then redirect to session time out page
            //session_start();
            //session_register("");
            if($userId == 419 || $userId == 423){
                $userId1 = $this->Session->write('userId', $userId);
                }else{
                $userId1 = $this->Session->write('userId', $userId);
            }

            return $userId1;
        }

my controller contain also these line to include helpers,component

    public $components = array('Session');
    public $helpers = array('Html','Session');

and in core.php file i set session as-

Configure::write('Session', array(
    'defaults' => 'php', 'ini' => array('session.auto_start' => 1)
));

Please help me as i am unable to save userId in session

Thanks

Upvotes: 1

Views: 197

Answers (1)

shadyyx
shadyyx

Reputation: 16065

On the internet there You can find CakePHP cookbook to create simple application with authentication and authorization: book.cakephp.org

Here You can find very simple example on how to create UsersController, User model and Views for login etc with login action using CakePHP's inbuilt Auth object - there is no need to write the whole login logic - Auth object will do most for You.

Hope You'll enjoy it!

Upvotes: 1

Related Questions