Code Igniter: Load controller with jQuery but forbid direct url loading - php

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
}

Related

Reload current URL in Codeigniter After Submit The 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.

How can i put page loader in laravel controller's function?

I am routing my user's to third party URL.
Now whenever they are routing there is a blank page or default tab which is open is displaying, but they are actually redirecting on that URL.
What i want to do like as soon as that routing first function called i need to put page loader there so user's come to know that they are redirecting.
How can i do that in controller ? I can load view file i know but after that remaining code will not work of that function.
Guide me over here.
Thanks in advance.
I tried loading view file but it was not run remaining code.
Simply direct your users to a holding page, and then run some simple javascript redirects to get them to their actual destination.
app/routes/web.php
Route::get('redirect', 'YourController#redirect');
app/Http/Controllers/YourController.php
public function redirect(Request $request) {
$url = $request['url'];
return view('holding-page', compact('url'));
}
resources/views/holding-page.blade.php
<script type="text/javascript">
// Redirect the user to where they want to go after 3 seconds.
setTimeout(function() {
window.location.replace("{{ $url }}");
}, 3000);
</script>
Make sure you url encode any urls that you pass as a query string parameter.
https://yoursite.com/redirect?url=https%3A%2F%2Fstackoverflow.com

Loading a view from controller

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?

response and redirect on one return on laravel 4

I want return response that can download and a redirect that can redirect to other page on the same controller function is it possible if so how can I do it if you could show me like this code if i want to download and then redirect to other page. on one controller.
public function handledown()
{
return Response::download(Input::get('book')) & Redirect::action('ViewController#book');
}
You can't return multiple responses like you are trying to do there.
My advice to you would be to return the response download and then redirect to a route you define within a data-redirect attribute on your button etc what ever you use, with jQuery etc.
Another solution can be found here.

Adding dynamic id in the url using codeigniter

I have an edit page with url
example.com/product/edit/11
Now if the validation is false I intend to reload the edit page but can't figure how to add that id '11' in the url
Things I have done so far
In routes.php
$route['product/edit/(:num)'] = "product/edit/$1";
Now how do I pass that id in the url from the controller
I tried this
$this->load->view('product/edit/'.$edit_id,$data);
Edit: I am not trying to pass edit_id to the view file
I am trying to simulate this:
Edit
So that the function in my controller
function edit($edit_id)
{
//some code
}
Will have have that $edit_id to work with
It didn't work as I suspected.
I know I can pass that id to the view file and make it work, but I wanted to if I can pass that id in the url.
Thanks
Edit: I figured that even if I managed to pass it in the url. I will end up with more problem like setting the form attributes and other stuff. What I did was
redirect('product/edit/'.$edit_id);
and gave an error message by setting the flashdata.
Your Product.php controller
function edit($edit_id) {
//some codes
//passing edit_id to view
$data['edit_id'] = $edit_id;
$this->load->view('product/edit', $data);
}
And in view/product folder, edit.php view file
//some codes
echo $edit_id
Or directly from url in view file
echo $this->uri->segment(3);

Categories