user2615947
user2615947

Reputation: 155

Hide var data on ajax

I am making AJAX like function but I have problem that bad user can change the value into any other current user. So, how can I prevent this thing?

$live = 'user1';
$fol = 'user2';

function ajax(like){
    var data = 'like='+like+'&CURRENTUSER=<?php echo $live; ?>&TOFOLLOW=<?php echo $fol; ?>';
    $.ajax( {
        type: 'POST',
        url: 'ajax.php',
        data: data,
        success: function(e) {
            $('#success').html(e);
        }
    });
}

Also I want to move this ajax function into ajax.js file, but i am have problem in getting the value $live and $fol of users because echo $live doesn't work on .js. So, is there any way to do this like Facebook, Twitter AJAX function does ?

Upvotes: 1

Views: 499

Answers (3)

s.d
s.d

Reputation: 255

There can be many solutions for such problem.

  1. Add one of follow user in session before page load so even you dont need to send data in ajax. Just need to confirm action and all data will be taken from session. Hence hackers cant modify users.(This is how i solved the problem in my project)

  2. You can build an function like encode() & decode(). when you are using data in file encode() it first.Then at code end use decode() to extract the info. Since if invalid data came out mean some one has tempered and you will not execute that action. But you have to create such encode() & decode() yourself.

    $live = encode(user1);
    

At php end

   $real_live = decode($live);

3. Ajax request to when start php execution you can have a function like

    check_auth(user1,user2);

So even if some one used bad data your security rules can filter them.

Hope you can use any of them.

Upvotes: 1

C3roe
C3roe

Reputation: 96339

I have problem that bad user can change the value into any other current user. So, how can I prevent this thing?

Of course you can not do that at all.

HTTP is a stateless protocol – so each and every request that reaches your server is to be mistrusted, period.

You have to check server-side whether the requesting client is authorized to request/perform whatever action it is he wants to trigger – f.e. by checking that the user id that is passed as the “current” user against the session where you stored your login information. (So when you have the id of the current user stored in there, then there’s no need to actually send it from the client any more in the first place.)

This is one of the most basic security principles of any web application – don’t trust any incoming request, until you have verified that the client has the appropriate authorization. So asking for how to “hide” any data that is send from the client is completely the wrong question – that would be what’s called “security by obscurity”, and that does not work.

Upvotes: 2

undone
undone

Reputation: 7888

This solution works for apache web-server. For interpreting JS file using php, add this line to your .htaccess file:

 AddType application/x-httpd-php .js

And put your script inside ajax.js. One other way is using rewrite URL:

 RewriteEngine On
 RewriteRule  ^ajax.js$    ajax.js.php  [L]

And put your scripts inside ajax.js.php file. Of course, all these are if you want to show your URL as JS file.

at the top of your ajax.js or ajax.js.php file, before any kind of output, put this:

header('Content-Type: application/javascript');

Upvotes: 2

Related Questions