What is the best way to set flash data messages when session have timed out and then be able to get message on login page.
I have a warning variable that would like to use but can not seem to get it working with sessions timed out I have it redirecting to the login page one sessions have timed out.
But not sure best way to set flash data using my error array, any idea's.
Login Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MX_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('user');
$this->load->library('form_validation');
$this->lang->load('common/login', 'english');
}
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (null !==($this->session->flashdata('message_name'), $this->error)) {
$data['message'] = $this->session->set_flashdata('message_name', 'This is my message');
} else {
$data['message'] = '';
}
$this->load->view('common/login', $data);
} else{
if($this->validate()) {
redirect('dashboard');
} else {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (null !==($this->session->flashdata('message_name'), $this->error)) {
$data['message'] = $this->session->set_flashdata('message_name', 'This is my message');
} else {
$data['message'] = '';
}
$this->load->view('common/login', $data);
}
}
}
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->error['warning'] = $this->lang->line('error_login');
return !$this->error;
}
}
}
Login View
<?php echo modules::run('common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title"><i class="fa fa-key"></i> <?php echo $text_heading; ?></h2></div>
<div class="panel-body">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<?php echo form_open('login');?>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" size="50" />
</div>
<?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="password" value="" placeholder="Password" class="form-control"/>
</div>
<?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
</div>
</div>
</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>
set_flshdat is codeigniter function that will only be available for next server request and then automatically cleared. you can see in detail here flashdata and search for set_flashdata in this link
so if you need message after redirect to other page or same page
set flashdata on first requested controller page
$this->session->set_flashdata('message_name', 'This is my message');
and on second server request or your redirection page will get this flash data using below
echo $this->session->flashdata('message_name');
So, basically if you don't want to redirect page and want to load view
//set data in controller
$data['message_name'] = 'This is my message'
//and pass this to view
$this->load->view('yourview_name', $data);
And in your view page just write
echo $message_name;
Related
When I login admin panel locally it redirects and load login page again, not open dashboard of my admin panel but when I am moving my file to live server admin panel works perfectly.
My Login page Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="login-box-body" class="text-center" style="margin:0 auto;padding:30px;background:#f0f0f0;border-radius:10px 10px 0 0;">
<div style="margin:0 auto;" class="text-center">
<img src="<?php echo base_url('upload/images/logo.png'); ?>" style="width:300px;" class="text-center img-responsive;" title="<?php echo $title_lg; ?>" />
</div>
</div>
<div class="login-box-body">
<p class="login-box-msg"><?php echo lang('auth_sign_session'); ?></p>
<div class="text-danger"><?php echo $message;?></div>
<?php echo form_open('auth/login');?>
<div class="form-group has-feedback">
<?php echo form_input($identity);?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php echo form_input($password);?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<!-- <label>
<?php /*?> <?php echo form_checkbox('remember', '1', FALSE, 'id="remember"'); ?><?php echo lang('auth_remember_me'); ?><?php */?>
</label>-->
</div>
</div>
<div class="col-xs-4">
<?php /*?><?php echo form_submit('submit', lang('auth_login'), array('class' => 'btn btn-primary btn-block btn-flat'));?><?php */?>
<input type="submit" name="submit" value="Login" class="btn btn-primary btn-block btn-flat" style="background-color: #2f76bb;border-color: #2f76bb;">
</div>
</div>
<?php echo form_close();?>
above code is my login page code
My Authentication Code
class Auth extends MY_Controller {
function __construct()
{
parent::__construct();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
$this->lang->load('auth');
}
function index()
{
if ( ! $this->ion_auth->logged_in())
{
redirect('auth/login', 'refresh');
}
else
{
redirect('/', 'refresh');
}
}
function login()
{
if ( ! $this->ion_auth->logged_in())
{
/* Load */
$this->load->config('admin/dp_config');
$this->load->config('common/dp_config');
/* Valid form */
$this->form_validation->set_rules('identity', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
/* Data */
$this->data['title'] = $this->config->item('title');
$this->data['title_lg'] = $this->config->item('title_lg');
$this->data['auth_social_network'] = $this->config->item('auth_social_network');
$this->data['forgot_password'] = $this->config->item('forgot_password');
$this->data['new_membership'] = $this->config->item('new_membership');
if ( $this->form_validation->run() == TRUE)
{
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
{
if ( ! $this->ion_auth->is_admin())
{
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/', 'refresh');
}
else
{
/* Data */
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
/* Load Template */
redirect('admin', 'refresh');
}
}
else
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login', 'refresh');
}
}
else
{
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'email',
'value' => $this->form_validation->set_value('identity'),
'class' => 'form-control',
'placeholder' => lang('auth_your_email')
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'class' => 'form-control',
'placeholder' => lang('auth_your_password')
);
if($this->session->flashdata('message'))
{
$this->data['forgot_password'] = TRUE;
}
/* Load Template */
$this->template->auth_render('auth/login', $this->data);
}
}
else
{
redirect('/', 'refresh');
}
}
This is my Authentication Code.
Thanks For advance
Please ensure that the following are updated:
The PHP version (on the local server)
The Codeigniter version
The Ion_Auth library
Hope it helps :)
Currently trying to figure out how to update existing SQL data in cake php. I have my input fields being automatically filled with preexisting data but whenever I hit save to change existing data it does nothing. NO error or warning message.
Here is my Controller:
public function settings()
{
$user = $this->UserAuth->getUser();
$userid = $this->UserAuth->getUserId();
//$userEntity = $this->Push->find()->where(['user_id = ' => $userid])->order('time')->toArray($this->request->data, ['validate'=>false]);
//$query = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
$userEntity = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
if($this->request->is('patch', 'post', 'put')) {
$errors = $userEntity->errors();
if($this->request->is('ajax')) {
if(empty($errors)) {
$response = ['error'=>0, 'message'=>'success'];
} else {
$response = ['error'=>1, 'message'=>'failure'];
$response['data']['push'] = $errors;
}
echo json_encode($response);exit;
} else {
if(empty($errors)) {
if(!empty($this->request->data($userEntity['iosapikey']))) {
$userEntity['iosapikey'] = $this->request->data['Push']['iosapikey'];
}
if(!empty($this->request->data['Push']['androidapikey'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['androidapikey'];
}
if(!empty($this->request->data['Push']['restapikey'])) {
$userEntity['restapikey'] = $this->request->data['Push']['restapikey'];
}
if(!empty($this->request->data['Push']['segments'])) {
$userEntity['iosapikey'] = $this->request->data['Push']['segments'];
}
if(!empty($this->request->data['Push']['tags'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['tags'];
}
if(!empty($this->request->data['Push']['deeplinks'])) {
$userEntity['restapikey'] = $this->request->data['Push']['deeplinks'];
}
if($this->Push->save($userEntity, ['validate'=>false])) {
$this->Flash->success(__('Push settings have been updated successfully'));
//$this->redirect(['action'=>'index', 'page'=>$page]);
} else {
$this->Flash->error(__('Unable to update push settings, please try again'));
}
}
}
} else {
if(!empty($userEntity['iosapikey'])) {
$userEntity['iosapikey'] = $userEntity['iosapikey'];
}
if(!empty($userEntity['androidapikey'])) {
$userEntity['androidapikey'] = $userEntity['androidapikey'];
}
if(!empty($userEntity['restapikey'])) {
$userEntity['restapikey'] = $userEntity['restapikey'];
}
if(!empty($userEntity['segments'])) {
$userEntity['segments'] = $userEntity['segments'];
}
if(!empty($userEntity['tags'])) {
$userEntity['tags'] = $userEntity['tags'];
}
if(!empty($userEntity['deeplinks'])) {
$userEntity['deeplinks'] = $userEntity['deeplinks'];
}
}
$this->set(compact('userEntity'));
$this->set('_serialize', ['push']);
}
This is my template:
<div class="panel-body">
<?php echo $this->element('Usermgmt.ajax_validation', ['formId'=>'editPushForm', 'submitButtonId'=>'editPushSubmitBtn']); ?>
<?php echo $this->Form->create($userEntity, ['id'=>'editPushForm', 'class'=>'form-horizontal', 'type'=>'text']);?>
<div class="form-group col-md-9 col-sm-10">
<label>iOS API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.iosapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Android API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.androidapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>REST API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.restapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Segments:</label>
<div class="">
<?php echo $this->Form->input('Push.segments',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Tags:</label>
<div class="">
<?php echo $this->Form->input('Push.tags',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Deeplinks:</label>
<div class="">
<?php echo $this->Form->input('Push.deeplinks',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-sm-offset-2 col-sm-3">
<?php echo $this->Form->Submit(__('Save'), ['div'=>false, 'class'=>'btn btn-primary pushbutton', 'id'=>'editPushSubmitBtn']); ?>
</div>
</div>
<?php echo $this->Form->end();?>
I'm sure I have a small error somewhere I just haven't spotted yet, but having put many hours into this with no positive result I thought it was time to find some help.
Cake PHP find function returns an object so i would request you to use a statement like $userEntity->iosapikey = $this->request->data['Push']['iosapikey']; to assign values and also enable debuging in cakephp https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#debugging-queries-and-resultsets
I am Creating a Blogging Application with Codeigniter. So therefor i have created admin panel for admin. But on my new article controller i cant display form errors. Here is my code.
Add Article and Store Article Controllers
public function add_article()
{
$this->load->model('dashboardmodel');
$username = $this->dashboardmodel->get_username();
$this->load->helper('form');
$this->load->view('admin/add_article', ['user'=>$username]);
}
public function store_article()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="alert alert-dismissible alert-danger">', '</div>');
$this->form_validation->set_rules('title', 'Post Title', 'required|trim|alpha');
$this->form_validation->set_rules('body', 'Post Content', 'required');
if ($this->form_validation->run()) {
$title = $this->input->post('title');
$body = $this->input->post('body');
echo 'Successful';
} else {
return redirect('admin/add_article');
}
}
Add Article View
<?php
include_once('admin_header.php');
?>
<div class="container">
<fieldset>
<legend>New Post</legend>
//Here ERRORS SHOULD BE DISPLAYED
<?php echo validation_errors(); ?>
<?php echo form_open('admin/store_article', ['class'=>'form-horizontal']);
if ($error = $this->session->flashdata('login_failed')) : ?>
<div class="alert alert-dismissible alert-danger">
<?= $error ?>
</div>
<?php endif; ?>
<div class="form-group">
<div class="col-lg-10">
<?php echo form_input(['name'=>'title', 'class'=>'form-control', 'placeholder'=>'Post Title', 'value'=>set_value('title')]); ?>
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<?php echo form_textarea(['name'=>'body', 'class'=>'form-control', 'placeholder'=>'Post Content']); ?>
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<?php echo form_reset(['name'=>'reset', 'value'=>'Reset', 'class'=>'btn btn-default']),
form_Submit(['type'=>'submit', 'value'=>'Publish', 'class'=>'btn btn-primary']); ?>
</div>
</div>
</fieldset>
</form>
</div>
<?php
include_once('admin_footer.php');
?>
Dont What's Going On but i am Stuck in it.
Your problem is that when the form submission contains errors, you redirect to the page again, which refreshes the page and all errors are gone. What you need to do is replace your return redirect('admin/add_article'); line with actually loading the same view again, which would contain the errors this time. And since you load other things for the view, you'll have to replace it with this whole code (which is the same as the other method so maybe you should give the structure of this code more thought):
$this->load->model('dashboardmodel');
$username = $this->dashboardmodel->get_username();
$this->load->helper('form');
$this->load->view('admin/add_article', ['user'=>$username]);
i am trying to change the login button on my page to a logout button when someone is logged in.
$logged_in = $this->session->userdata('logged_in');
if ($logged_in) {
$data['Lbutton'] = '<div id="login">
<button class="login3"><?php echo anchor("pages/logout", "Logout"); ?></button>
</div>';
} else {
$data['Lbutton'] = '<div id="login">
<button class="login3"><a href= <?php echo base_url()?>index.php/login>login</a> </button>
</div>';
}
and this is my view:
<?php echo $Lbutton ?>
This is Answer:
if ($logged_in)
{
$data['Lbutton'] = '<div id="login">
<button class="login3">'.anchor("pages/logout", "Logout").'</button>
</div>';
}
else
{
$data['Lbutton'] = '<div id="login">
<button class="login3"><a href="'.base_url().'index.php/login" >login</a> </button>
</div>';
}
I am developing a site with CI 2.1.3
I have a blog module, in this blog I have a form to post a comment.
I am calling this form inside a view with:
echo Modules:: run('blog/comment');
When I submit this form with ajaxForm, the values of the input fields are not being cleared.
My controller’s function for the comment form:
public function comment($postId)
{
$this->load->helper('form');
$this->data['success'] = FALSE;
$this->data['postId'] = $postId;
if(!isset($_POST['comment_submit']))
{
$this->data['new_comment'] = $this->blog_comment_m->get_new();
}
else
{
$this->data['new_comment'] = $this->blog_comment_m->object_from_post(array('author', 'authur_email', 'content'));
$this->load->library('form_validation');
$rules = $this->blog_comment_m->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE)
{
$this->data['success'] = TRUE;
$this->data['new_comment'] = $this->blog_comment_m->get_new();
}
}
$this->load->view('add_comment', $this->data);
}
The comment form:
<div id="commentAjax">
<?php $attr = array('id'=>'commentForm'); echo form_open(site_url('blog/comment/' .
$postId), $attr); ?>
<input type="hidden" name="post_id" value="<?php echo $postId; ?>" />
<div style="border-top:2px groove #930"><h4>Leave a Comment</h4></div>
<div class="control-group <?php if(form_error('author')) echo 'error'; ?>">
<label>Name *</label>
<?php echo form_input(array('name'=>'author', 'class'=>'input-large', 'value'=>set_value('author', $new_comment->author))); ?>
<span class="help-block"><?php echo form_error('author'); ?></span>
</div>
<div class="control-group <?php if(form_error('author_email')) echo 'error'; ?>">
<label>Email *</label>
<?php echo form_input(array('name'=>'author_email', 'class'=>'input-large', 'value'=>set_value('author_email', $new_comment->author_email))); ?>
<span class="help-block"><?php echo form_error('author_email'); ?></span>
</div>
<div class="control-group <?php if(form_error('content')) echo 'error'; ?>">
<label>Comment *</label>
<?php echo form_textarea(array('name'=>'content', 'value'=>set_value('content', $new_comment->content))); ?>
<span class="help-block"><?php echo form_error('content'); ?></span>
</div>
<div>
<?php echo form_submit('submit', 'Send Comment', 'class="btn btn-submit"');?>
<input type="hidden" name="comment_submit" value="1" />
</div>
<?php echo form_close(); ?>
<?php if($success): ?>
<div style="border:1px solid #666; background:#9F9; color:#000; margin-top:10px; width:50%; padding:5px; font-weight:bold">
<p>Thank you for your comment.</p>
<p>To avoid spam, your comment has been submitted for approval.</p>
<p><h2 class="highland">Highland Coffee Roastery</h2></p>
</div>
<?php endif; ?>
</div>
<script>
$(function()
{
var options = { target: '#commentAjax' };
$('#commentForm').ajaxForm(options);
});
</script>
I dumped the $new_comment array and the fields values are empty.
I checked the page source and the input fields values = ''.
Yet, I still see the values that I submitted in the input fields.
Refreshing the page, still, displays the values.
What is wrong?
I got the answer on the Daniweb forum:
Ok, the problem is that set_value() doesn’t consider if the validation runs true or false, usually you redirect() and so the POST array is resetted automatically, here we can force this action by extending /system/libraries/Form_validation.php, create /application/libraries/MY_Form_validation.php and paste this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function resetpostdata()
{
$obj =& _get_validation_object();
foreach($obj->_field_data as $key)
{
$this->_field_data[$key['field']]['postdata'] = NULL;
}
return true;
}
}
?>
After the validation runs true, call the method, as in this example:
if($this->form_validation->run() === FALSE)
{
# . . .
}
else
{
$this->form_validation->resetpostdata();
# load view & other stuff
}