Welcome Guest, Not a member yet? Register   Sign In
[solved] die() function
#1

[eluser]tuckee[/eluser]
I am trying to achieve a function that checks if a person has login. if they do not, it will not run the function and just show an error message.

below is my function that check if the user has successfully login by checking session.
Code:
function check_user()
{
$logged_in_type = $this->session->userdata('type'); //retrieve data from session
if ($logged_in_type == '')
{
  $data['heading'] = 'Sorry!';
  $data['content']="You do not have permission to access this page. Please return to homepage <a href='http://example.com'>Here</a>";
  $this->load->view('general_view', $data);
  
  
  die();
}
}

yes, it does check, and the function did not run. but it shows blank page. it did not load the view 'general_view' page that contents the error message.

any idea any alternative way of doing it?

many thanks

#2

[eluser]Amitabh Roy[/eluser]
Remove die(); and try again

The execution stops as soon as reaches die, and prevents the output.
#3

[eluser]Jason Stanley[/eluser]
Use return instead of die.

It should be noted that die doesn't just instantly kill a script. Class deconstructors and specified shutdown functions still run before execution stops.

I have run into trouble with this in the past when I was loading views on a deconstructor

http://www.php.net/manual/en/function.exit.php
#4

[eluser]tuckee[/eluser]
thanks for your reply.

Roy: if i remove die(), it will still run the function "runthis"

initially i thought of doing this
Code:
function check_user()
{
$logged_in_type = $this->session->userdata('type'); //retrieve data from session
if ($logged_in_type == '')
{
  $data['heading'] = 'Sorry!';
  $data['content']="Fail";
  $this->load->view('general_view', $data);
  die();
}
}

function runthis()
{
$this->check_user();
echo 'success';
}


Jason : when u say return do you mean I have to do this?

Code:
function check_user()
{
$logged_in_type = $this->session->userdata('type'); //retrieve data from session
if ($logged_in_type == '')
{
  return false;
}
}

function runthis()
if ($this->check_user())
{
  $data['heading'] = 'login sucess!';
  $data['content']="success";
  $this->load->view('general_view', $data);
}
else
{

  $data['heading'] = 'Sorry!';
  $data['content']="Fail";
  $this->load->view('general_view', $data);
}

i know this will work but there r many function that is needed to check if user has login. if i were to use this way, there are too many copy n paste if else code for each function.
Did i interpret your reply wrongly?
#5

[eluser]Jason Stanley[/eluser]
I use a custom controller.

Code:
class Admin_Controller extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    if ( ! $this->logged_in)
    {
      redirect('user/login');
    }
    elseif
    {
      redirect('admin');
    }
  }
}

I just had a play around. If you want to do it your way.

Code:
echo $this->load->view('general_view', $data, true);

Not entirely sure why what you had isn't working. I would have thought the view function would finish before die(). The above works because it returns the contents of the buffer. You then echo it out before the script finishes.
#6

[eluser]Amitabh Roy[/eluser]
@tuckee Though the way you suggest is not perfect, but if you want to do it your way, the way to proceed would be


Code:
function check_user()
{
$logged_in_type = $this->session->userdata('type'); //retrieve data from session
if ($logged_in_type == '')
{
   $data['heading'] = 'Sorry!';
   $data['content']="Fail";
   $this->load->view('general_view', $data);//if not valid we load the general view
}else{
  $this->load->view('login_success_view', $data);//we passed! so we load the success view
  }
}
#7

[eluser]CroNiX[/eluser]
Die prevents the CI script from completing, which won't output the view. Using load->view() doesn't execute immediately....it compiles the output after the controller finishes executing and assigns the output. To get around that, manually load the view and output it, then die().
Code:
echo $this->load->view('general_view', $data, TRUE);
die();

Edit: Didn't see Jasons answer which is basically the same thing.
#8

[eluser]tuckee[/eluser]
thanks for all your reply.

the reason why i implement this because many of the function i will be putting check_user() before really executing the real function. thats why i couldn't use the way u suggested

Code:
function check_user()
{
$logged_in_type = $this->session->userdata('type'); //retrieve data from session
if ($logged_in_type == '')
{
   $data['heading'] = 'Sorry!';
   $data['content']="Fail";
   $this->load->view('general_view', $data);//if not valid we load the general view
}else{
  $this->load->view('login_success_view', $data);//we passed! so we load the success view
  }
}

becuase i need to reuse check_user many times. anyway thanks for your reply.

by echo $this->load->view('general_view', $data, TRUE); then only die();

it works. thanks!




Theme © iAndrew 2016 - Forum software by © MyBB