![]() |
[solved] die() function - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: [solved] die() function (/showthread.php?tid=48506) |
[solved] die() function - El Forum - 01-19-2012 [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() 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 [solved] die() function - El Forum - 01-19-2012 [eluser]Amitabh Roy[/eluser] Remove die(); and try again The execution stops as soon as reaches die, and prevents the output. [solved] die() function - El Forum - 01-19-2012 [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 [solved] die() function - El Forum - 01-19-2012 [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() Jason : when u say return do you mean I have to do this? Code: function check_user() 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? [solved] die() function - El Forum - 01-19-2012 [eluser]Jason Stanley[/eluser] I use a custom controller. Code: class Admin_Controller extends CI_Controller 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. [solved] die() function - El Forum - 01-19-2012 [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() [solved] die() function - El Forum - 01-19-2012 [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); Edit: Didn't see Jasons answer which is basically the same thing. [solved] die() function - El Forum - 01-19-2012 [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() 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! |