-1

Possible Duplicate:
Detecting Ajax in PHP and making sure request was from my own website

I have a form for uploading images and i am using Ajaxform()

$('#uploadformimage').ajaxForm({

    beforeSend: function() {
         $(".progress").css("display","block");
         $("#response").html("");
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
        //console.log(percentVal, position, total);
    },
    complete: function(xhr) {
            $(".bar").css("width","100%");
            $(".percent").html("100%").delay("8000").parent().slideUp(1000);
            $("#response").html(xhr.responseText);

    }
}); 

This is my javascript code .I want to find out server-side that the form is uploaded by ajax or not? Javascript may be disabled on some computer so i want to show some different data ?

Community
  • 1
  • 1
StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • Before asking your question(s), please use the search. I just pasted in your questions title in search: http://stackoverflow.com/search?q=%5Bphp%5D+How+to+make+sure+ajax+is+used+or+not%3F - See http://stackoverflow.com/questions/ask-advice – hakre Oct 06 '12 at 11:26
  • @hakre it is not necessary that everyone knows everything.You should see this [User hakre](http://stackoverflow.com/questions/6562276/how-to-merge-two-arrays-by-taking-over-only-values-from-the-second-array-that-ha) – StaticVariable Oct 06 '12 at 11:36
  • 1
    That is no excuse for not using the search. – hakre Oct 06 '12 at 11:38

5 Answers5

2
if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest')
{
    // This is an AJAX request
}
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
1

I built a demo and here's the results

Request URL:http://localhost/test.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:32
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost
Origin:http://localhost
Referer:http://localhost/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11
X-Requested-With:XMLHttpRequest
Lewis E
  • 327
  • 1
  • 7
  • I use google chromes inspector there you can see ajax calls aswell as every other external call there form the clients stand point anyway hope that helps – Lewis E Oct 06 '12 at 07:08
0

You could pass an option GET /foo?xhr=1

Then in PHP $_GET['xhr']

chovy
  • 72,281
  • 52
  • 227
  • 295
0

Lets assume that AJAX is not involved.

All Javascript/AJAX can achieve is to make the users experience preferable to the competitors.

So why are you caring about the image when it will be up to the person to upload it. That person may not have Javascript,

Just go for the minimum and add the bells and whistles!

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

You can use the <noscript> tag in your HTML to generate different HTML, so the form can post to a different server-side script or add a hidden field to the form.

Barmar
  • 741,623
  • 53
  • 500
  • 612