I have controller written in CodeIgniter
this->layout->view('pages/dashboard/youtubeupload')
This code is the part of YouTube video upload code. When I try running this Controller independently it works well. After the success, I have to redirect the controller to view as stated in the last line above.
code for the view:
print_r($_SESSION);
echo "<p>You need to <a href='".$_SESSION['authUrl']."'>authorize access</a> before proceeding.<p>";
The question is, how can I redirect from the controller to view in CodeIgniter?
It's hard to understand exactly what is After the success for you, the youtube upload video proccess or the simple execution of the controller? Or other thing that I missed?
Inside the controller you'll have many methods that will renderize any views, according with your validations you have the possibility to present error messages or simple redirect the user for a login page if a authenticate session is missing for example:
if($_SESSION['user'] == NULL) {
redirect('/account/login', 'refresh'); // or other view
} else {
this->layout->view('pages/dashboard/youtubeupload')
}
Can you explain more and post more code?
Related
I was creating a simple Codeigniter blog project. Everything was going right. I created a simple user controller which still works and it's the default one too. after that, i created an admin controller with login(), register(), and sendmail() functions inside it. it was working fine with it but when i added the logout function in my admin.php controller and also used the redirect method to redirect the user to the login page if he/she is not logged in to the system. but first, it gave me the error of too many redirect,s and then my admin controller with all its functions stopped working. I shifted the login function to a separate controller named login.php in the controller folder but it's still not working. I am getting 404 error all the time even though i removed the code of logout function and redirect code too but still getting 404 error.
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('id'))
return redirect('admin/login');
}
This was the redirect constructer i used in admin.php and will work with all my functions. and was working fine before i added the logout function. logout function was shown one time i pressed that and then it gave that too many redirects error and after that, i am getting 404 error.
<?php
if($this->session->userdata('id'))
{
?>
<li>Logout</li>
<?php } ?>
This was the code that I used in my logout view to make logout button visible only when the user is logged in with the help of my session.
!!importent
But now i noticed something else. if i add index.php in my base url like websitename/index.php/admin/login then it open the login page even though i used htaccess file already and it was working fine. i was able to access website without using index.php in base url but when i add login details it is not logging me in to the system. it redirects me to the localhost/dashboard page given by xammp.
I am using Laravel 8.0 to create a web application. I have multiple pages that can only be seen if you are signed into an account. I have it setup so that once you go to the page, it uses a controller function to return the view, but before that it checks if you are logged in with a simple if statement. If you are logged in, it returns the view, else it redirects to login page. I am wanting to make it so after they login it redirects them back to the page they were trying to go before. I used return redirect()->back(); to do this, but it returns them too the login page as it thinks that is where it was before. I use a separate route and controller function to login them in via the form. If anyone could help, I would much appreciate it!
You have to use following code. In your login function just add this
Session::put('previousUrl',url()->previous());
$previous_url = Session::get('previousUrl');
And after login authentication successful just call this
return Session::get('previousUrl');
i prefer use this
redirect()->route('your route name')
because in logic, after submit form, it goes to another route which is /login with POST Request
So when u use this
redirect()->back()
it goes to /login with GET Request which is your login form
I have one form in my view.php file. Its URL like https://example.com/members/view/1, https://example.com/members/view/2 etc. When I submit form its calling model through controller like below
public function insert_comments(){
$data=$this->input->post();
$this->load->model('work_model');
$result=$this->work_model->insert_comments($data);
if($result)
{
$this->session->set_flashdata('insert_comments','your comments succesfully');
$this->session->set_flashdata('succesfully','alert-success');
$this->load->view('add_coments');
}
else{
$this->session->set_flashdata('insert_comments','your comments failed');
$this->session->set_flashdata('succesfully','alert-danger');
$this->load->view('add_coments');
}
}
}
and model is like below
public function insert_comments($array)
{
return $this->db->insert('comments',$array);
}
Currently its working fine and on form submit its loding view called add_comments, instead I want reload/refresh current page. I am not able to get idea of how I can do it, let me know if someone can help me for do it.
Thanks!
According to this answer, in the controller you can use:
redirect($this->uri->uri_string());
use this :
redirect($_SERVER['REQUEST_URI'], 'refresh');
Whenever you are reloading or redirecting a page you should always use the redirect() method instead of loading a view.
Redirection basically uses the header() method of core PHP and redirection will never execute the code blocks written beyond the redirect() method. But in the case of loading the view, it can execute until the end of the code block.
In your code, replace the line $this->load->view('add_coments'); with the redirection to the desired controller
redirect('your-controller','refresh');
I hope that helps you.
I have a website build in Code Igniter with a commenting system that loads a controller function with jQuery for each page.
I'm also passing the article ID as URI segment to the controller while loading.
Lets say the controller name is "comments" and the function is:
function get_comments(){
$article_id = $this->uri->segment(3);
echo 'the uri segment is '.$article_id;
}
I call them to my page with the following jQuery:
$(document).ready(function(){
$('.comments-holder').load('<?php echo base_url();?>comments/get_comments/'<?php echo $article_id;?>);
});
and this is fine.
The thing I want to accomplish is to forbid the user to load the "get_comment" function by typing the http://www.domain.com/comments/get_comments/
I believe that is possible not to have a solution for this problem a workaround will work good as well even if it means more changes to be done.
Code igniter has this by default.
Add this in the ajax action:
if ($this->input->is_ajax_request()) {
// your regular ajax code
} else {
// redirect or show error
}
I have a few pages that require login, so all controllers that link to these pages start with
$this->checkSession();
//...rest of the code
CheckSession should verify the session is still live, otherwise display a message and stop the execution of the rest of the code in the controller:
function checkSession()
{
if (!$this->session->userdata('is_logged_in'))
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
exit();
}
}
.
I was expecting these instructions to happen in sequence, but I only get a blank page.
How can I make sure exit() gets executed only after all views are loaded?
In this case Pedro is correct. If they are not logged in just redirect them, it's even better if you can use Public/Admin named base controllers to stop you having to do this in each separate protected file.
Generally speaking though, if you use exit() it will stop the Output library for running. If you just want to stop the current controller from executing but allow output of the controller you can use return in exactly the same way.
function checkSession()
{
return (bool) $this->session->userdata('is_logged_in');
}
Then simply:
if(!$this->checkSession())
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
return;
}
exit() should only ever be used if you really want instant death of your application's execution for debugging, error reporting, etc.
In this case you should not use exit, what you should do if the session is not valid is redirect your app using example:
redirect('/init/login/','refresh');
I had a similar problem. Where I wanted to stop the user to due to no login. But I wanted to offer a list of links for them not simply redirect them to a login page. I am using CI version 1.7.2 and the $this->_output() $this->display->_output() and $this->output->_display() solutions did not work for me. I was however to get my results using the $this->output->get_output() function.
$this->load->vars($data);
$this->load->view('template');
die($this->output->get_output());
$this->output->_display();
exit();
Is the correct answer! Thanks to Sam Sehnert... It's hidden in the comments so thought I'd re-post.
I don't know enough about codeigniter's workflow but it seems to me that you want to redirect to the login page instead of trying to render it. Evidently, none of the code you supplied sends the template to the browser by the time exit() is called.
exit() cuts your scrip there and the actual _output() function of the controller is never called. What you need to do is add action in one of your controllers for example the user login screen and redirect there. You can use the flashdata function from the Session - http://codeigniter.com/user_guide/libraries/sessions.html to pass your message and then catch it inside your view and display it.
Another way which is not very smart but should work is to forcefully call the output function.
function checkSession()
{
if (!$this->session->userdata('is_logged_in'))
{
//the session has expired!
$data['main'] = 'confirmation_message';
$data['title'] = "Session expired";
$this->load->vars($data);
$this->load->view('template');
$this->_output();
exit();
}
}
Actually in the newest CI function to manually call output class is
$this->display->_output();
and don't be worried - it handles caching, content will also be properly gzipped if you set so in config
I usually add and extended controller with login logic that handles login functions, so that if a normal controller is one that is needing an auth then the login method is called automatically and the original content is not displayed. It's a good solution if you would like to stay on the page the user tried to access without redirecting (and then posting him back to the same page)
Put the code in a variable and write it.
$html = $this->load->view('template',null,true);
echo $html;
exit();