I am using window.location.href in a php function which forces a file download without opening an additional window. The file download works and the rest of the php script executes. However, nothing that I output after the line with the javascript in the function will show up in the iframe. Is there a way to redirect the output back to the original php script?
Here is the javascript line from the php file:
echo "<script>window.location.href='download.php';</script>";
Here is the code from download.php:
<?php
session_start(); // Starts new or resumes existing session
$filename = $_SESSION['filename']; // Creates variable from session variable
header("Content-Type: text/plain"); // Output text file
header("Content-Disposition: attachment; filename=$filename"); // Output file name
readfile($filename); // Outputs the text file
?>
This is an example of something in the php file that will not output to the iframe after the line of javascript:
echo 'Test Output';
Thanks in advance,
Jay
EDIT
I replaced the line of javascript with the following and it works perfectly:
echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";
You don't need to put the redirect inside the iframe page.
The best way I can think of atm is to use an invisible frame as mentioned in Andrew Dunn's answer to this question
Quoting from that answer:
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
Have that inside your main page. url is basically the download url ("download.php" in your case). You can use a button to make the download manual. If you want it forced. Add src=url to the iframe and remove the script.
I would advice researching and using jQuery.
Related
I am trying to protect my content from example.php file. Therefore, I need to disallow direct access to this page as well as any iframe point to it without my permission.
I configure a php file called iframe.php loading iframe from example.com. This is how I did:
Code for example.php:
<?php
session_start();
if(!isset($_SESSION['iframe']) || !isset($_GET['internal']) || $_SESSION['iframe'] != $_GET['internal'])
{
die("This page can be accessed just from within an iframe");
}
unset($_SESSION['iframe']);
?>
Code for iframe.php
<?php
session_start();
$_SESSION['iframe'] = md5(time()."random sentence");
?>
<iframe src="example.php?internal=<?php echo $_SESSION['iframe'];?>" width="500" height="100"></iframe>
It works well. However, When I click on any link in iframe.php, I will be redirected to other page. After that, I click the Back button in browser and receive the message: "This page can be accessed just from within an iframe".
Could you please help me keep the page working perfectly after getting back?
Thank you for reading.
Its Simple, you can do it by Javascript.
if(window==window.top) {
// not in an iframe
}
In your file with iframe code just set php cookies:
<?php
setcookie("stackoverflow", 1);
?>
In your php files add code:
<?php
if(isset($_COOKIE['stackoverflow'])){
echo "";
}
else{
die('Good Bye');
}
?>
Best Regards ;)
I have a site https://one.com/go.php that (for example) contains :
session_start();
$_SESSION['id_ad'] = 123456;
header('Location: https://two.com/index.php');
and on https://two.com/index.php user click and go to https://three.com/index.php that contains :
<script type="text/javascript" src="https://one.com/2.php"></script>
and finally 2.php is:
<?php
session_start();
header("Content-Type: application/javascript");
?>
alert("<?=$_SESSION['id_ad']?>");
can i access $_SESSION['id_ad'] in 2.php ?
I tested only on firefox works.
////////EDIT//////
thank you to #Dimash to mention my mistake, but the main question still exists
It works, you just return javascript wrongly. Firefox for some reason works, but other browsers fails.
Here is working code:
<?php
session_start();
header("Content-Type: application/javascript");
?>
alert("<?=$_SESSION['id_ad']?>");
Please note, that code was tested.
expect javascript file, not php with tag, you need return reall js file, so you need add header content type, telling that it is js file + remove script tag, as it is not html but regular js file.
When I try to load this simple code in my wordpress post,
Download
I inserted the above codes in "text" / "codes" area of the editor,
but when I went over to the Visuals,
it shows "Download", which is fine,
but when I revert back to the "text" area it seems wordpress has strip away the PHP codes, leaving behind only,
Download
It basically strips away all the onClick php codings,
can any wordpress guru's out there point me in the right direction?
I've tried some PHP loading plugins but it seems that it won't work?
Thanks
If you are trying to run a PHP function from an anchor href link:
The link passes the var "dl" with a value of "1" in the query string back to the same page.
<a href="./?dl=1">
When the "dl" is received, the function is run.
if (intval($_GET['dl'])==1)
Assumes the page is index.php
<?php
if (intval($_GET['dl'])==1){AMIdownload(true,'11721','1.1.5.26','.','.','','http://oi61.tinypic.com/167uu0j.jpg','test try','http://bit.ly/1CYaaJs');}
echo <<<EOT
<!DOCTYPE html>
<html>
<head><title>Call PHP</title><body>
Download
</body></html>
EOT;
function AMIdownload(){
echo "<h2>Downloading</h2>";
}
?>
Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>
Is there any other way to launch an automatic download. Currently we do it this way.
If download does not start click here:
Filename (121 MB)
And much later on in the page we have this code.
<script type="text/javascript">
window.onload=function(){
setTimeout(function(){
window.location.href='http://www.example.com/file.zip';},2000);
};
</script>
Problem is that it sometime launches the same file to download multiple times to end users. Is there a better way to do this?
One option is to have an IFrame hidden in page with src pointing to zip file to download.
<div style="display:none;">
<iframe id="frmDld" src="http://www.example.com/file.zip"></iframe>
</div>
Another option will be to write the file to response stream when sending the page back(Don't know what you are using on server side. If it is asp.net/mvc then I can help.).
If
Filename (121 MB)
works then this should work too:
<?php
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL= http://www.example.com/file.zip \">";
?>