Welcome Guest, Not a member yet? Register   Sign In
prevent direct access to this function/page ?
#1

[eluser]Arun Joshi[/eluser]
I have a controller named 'posts' and a function 'ajaxLoadProperties' in it. This function will call via ajax to load some ajax contents.
But if anyone trying to access this page directly by typing in address bar, this data will be available to users. Can I prevent direct access to this function/page ?
That is keep this page only for ajax calling..

my path
Code:
http://localhost/mysite/posts/ajaxLoadProperties
#2

[eluser]Dam1an[/eluser]
you can add an underscore to the start of any function you don't want accessible directly via a URL, then you just need to make sure you use the underscore when calling it internally
#3

[eluser]Arun Joshi[/eluser]
Thanks Dam....
CI Rocks..........
#4

[eluser]cahva[/eluser]
Hi,

You can also prevent other than ajax request with:

Code:
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
    redirect('');
}

For example when I have ajax functions in their own controllers so I put that to the constructor.
#5

[eluser]Arun Joshi[/eluser]
hi Damian,

When I add underscore in function name it prevent me from accessing page via typing in address bar. Thats fine. But I cant access that page even through an ajax call.

@cahva,

I want to check the ajax call in a function in my controller. My controller is common for functions other than ajax calls. So I cant include this ajax path checking in my constructor.
I need to check it in my function say ajaxRemove() in my controller say 'posts'
#6

[eluser]Thorpe Obazee[/eluser]
[quote author="Arun Joshi" date="1244643788"]
@cahva,

I want to check the ajax call in a function in my controller. My controller is common for functions other than ajax calls. So I cant include this ajax path checking in my constructor.
I need to check it in my function say ajaxRemove() in my controller say 'posts'[/quote]

Then check only in that method instead of putting it in the constructor.
#7

[eluser]Arun Joshi[/eluser]
Sorry bargainph,
Its not working... I tried like this

Code:
function ajaxRemove()
{
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
    echo 'ajax call';
}
else
{
    echo 'url call';
}
}
#8

[eluser]Thorpe Obazee[/eluser]
Ajax helper:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function is_ajax()
{
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}

?>

Code:
function ajaxremove()
{
    $this->load->helper('ajax');

    if ( ! is_ajax())
    {
        // not ajax
    }
    else
    {
        // ajax
    }
}
#9

[eluser]pistolPete[/eluser]
I wouldn't rely on HTTP_X_REQUESTED_WITH.

You could try using a POST request:

Example JS code (using jQuery)
Code:
$.post("/your_controller/ajax_function", { ajax: "yes"},
  function(data){
    alert(data);
  });

PHP controller code:
Code:
function ajax_function()
{
   if($this->input->post('ajax') == 'yes')
   {
      echo 'ajax call';
   }
   else
   {
      echo 'url call';
   }
}
#10

[eluser]Arun Joshi[/eluser]
I dont have ajax helper in my helpers folder.




Theme © iAndrew 2016 - Forum software by © MyBB