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.
Related
Hey im very new to web developing and i have a big problem.
Im trying to write a javascipt script which does a jquery getJSON request to a php file which then reads out the content of a JSON file and echos them back to the javascript. Now im getting a access-control-origin-header. I don't want to change the header. Is it somehow possible to read the contents of a json file with javascipt? I am the one writing the json file so is it not possible to give everyone access to that one file?
Instead of sending a request to the other origin from client-side, have the getJSON() function call a URL on the same origin. From this endpoint some server-side code can call your PHP script on the other origin and return the result back in it's response.
Trying to save an object to a JSON file in javascript by converting the object to JSON (via JSON.stringify), and passing the stringified JSON to a PHP script using AJAX. However, this continually throws a 405 error. I suspect this has to do with the config file of my Nginx server, or it could be that I'm not hosting the PHP properly. I've seen the phrase 'static hosting' mentioned, which is unclear to me. Currently, the PHP script exists in the same directory as my HTML and javascript (the data in the object was collected via the HTML interface). I've tried adding headers in the nginx config with no success. General examples on how to enable CORS and hosting PHP with Nginx would be helpful.
Javascript :
$.post('save.php', {data : jsonData});
Nginx config currently has no headers added as this has not been successful in the past.
Never mind. The problem was with the PHP hosting. The default config file has a commented portion that forwards PHP to a server at 127.0.0.1:9000. I call this script with '127.0.0.1:9000/myScriptName.php'.
Now if only the PHP script itself would work...
while(x<=num_of_cpkgs){
var cpkg_navtray = '\'navtrays/' + cpkg_array[x] + '.html\'';
<?php include ?> cpkg_navtray <?php ; ?>;
x++;
}
cpkg_array contains potentially multiple file names. I'm wondering if there's a way to include a Javascript-generated filename in a PHP include statement like this?
This doesn't work like this.
TL;DR:
You need AJAX calls for this.
Longer:
When you load a page in your Browser, the server send you the file. If the file is a php file, then it calls the php to process the file. After the file is processed the server send it to you as a static file.
With JS you can do some interaction to the website. Until now you probably got used to sending each file as a GET or POST data with a form. With JS you have to make an XMLHttpRequest to create a dynamic request and your page is won't be refreshed again however you will get the response as a variable in JS.
Read all about this here.
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
I am trying to create a web application that loads content dynamically. When I do this, of course I want to do the development locally, i.e. localhost. Some of the "functionality" is a form and when posting that form an e-mail is sent from the server. Because I want to access the servers e-mail functionality, I am linking that specific page to the server. But the problem is that it is not loaded.
In my script below it works, but if I change the comments so I am pointing to iandapp.com, than I just get empty string. It's exactly the same page, just copied it to the server.
$("#support").click(function () {
if(support_page==null){
//$("#section2").load("http://www.iandapp.com/smic/subscription_2.php", function(data) {
$("#section2").load("subscription_2.php", function(data) {
support_page = data;
});
}
The script is located inte the main page (index.html) and content should be loaded into a div with id="section2".
I know that (support_page==null) is true because I have a break point inside where it stops.
Please let me know what the probelm is and how I can fix it. I have been going on for hours trying to get this working.
Thanks in advance!
google about
cross domain ajax requests
. This is disabled in the browser level. There are ways to circumvent this, both client side and server side.
It probably has something to do with it being a cross-domain request. You could use what I consider to be a "hack", http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/, but I.M.O. it's not worth it.
Have you considered sending through an SMTP server instead? If so, you'd have no problem with the file (sending the mail) being local.
And what about adding proper headers on server's http response to allow crossdomain ?
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Origin: *
Use .getJSON() instead of .load(), this method supports cross-domain requests. You'll need to make sure your PHP script does something like the following:
echo $_GET['callback'] . '(' . json_encode($results) . ')';
jQuery will append something like ?callback=callback0234 to the request url because it wants you to 'call' the callback function when your script returns. So the output of your script may look something like:
callback0234('mydata': '<p>This is my data</p>')