Hi, I'm new to CakePHP.  I'm using WinXP, Apache 2.2, PHP 5 and
CakePHP 1.2.

I run into this problem about session. I can't retrieve the session
when I redirect the user to another page like "/articles" after login
except if it's on the same controller 'UsersController'.

class UsersController extends AppController {
        var $name = "Users";
        var $helpers = array("Html", "Form", "Countrylist");

        function register() {
                $this->set('password_verification_error','');

                if (!isset($this->user) || !$this->user) {
                        if(!empty($this->data)) {

                                $this->User->data = $this->data;

                                if ($this->User->validates()) {
                                        if($this->data['User']['password'] != 
$this->data['User']
['password2']) {
                                                
$this->set('password_verification_error','Please verify your
password again.<br>The passwords specified do not match.');
                                        } else {
                                                $this->data['User']['password'] 
= md5($this->data['User']
['password']);
                                                
$this->data['User']['last_login'] = date("Y-m-d H:i:s");
                                                
$this->data['User']['join_date'] = date("Y-m-d H:i:s");

                                                $this->cleanUpFields();
                                                $this->User->create();

                                                
if($this->User->save($this->data)) {
                                                        
$this->Session->write('user', $this->data['User']['email']);
                                                        
$this->redirect('/users/index');
                                                } else {
                                                        
$this->flash("problem.");
                                                }
                                        }
                                } else {
                                        $this->validateErrors($this->User);
                                }
                        }
                }  else {
                        $this->autoRender = false;
                        $this->redirect("/");
                }
        }

        function index() {
                $this->checkSession();
        }

        function login() {
                $this->pageTitle = "iMallow - Author Login";

                $this->set('error', '');
                if ($this->data) {
                        $results = 
$this->User->findByEmail($this->data['User']['email']);
                        if ($results && $results['User']['password'] == 
md5($this-
>data['User']['password'])) {
                                $_SESSION['user'] = 
$this->data['User']['email'];
                                $this->Session->write('user', 
$this->data['User']['email']);
                                $this->Session->write('last_login', 
$results['User']
['last_login']);

                                $results['User']['last_login'] = date("Y-m-d 
H:i:s");
                                $this->User->save($results);

                                $this->redirect('/articles');
                        } else {
                                $this->set('error', 'Invalid login 
credential.');
                        }
                }
        }

        function logout() {
                $this->Session->delete('user');
                $this->redirect('/users/login');
        }
}


My ArticlesController looks like this:


class ArticlesController extends AppController {
        var $name = "Articles";

        function index() {
                $this->Session->renew();
        }
}


I'm trying to get the session on my layout view which looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title><?php echo $title_for_layout?></title>
<?php echo $html->meta('favicon.ico','/
favicon.ico',array('type'=>'icon')); ?>
<?php echo $html->charset('utf-8'); ?>
<?php echo $html->css('main','import'); ?>
</head>
<body>
<center>
<div id="maincontainer">
        <div id="header">
                <div id="logoarea"><?php echo 
$html->link($html->image("logo.gif",
array("alt"=>"iMallow Articles",
"boder"=>"0")),"/",false,false,false) ?></div>
                <div id="rinfo">
                        <?php
                                if($session->read('user')) {
                                        ?>
                                        Hi, <b><?php echo 
$session->read('user') ?>!</b> <br />
                                        <?php echo $html->link('My Account', 
'/users/profile') ?>, <?php
echo $html->link('logout', '/users/logout') ?>
                                        <?
                                } else {
                                        ?>
                                        Welcome <b>Guest!</b> <br />
                                        <?php echo $html->link("Sign 
In","/users/login") ?> , <?php echo
$html->link("Register Here","/users/register") ?>
                                        <?
                                }
                        ?>
                </div>
                <div class="cb"></div>
        </div>
        <div id="mainbody">
                <?php echo $content_for_layout ?>
        </div>
        <div id="footer">
                <a href="#">Submit An Article</a> | <a href="#">Find 
Articles</a> |
<a href="#">Terms of Use</a> | <a href="#">Privacy Policy</a> | <a
href="#">Contact Us</a><br>
                Copyright &copy; 2007. All rights reserved.
        </div>
</div>
</center>
</body>
</html>


If I'm on the /users/index page...It is working but if on another page
like for example /articles, session information was gone. I know I am
missing something here.


Thanks to all for the help!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to