Jquery Post to external php - php

I want to post to an external php file and get the result. It a php that i have hosted in my server online. I want the static page in my localhost post by ajax and load the html in a div. But I'm not able to do this.
$.post("http://www.site.com/index.php", { font: "panchami", input: "hi" } );
Is there anything wrong in this?

The Same Origin Policy prevents Ajax calls to external domains.
Popular workarounds include
JSONP
Embedding the data in an iframe instead
Using a server-side proxy the does the fetching (see #BrunoLM's answer for a PHP example; it is possible in any server-side language)
YUI's Get as shown in #Alex's answer
depending on what your use case is.

Javascript doesn't allow cross domain requests.
What you can do is a PHP file on your server that reads the contents of the other site:
<?php echo file_get_contents($_REQUEST['url']); ?>
Then make requests to your file, like so:
$.post("proxy.php?url=external_url", ...);

Or using GET, for example:
http://developer.yahoo.com/yui/get/

This kind of request is dangerous, it is called a Cross-Site request and is forbidden by most browsers. If you look in your error console you should see a message to that effect.
If you really have no alternative then you can consider using iframes, the src attribute can be outside the current domain and you can parse the information using javascript.
Hope that helps :)

Related

How have safe HTTP Request Method

when use GET Method for receive JSON data , we can acsses the result directly from web browser , for example i send a mydata value from ajax to a main.php file and it process and get answer show a result some thing like below :
<?php
if (isset($_GET["mydata"])) {
if ($_GET["mydata"]=="hello"){
echo "hello world";
}
}
?>
but when a user call it in browser directly like http:mysite.com/mydata.php?mydata=hello recive answer . i want dont allow users to get answer of http request directly , and just can show it from ajax result of main page is it possible ?
You're asking how to prevent an ajax-only request from being accessed directly by copy-pasting the URL into the web browser; that is, only allowing the URL to be accessible via ajax on the main web page.
Well, there are a few things you can try:
Check the Referrer for the URL of the main page with $_SERVER['HTTP_REFERER']
Set a header in Javascript using xhr.setRequestHeader() and then ensure it's value by checking for $_SERVER['HTTP_X_....'] in PHP
Like Jay Bhatt recommended, check for the X_REQUESTED_WITH header, but be aware this might not always be set (see: X-Requested-With header not set in jquery ajaxForm plugin)
However, in any of these situations you should be aware that anyone who knows what they are doing can easily set any HTTP header, variable, or even modify the referrer which is sent to the server. As such, there is no 100% guarantee that your resouce can be accessed only via AJAX on the main web page. There is no control built in the internet to verify where a request is coming from, so anyone can easily spoof or fake it.

How to send out a http reponse

I am a php newbie and I came across some problems when use php with apache.
I don't want to use browser to send or receive http request so that I have to manually deal with this problem. On server side, I can use file_get_content("php://input") to extract body from the http request, but how can I build the http response? The method is "post" and I want to insert a xml string to the response body. Thank you for help!
Example:
See: http://php.net/manual/en/function.header.php
For HTTP codes, Google them up.
Simply echo "whatever" will return that back to the users browser. Remember that PHP is an interpreted language originally for inline scripting (i.e. mixing with HTML) prior to sending to the browser. So a file:
<?php echo "here"; ?>
will just return that reply to the users browser when they go to the appropriate URL. Adding any parsing etc. can be done in addition to this basic application logic.

Using PHP inside Javascript? To shrink URL

I made a bitly url shrinker, and I currently have a Soundcloud Javascript API that outputs a url link of a song. Im trying to shrink it using my shrinker. The shrinker works using this:
<?php echo $bitly->shorten('http://google.com'); ?> //Equals google.com in short url format
The javascript code I'm trying to implement it in is this: Ill go ahead and give you what I tried to do already, that didn't work.
Before I edited:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
After I tried:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
Any suggestions, I'm open to anything. And would love to make this work!
That has been already explained but in case you're new to this concept, there is a simplified explanation.
<?php tags in your code are processed on server before your page is sent to user's browser. Actually browser never receives those tags - they're replaced with PHP output on server and then the resulting page is sent to user.
As a result of some mistake sometimes PHP code makes into user's browser but it behaves as any other non-standard tag - content between <?php and ?> would be invisible to visitor.
JavaScript, on the other hand, operates in user browser with (in our case) what PHP has already output. When you change the page with JavaScript, it's not sent back to server - actually, server is totally unaware of that, so it can't execute the PHP code you're outputting by your JavaScript.
In order to achieve a similar result you need to send an AJAX request from your JavaScript code. It'll basically be another "page request" initiated by your JavaScript, but happening at the background with PHP output not replacing your current page, but arriving into your JavaScript code. This way your JavaScript is outputting PHP output and not PHP code, that's why it is possible.
You cannot call PHP on a string that is generated via javascript since PHP is server side and executed before JavaScript which is client side.
If you want to shorten this string, you'll have to make an ajax call to a php page that will return the shrunk url.

getting cookie from an external javascript in php before generating HTML output

Is it possible getting cookie from an external js with Php before generating HTML to the browser?
Something like
<?
//Get if i have some cookie information
if($_COOKIE["js_app"])
$cookie_js = $_COOKIE["js_app"];
//now, talk with some app.js
if($cookie_js)
$cookie = some_function(target = 'some_domanin/app.js',$cookie_js);
else
$cookie = some_function(target = 'some_domanin/app.js');
$_COOKIE["js_app"] = $cookie;
//now i can generate the HTML output.
....
?>
It isn't possible because the javascript would never be executed. Your some_function (assuming it used cURL to make the request and pass along the cookie), would just receive the javascript code (like what happens when you view javascript source in your browser).
When JS is loaded by the browser, the browser's javascript interpreter handles parsing and executing the javascript.
Edit: Hmm... googling for "php javascript interpreter" resulted in http://j4p5.sourceforge.net/index.php. Never used it before and probably never will but if you don't need to interact with the user-side of things, that might help you out.
You are looking for a way to send request with cookies, and get the cookies from the response using PHP, right? I think PEAR HTTP_Request is what you are looking for, although you have to install it on your server as a package.
If that is not an option, I guess you will have to do HTTP requests using sockets.
UPDATE: If you are setting the cookies using Javascript, you will have to parse the javascript - not really an option. These functions only work when the cookies are set server-side.

Proxifying pages that use javascript

I'm building a proxy and am trying to deal with a page that uses javascript. The page has a button like this:
<input type="submit" ...cut this out... onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(...cut this out...)) />
When I click this button from my proxy the URL is rewritten to look like this (notice the javascript code inserted here):
http://domain.com/proxy/index-new.php?q=https://proxiedomain.com/javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(...cut this out...))
I'm not sure how I can handle this in my proxy server. When I don't use a proxy the headers are sent to a completely different page (the URL doesn't include this javascript). Can anyone give me any hints as to what I should look into or read to understand this problem better? From what I understand so far, I need this javascript to be executed (which would require a cient browser).
Any link that points to javascript:... will run JavaScript but not necessarily load a page.
I would leave these links alone, and instead ensure that the form action URL is set to your proxy, and any location.href = 'http://www.example.com/fully_qualified_urls'; are swapped for the proxy URL.
e.g. a simple RegEx replace of "OLD_URL" for "NEW_URL" (accounting for any HTTP vs. HTTPS protocol differences) should suffice for the most part.
Note: I'm aware it isn't "simple", but trying to inspect a javascript: based "link" to modify its behavior will be very awkward.

Categories