An idea about login script-Collection of common programming errors
At the moment I think about login script. Idea – when you submit login and password, they checks into model and if login and password is correct, script shows a message about completed login process. But if I put in view echo $message;
and I am at login page, kohana shows error – Undefined variable $message. But when I am at page, where the variable defined, all working. How to realize this idea.
EDIT!
class Controller_About extends Controller_Template {
public function action_index() {
session_start();
if (isset($_SESSION['lietotajvards'])) {
if (!empty($_POST['virsraksts']) and !empty($_POST['saturs'])) {
$name = Model::factory('index')->insert_names($_POST['virsraksts'], $_POST['saturs']);
$result = $name;
$this->template->site_name = Kohana::$config->load('common')->get('site_name');
$this->template->site_description = Kohana::$config->load('common')->get('site_description');
$this->template->page_title = 'About';
$this->template->content = View::factory('about/about')->set('result', $result);
$this->template->styles[] = 'index/index';
} else {
$this->template->site_name = Kohana::$config->load('common')->get('site_name');
$this->template->site_description = Kohana::$config->load('common')->get('site_description');
$this->template->page_title = 'About';
$this->template->content = View::factory('about/about');
$this->template->styles[] = 'index/index';
}
} else {
if (!empty($_POST['lietotajvards']) and !empty($_POST['parole'])) {
$user_model = Model::factory('index')->valide($_POST['lietotajvards'], md5($_POST['parole']));
foreach ($user_model as $d) {
if ($_POST['lietotajvards'] == $d['lietotajvards'] and md5($_POST['parole']) == $d['parole']) {
$_SESSION['lietotajvards'] = $_POST['lietotajvards'];
$this->template->content = View::factory('login/index')->set('message', 'Ielogosanas veiksmiga!');
echo '';
}
}
}
$this->template->site_name = Kohana::$config->load('common')->get('site_name');
$this->template->site_description = Kohana::$config->load('common')->get('site_description');
$this->template->page_title = 'About';
$this->template->content = View::factory('login/index');
$this->template->styles[] = 'index/index';
}
}}
I have problem only with variable $message.
-
The problem is you’re setting the template content at the very end, which is overriding what you’ve previously set it to. Try changing
$this->template->content = View::factory('login/index')->set('message', 'Ielogosanas veiksmiga!');
to
$message = 'Ielogosanas veiksmiga!';
and then at the end change the view loading to
$this->template->content = View::factory('login/index')->bind('message',$message);
Using the bind method on the view will pass a null value to the view if the variable has not been set inside your controller.
Originally posted 2013-11-27 12:26:55.