Call an external PHP script on a website without using PHP - php

My issue is following: My Partner's have websites and I want that partners can include a script from my server that dynamically generates a link and an image.
i.e.
myscript.php (on my server) generates following output:
<a href="http://www.myserver.com/apples.php?12345">
<img src="http://www.myserver.com/images/12345.png" />
</a>
I don't want my partners to be bothered with any php scripting, hence the best solution for now is to supply them with an iframe, where the src is the link to my script on my server that generates the output above.
However I clearly don't have any control over my partner's website and i.e. opening a lightbox out of the iframe certainly won't work.
Because of that I tried a different approach. I used Ajax to dynamically load my script, however this also doesn't work due to ajax security features (you cannot call an external script via ajax - only if you do a php workaround).
So basically, my question is: Is there any good solution to call my script on my server on the partner's website and display the generated code on my partner's website without the use of php?

Have your PHP generate JavaScript (don't forget the content-type header). Then they can include it with a <script> element.

Make a PHP file, like
<?php
$url = "http://www.myserver.com/apples.php?12345";
$img = "http://www.myserver.com/images/12345.png";
echo "document.getElementById('divthing').innerHTML = '<img src=\"" . $img . "\" /> '";
?>
Your partner's page would be like:
<html>
<body>
Hey, check this site out: <div id="divthing"></div>
<script type="text/javascript" src="http://yoursite.com/script.php"></script>
</body>
</html>
(I know, not really clean code (innerHTML etc), but you should get the idea :))

Could you make it with javascript file which replace/creates that anchor where ever you put the javascript link.
picture.js:
$('#image').ready(function(){
var image = 'http://host/url/to/image.jpg';
$('#image').load(image);
});
on you partners site:
<div id="image">
<script type="text/javascript" src="http://yourhost/picture.js"></script>
</div>
I don't know if it possible, but.. :) and this needs jQuery. And im slow.

Related

Load content from website to MATLAB including internal frames (iframe)

I'm using urlread in MATLAB to load the content of websites and then search that for further use.
However, I came across a site on which the content I need is located in a specific internal frame which is embedded in the index.php through <iframe>.
When I use urlread with the url of the file which is seemingly embedded in the main file, I still only get the content of that index.php instead of that of the targetframecontent.php.
I guess urlread might not be the correct tool for this or I might be overlooking other things which make my attempts unsuccessful.
Is there a way to get the content of such an internal frame into MATLAB?
EDIT: A bit more precisely:
I would usually use the following to read in the content of a website to my MATLAB workspace:
data = urlread('http://[...]index.php')
But doing that I only get the content of the parent index.php and not that of the embedded targetframecontent.php. I figured that out when going through the websites source code, where the part of the website which includes the content I'm interested in gets loaded into a frame with:
<iframe src="http://[...]targetframecontent.php" width="850px" height="1000px" border="0" frameborder="0"></iframe>
Because of that I tried to use urlread directly with the url which is loaded in the specific target frame.
However, when I did this I still got the same content as if I was still loading the index.php.
So it seems to redirect urlread back to index.php whenever I try to use the direct url of targetframecontent.php. That is also what happened when I tried to load targetframecontent.php in a browser.
Thanks for any help.
It looks like the site in question checks the Referrer of the HTTP request. Using wget, if you pass the --referer option, you can download the <iframe>:
$ wget --referer="http://www.sf.tv/sfmeteo/lokalprognosen/index.php?q=Gen%C3%A8ve" -O test_ref "http://www.sf.tv/sfmeteo/lokalprognosen/detailprognose.php?id=&q=Gen%C3%A8ve&max=&drs=0&kiosk=&js=&deeplink=&f="
$ cat test_ref
[...]
<body class="sf">
<!-- referrer:http://www.sf.tv/sfmeteo/lokal -->
<script type="text/javascript"> <!--
var keineprognose = '0';
// -->
</script>
<p class="inv">
zum Inhalt<br />
</p>
However, if you wget without sending a referrer, you get:
wget -O test_noref "http://www.sf.tv/sfmeteo/lokalprognosen/detailprognose.php?id=&q=Gen%C3%A8ve&max=&drs=0&kiosk=&js=&deeplink=&f="
<body class="sf" role="application">
<p class="inv">
zum Inhalt<br />
</p>
<!--googleoff: all--> <div id="HEADWRAP" class="sf-header">
<div class="INNERWRAP">
<h1 class="inv">SF Schweizer Fernsehen - Navigation</h1>
Which is the original, parent page.
Therefore, to suck this into MATLAB, you'll need to set the Referer HTTP header. Unfortunately I don't have MATLAB in front of me right now, but this page looks like it gives a good introduction to how to send headers with MATLAB HTTP requests: http://undocumentedmatlab.com/blog/expanding-urlreads-capabilities/
Edit: Because it's not super-obvious from that page, here's a link to the urlread2 function, which also includes some examples: http://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2/content/urlread2.m

php execute a series of scripts in a new tab

I would like to create a php file (lets call it master.php) that executes automaticlly a series of scripts in a new tab. Every script listed should be executed in a different tab. I tried using header(location), however it can only be used to redirect once. Any idea?
What you want goes beyond the scope of php. You have to use javascript to control the logic among the browser's tabs you want to launch.
From php, you should generate the proper javascript code to do that, in particular take a look at the javascript function window.open
You can create a hyperlink as below:
<body onload="document.getElementById('runScripts').click()">
<a id="runScripts" href="script1.php" target="_blank" onclick="window.open('script2.php');window.open('script3.php')">Click Here</a>
If master.php returns the first page, and then you could use javascript to open the other pages. Please note that this does not have 100% browser support.
<html>
<head>
<script type="text/javascript">
window.open('page2.php','_newtab1');
window.open('page3.php','_newtab2');
window.open('page4.php','_newtab3');
window.open('page5.php','_newtab4');
</script>
</head>
<body>content of first page</body>
</html>
You could also use '_blank' instead of ''newtab'
I would suggest using Javascript to execute the scripts in different tab. However this open new windows and popup option has to be disabled. New tabs are not opened unless user explicitly clicks on an anchor element. The following code is an example from withing a PHP for loop.
window.open("script" + <?php echo$i ;?>+ ".php", '_blank');

Script source file with php not parseing

I have a menu that appears on all my pages. It contains php for passing filenames to url's as follows. Here is a snippet...
document.write('<li>abc</li>');
document.write('<li>def</li>');
document.write('<li>ghi</li>');
It includes css and that works fine.
I have saved the menu as a seperate .js file and used...
<script src="js/menucss.js"></script>
...to load it from each page that I want it to appear. The menu displays and the css works fine - however the php does not parse and the page shows the php markup. What am I doing wrong?
Thanks in advance.
Neil
You shouldn't do it like that.
Have a page called menu.php saved somewhere, which includes:
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
</ul>
Then include it, on the server side (index.php):
<html>
<head>
<title></title>
</head>
<body>
<header>
<?php include("main.php"); ?>
</header>
</body>
</html>
This approach is far superior:
It does not require an extra request to get the data.
There will be no flickering or awkward moments of data loading, the menu would appear as if you've coded it directly into the page.
It's much faster.
It doesn't use JavaScript, which the client can choose to disable.
You confuse server side and client side. PHP is about server side and PHP code it is interpreted on server. JavaScript is about client side, and JavaScript runs when user already has gotten html page, so when you dinamically add php in html on client side by JavaScript it cannot be run.
From what I understood I think that you are Trying to run PHP inside a .js file.
You should include that code in a PHP file.
The problem you're experiencing is probably due to the fact that you're attempting to run PHP on a .js file, and your server is not configured to it.
Change the file's extension to .php and use:
<script src="js/menucss.php">

Can I source a php file as javascript?

I am using a WP template that allows me to incorporate arbitrary HTML. Unfortunately, I have to use this particular widget and can't use other WP widgets.
I have on my webserver /some/path/serve_image.php that spits out a random HREF'd IMG SRC with a caption and some other info from a MySQL query.
Now...how can I say "take that output and treat it as HTML"? If I just put "/some/path/serve_image.php" I get that literal string.
I tried:
<script type="javascript" src="/some/path/serve_image.php"></script>
but that didn't work. I tried changing everything in serve_image.php to be document.write() calls and that didn't seem to work either. I'm not the world's greatest JS guy...
So if I have a URL on the net that spits out some HTML and I want to include that HTML in my web page, what's the best way to do that? Sort of like what Google does with Adsense - you source their show_ads.js.
Why no? Add
header('Content-Type: application/javascript');
And output JavaScript Like:
echo("var image = \"".$images[array_rand($images)]."\";");
echo("$('img.randim').attr('src', image);
No. JavaScript and PHP are two completely separate languages. In fact, if it was JavaScript, you aren't even loading it the right way.
<script type="text/javascript"></script>
The way you're trying to do it would throw a parse error, because it would try to use the PHP as JavaScript. Some browsers would even reject it, because PHP files have a text/html MIME type, while JavaScript should be application/javascript.
PHP has to be done server side, so loading it in the client just doesn't work.
What I think you're after is this:
<?php
require('/some/path/serve_image.php');
?>
Just place that wherever you want the image to be.

hide javascript from showing up in browser

I have some javascripts that I am using in my files. But when we view the source code it shows our javascript as it is. Is there any way with which we can hide our javascript from showing up in the browser using php.
There is a free javascript obfuscator at javascriptobfuscator.com. It will not prevent dedicated people from "stealing" your code, but normal copy&paste will not be easy.
Also see this question: How can I obfuscate (protect) JavaScript? . It contains some very good answers and also explain how this is security through obscurity.
That's how it works, it visible to everyone.
You can obfuscate it, though.
As Javascript is executed inside the browser, on the client's machine, it has to be sent to that client machine.
So, one way or another, the client has to be able to read it. So, no, you cannot prevent your users from seeing the JS code if they want to.
You could obfuscate it, but someone who really want to get to your source will always be able to (event if it's hard)... But the thing is : why would you prevent your users from seeing the JS source code if they want to ?
As a sidenote : with minified/obfuscated JS code, when you'll have a bug, it'll be really harder to track down... (and you really have to keep a no-obfuscated version on your development/testing machine)
I recommend minifying it and that will remove the comments and white spacing from your code. If you don't want the names of the variables visible then you will need to obfuscate it.
I'm not sure if this will work, I may try it sometime. But basically:
<script type="text/javascript" src="MyScript.php"></script>
In the PHP file add some sort of refering to check what page requested it or what the last page was. Then if it was one of your own pages, then echo the JS, if not then don't echo it. It will still be possible to read the JS, but even harder than just viewing source and de-obfuscate it. So you could also obfuscate the code inside the .php file.
no. javascript executes on the client side.
There is another way of hiding the Javascript for the most simple users
Just test here to try finding the javascript behind the textbox...
Yet, the script is still visible for experienced users -see the bottom of this post to understand why-
The idea is to put your javascript functions in a separate ".js" file. When loading your source PHP or HTML page, instead of calling it directly with
<SCRIPT language="JavaScript" SRC="original_file_to_hide.js"></SCRIPT>
, you will include a header php script that will copy the "mysource.js" file to a random "kcdslqkjfldsqkj.js" file, and modify your HTML file to call
<SCRIPT language="JavaScript" SRC="temporary_copy_of_the_file.js"></SCRIPT>
instead. After that, just delete the copy kcdslqkjfldsqkj.js file on your server, and when the user will look for the source code, the browser will link to a vanished file !!!
So this is for the theory, next, there is a small issue to workaround : if the HTML/PHP file is loaded too fast, your script will be vanished from your server before the browser had time to load the script.
Thus, you need
To copy the file to a different random name
To load the file in the source PHP file
To wait a few seconds after your HTML/PHP file is loaded before...
...Deleting the file
Here is the source for the HTML/PHP "test.php" page which is to be displayed to the end-user:
<?php
//javascript source code hiding technique : Philippe PUECH, 2013
//function thanks to Stackoverflow, slightly modified
//http://stackoverflow.com/questions/4356289/php-random-string-generator
function RandomString()
{
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$randstring = '';
for ($i = 0; $i < 10; $i++)
{
$randstring = $randstring.$characters[rand(0, strlen($characters))];
}
return $randstring;
}
//simple header script to create a copy of your "precious" javascript ".js" file
$original_filename="functions.js"; //find a better (complicated) name for your file
$hidden_filename=RandomString().".js"; //temporary filename
copy($original_filename,$hidden_filename);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Catch my Javascript if you can !</title>
</head>
<SCRIPT language="JavaScript" SRC="<?php echo($hidden_filename); ?>"></SCRIPT>
<script type="text/javascript">
</script>
<body onLoad="javascript:testfunc();">
This is the page with anything you like !
</body>
</html>
<?php
sleep(1);
//you can comment following line
echo "finished !";
unlink($hidden_filename);
?>
Here is the source for the "functions.js" file which will be hidden to the user.
// JavaScript Document
function testfunc(){
alert("It works...");
}
However, as told in the comment, the developer tools of the browser will keep the script in memory, and make it still visible to the curious users... ;-((

Categories