Using JS to include remote PHP file - javascript

I am wondering if it is possible to use JS (jQuery) to make a $.post from any website on any domain, to a script on my server?
The reason for this question, is because I do not want to give out my PHP files to the client (and I dont want to spend money on ionCube or the likes), so making the request to my server would not give out my source.
My prefered way would be to simply include a JS file, like
<script src="http://MySite.com/MyScript.js"></script>
and have a function in there that does the Post to my PHP script, however due to the same-origin policy, I cannot do a POST request to a remote server.
I could of course make a proxy.php and make the request to it, but I am trying to keep it in a way so the client only have to include a small snippet into their HTML code, in order to use my app.
Is there a (less painfull) way to do this?

Instead of using Javascript, why not give your client a PHP file that calls file_get_html() against your code on your server? It would have the same effect, without worrying about the client-side Javascript.
This would function similarly to a remote PHP include, but it is executed on your server and received as HTML.
You could also use cURL in PHP to call your remote PHP script, returning the output as HTML, and parsing out the bits you need.
file_get_html() is component of Simple HTML DOM

Related

How do you run a server-side script without it being sent to the browser at all?

I understand that the title is a little confusing, but essentially I want to make my website run a php file on the server side that uses arguments from the html e.g
document.getElementById("example").value;
So I'd like to run it on the server but not have it linked to the html file. Is this possible?
I suggest you create PHP scripts on server side and reach them with XMLHttpRequest (tutorial here) or AJAX with Jquery for example.
PHP scripts you need, don't have to generate any HTML code : they just would process data.
In your JS, you get your values from the page, then you send your data to the server.

How to send a javascript file (with several functions in it) from the server to the client side?

I need to have the functionality in the server side in order to hide the implementetion to the final user.
I didn't find a topic with this kind of solution.
I have a .js file with functions I use within the html5 file.
The js files are "called" in the html by using the script tag, but through the url the user can track them and see the .js file content. I don't want this to happen.
$getScript() does the job, but again the url can be cathched, thus the file content too. Much the same with $ajax function.
Everything work ok, but I want to hide the js content.
The .js file is something like this:
var x, x,....
function A(){...}
function B(){...}
and so on, I use A(), B() functions in the html.
Which is the best approach to get the content file from the server without doing the url visible?
Server: nodejs. (I send some json files through socket.io correctly, but I don't know how to achieve this other issue.
Thanks in advance, best!
If you are sending sensitive information to the client then you are doing it wrong. No matter if the client has the URL to the script, they will still be able to find it if they are determined as long as it is sent to their computer.
Find a different way to accomplish what you are trying to do without sending sensitive information to the client. It is not safe.

Using ajax or jsonp to retrieve php source code

Is there a way, using ajax or jsonp, to retrieve a php file source code?, the request is made by the same domain as the php files, I don't want the server to process the files, I only interesting with retrieval the content of the files, maybe there is a specific header used to request the server not to process the requested file?
The only solution I found is to change the file suffix, from .php to .txt/.html/etc but this solution is my last resort, if there is any other way I will be very thankful.
You could implement a php wrapper which your ajax code could request and pass in the file you would like to see the contents of:
Request via ajax: http://yourdomain.com/get_content.php?file=test.php
get_content.php:
<?php
if (isset($_GET['file'] && file_exists($_GET['file'])) {
echo file_get_contents($_GET['file']);
}
?>
Please do not use the code above in production mode, it is just an idea how to handle your problem.

How is read/write a text file possible in javascript if my text file is stored in server?

i am trying to read a file from the server and then write back to that same file again using javascript. Is it possible to read/write the text file using JSON,Javascript or Ajax.
It is not possible to edit server sided files using pure Javascript, imagine the security issues :)
What you could do is to use a server sided language, like PHP or ASP, and send an ajax request to the server containing the information you want to write.
JavaScript is client-side language, so it don't have access to server. To do it you have to use AJAX and some server-side language, eg. PHP to handle requests.
You can read a file with javascript via an AJAX call.
You cannot save a file with javaScript.
You will need a server side language like PHP.
You can use:
step 1) jquery to load a file: http://api.jquery.com/load/
step 2) jquery to send the modified text to the .php : script http://api.jquery.com/jQuery.post/
step 2.1) save the data in php : http://php.net/manual/en/function.file-put-contents.php
$.getJson("/main.txt",function(jsonObject){
// your logic;
})

Read response of a file called with script src tag from external sever

So basically we are trying to read a text or html file from external server which we don't have control off .
We are using script src tag to fetch the file but we are unable to read it
<script type="text/css" id="Script1" src="http://www.thirdparty.com/example.txt" ></script>
like in this screenshot you can see we are fetching a random txt file and we are getting a response but through javascript we are unable to read it .
Screenshot Image link
So now our motive is to read the example.txt file and store the content into a javascript variables.
Things that we have already tired but didn't work due to some or other reason
Using XHR and jQuery load function
$(document).ready(function () {
$("#container").load("http://www.thirdparty.com/example.txt");
});
but as I said its third party server and we don't have control over it , that's due to Cross-Origin XMLHttpRequest we are not getting any response from the server.
by the way is there anyways to eliminate origin header from the xhr request ?
Using proxy server wont work because of ip issue and cookies needs to be send from the client side.
Getting innerhtml and innertext of the script tag but it didn't return the response rather it give code between tag which is not required .
P.S. we what something client side javascript bases solution not serverside due to some specific requirements .
thanks in advance for helping
It's not possible due to security reasons. There is no, I repeat NO way to do this on modern browsers. At least with JS. You could use JAVA, but it's probably not what You want.

Categories

Resources