Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - displaying success or unsuccess message

Im wondering if someone could let me know how they handle success / unsuccess messages in CodeIgniter.

For example, what im doing at the moment when a user signs up to my site, this is what happens in the controller

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    // Redirect to the successful controller
    redirect( 'create-profile/successful' );
} else {
    // Redirect to the unsuccessful controller
    redirect( 'create-profile/unsuccessful' );
}

Then in the same controller (create-profile), i have 2 methods which are like the following

function successful()
{
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

The problem with this is that i can simply go to site.com/create-profile/successful and it will show the page.

If someone could possibly show me a better way of handling this, it would be greatly appreciated.

Cheers,

like image 285
BigJobbies Avatar asked Dec 13 '22 07:12

BigJobbies


2 Answers

You could set a flashdata before the redirection:

$this->session->set_flashdata('create_profile_successful', $some_data);
redirect( 'create-profile/successful' );

 

function successful(){
    if( FALSE == ($data = $this->session->flashdata('create_profile_successful'))){
        redirect('/');  
    }

    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}
like image 89
duxins Avatar answered Dec 14 '22 21:12

duxins


is there a reason you don't use this:

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
} else {
    $data['h1title'] = 'Unsuccessful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

greets, stefan

like image 44
Stefan Koenen Avatar answered Dec 14 '22 20:12

Stefan Koenen