Anti-refresh (F5) submit form in CodeIgniter - codeigniter

How to prevent refresh form page in CodeIgniter?
If I use redirect — all fine, but I can directly appeal to page site.com/update/success.
How can I prevent direct access to success page (only from site.com/update/) ?
Controller update.php
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
redirect('/update/success');
}
}
public function success() {
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}

You could set a token in flashdata in your index() function and then check for that token in your success() method.
class Update extends CI_Controller {
property $token;
public function __construct()
{
$this->load->library('session');
}
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
$this->session->set_flashdata('update_token', time());
redirect('/update/success');
}
}
public function success() {
// Make sure this request came from the index() method...
if( ! $this->session->flashdata('update_token'))
{
redirect();
}
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}
}

It's a time ago i used codeigniter so im not sure.
maybe you can make the succes function private and just call it in the else like this:
public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
$this->success();
}
}
private function success() {
$message = 'Your profile has been successfully updated';
$this->load->view($message);
}

public function index() {
if($this->form_validation->run() == FALSE) {
$data['error'] = 'Something wrong';
$this->load->view('update', $data);
} else {
// create success session/cookie
$this->_success()
}
}
public function _success() {
// destroy success session when called
// checks success session if existing if not the page has been refreshed redirect
$message = 'Your profile has been successfully updated';
echo $this->nocache();
$this->load->view($message);
}
public function _nocache()
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
YOu can add '_' underscore so that it is not accessible by url, and add a no cache to the headers so that the page will not stay cache on the browser when they click back or access the same url again.
code not tested

Related

codeigniter how pass login session in all controller

how to pass login session user data in my all controller pages:
i tried lot of codes but note working anything. please any one can help me.
also load session library session .it get error
Severity: Notice: Undefined variable: session_data
Filename: controllers/Banner.php
Line Number: 11
Backtrace:
File: C:\xampp\htdocs\aapkavyavahar\admin\application\controllers\Banner.php
Line: 11
Function: _error_handler
File: C:\xampp\htdocs\aapkavyavahar\admin\index.php
Line: 315
Function: require_once
My login contoller:
function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run())
{
//true
$username = $this->input->post('username');
$password = $this->input->post('password');
//model function
$this->load->model('main_model');
if($this->main_model->can_login($username, $password))
{
$session_data = array(
'username' => $username
);
$this->session->set_userdata($session_data);
redirect('login/enter');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username or Password');
redirect('login/index');
}
}
else
{
//false
$this->index();
}
}
public function enter(){
if($this->session->userdata('username') != '')
{
$this->load->view('includes/dashlink');
$this->load->view('includes/dashheader');
$this->load->view('index');
$this->load->view('includes/dashfooter');
}
else
{
redirect(base_url() . 'login/index');
}
}
public function logout() {
$this->session->sess_destroy();
$data['message_display'] = 'Successfully Logout';
$this->load->view('login', $data);
}
Other controller :
function __construct() {
parent::__construct();
$this->load->library('session');
if(!$this->session->userdata(' $session_data')){
redirect(login);
}
}
On creating the session you can give a name as below:
$this->session->set_userdata('session_name',$session_data); //give session name
Checking the session:
function __construct() {
parent::__construct();
$this->load->library('session');
if(!$this->session->userdata('session_name')){
redirect(login);
}
}
And also you can access the session data:
$data_session = $this->session->userdata('session_name');
$username = $data_session['username];

CodeIgniter Session won't destroy

I have got my login function to work which then has an array to show the session data but when I click logout it redirects me to the login page but does not clear the session data if I manually load up the members page.
Controller Function
public function logout(){
$this->session->sess_destroy();
redirect('site/login');
}
public function members(){
if ($this->session->userdata('is_logged_in')){
$data['title'] = "Members";
$this->load->view('view_header');
$this->load->view("view_members", $data);
}
else{
redirect('site/restricted');
}
}
Model
class Model_users extends CI_Model {
public function canLogin(){
$this->db->where('Username', $this->input->post('Username'));
$this->db->where('Password', md5($this->input->post('Password')));
$query = $this->db->get('user_registration');
if ($query->num_rows() == 1){
return true;
}
else{
return false;
}
}
}
Members View
<?php
echo "<pre>";
print_r ($this->session->all_userdata());
echo "</pre>";
?>
<a href='<?php echo base_url()."index.php/site/logout"?>'>Logout</a>
Not sure what I'm missing here since the logout function runs but doesn't seem to clear the session.
Try create a function in your Home controller cleaning the cache like this:
function clear_cache()
{
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
So call it from your controller's constructor
class Home extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->clear_cache();
}
}
Hope it help.

Defining and working with global object for codeigniter controller

Hello I am just working with Magento Soap clinet. And I'm creating lots function inside one controller and that's why I want to set $client and $session_id as global.
Here is my code-
<?php
// Turn off all error reporting
error_reporting(0);
//including SOAP client
require_once APPPATH.'third_party/client/soap_clinet.php';
//class Api
class Api extends CI_Controller{
//public variables
public $variable = "tree"; //working fine
//Defining $client object as following also creating error.
public $client = new SoapClient('http://localhost/mystore/index.php/api/?wsdl');
public $session_id;
public function _construct()
{
parent::_construct();
}
function index()
{
$data['title'] = "SOAP";
$data['heading'] = "Showing Magento SOAP connectiviy";
$this->load->view('apiview', $data);
}
//I need help for this function
function login(){
try{
$this->session_id = $this->client->login(
'fmniloy',
'abc123'
);
echo 'Connection complete: session id ='.$this->session_id;
}
catch (SoapFault $fault)
{
echo 'Fault Code: '.$fault->faultcode.'<br/>';
echo 'Fault Reason: '.$fault->faultstring;
}
} //login ends
function tree()
{
//it's printing global $variable successfully
echo $this->variable;
}
}
?>
Finally I got the solution.
First of all it's not possible to declare a object as public. We can pass $client object and $session_id to other functions. The solution is as following.
<?php
// Turn off all error reporting
error_reporting(0);
//including SOAP client
require_once APPPATH.'third_party/client/soap_clinet.php';
//class Api
class Api extends CI_Controller{
//public variable
public $remote_server = "http://localhost/mystore/index.php/api/?wsdl";
public $username = "fmniloy";
public $password = "abc123";
public function _construct()
{
parent::_construct();
}
function index()
{
$data['title'] = "SOAP";
$data['heading'] = "Showing Magento SOAP connectiviy";
$this->load->view('apiview', $data);
}
//Login to Server
function login(){
//login to Client Serve
$client = new SoapClient($this->remote_server);
try{
//filled with webservice username and passwd
$session_id = $client->login(
$this->username,
$this->password
);
//echo 'Connection complete: session id ='. $session_id;
}
catch (SoapFault $fault)
{
echo 'Fault Code: '.$fault->faultcode.'<br/>';
echo 'Fault Reason: '.$fault->faultstring;
}
//inputting $client object and $session_id to an array
$soap_vars = array($client, $session_id);
//return values to use in other functions
return $soap_vars;
}
//login ends
function tree()
{
$soap_vars = $this->login();
$result = $soap_vars[0]->call($soap_vars[1], 'catalog_category.tree');
echo '</br></br></br>Catagory Tree: ';
var_dump($result);
}
}
?>

Code Igniter - Unable to load requested file after redirect

I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result

codeigniter: call Index Method from another method

I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();

Resources