How to close php page from browser after executing the code? - php

I have a code in php which I want to schedule in task scheduler. But when this page runs it opens tab in my browser window. I want to close it anyway after execution or is there any way to run php page without opening tab in browser?
I have tried window.close(); but it's also not working.

You can use cURL. to run code in background or without opening browser tab.
see documentation cURL

It should be like:
filename.php
<?php
echo "I am here";
//here some php code
//adding close browser tab code
?>
<script>
window.close();
</script>
<?php
//here some php code
?>

Just run the script from your command line. There is no need to involve a browser at all.
http://php.net/manual/en/features.commandline.usage.php

Related

PHP echo and download pdf headers

Im in a situation where users can click a download button in a pop up. When they click "download", a request is made to my php script. The php script generates the PDF, echoes a script tag to close the modal and then downloads the PDF. I dont want to do a setTimeout to close the modal because of the timing differences for the PDF to be generated, which is why im echoing the script tag to close the modal. I've tried using ob_start/ob_end_flush but it still wont work. help! Thanks :)
The file gets downloaded, but the script tags wont get echoed. If I comment out the header & readfile, the script tags get echoed, so I know its just because output stops or something.
ob_start();
echo('<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>');
echo('<script>jQuery("body", window.parent.document).find("#pdfModal").addClass("out").removeClass("in")</script>');
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='results.pdf'");
readfile('../wp-content/plugins/ajaxpostfromfront/' . $token . '/results.pdf');
ob_end_flush();

Run Qt generated executable (EXE) through php (LINUX)

Sorry, this question must be repeated, it already asked the following link, but the answer is not cleared.. So can anyone please solve this problem???
exe not giving output in php
I am trying to call a Qt generated executable file through PHP code which fails to run the exe.. The same exe runs on double-click and through command line..
Below is my code
<?php
$exec_cmd = exec('"./myEXE"');
?>
<html>
<body>
<form>
<input type="submit" value="RUN" onclick="$exec_cmd"/>
</form>
</body>
<html>
Thank You...
PHP is a server side language. It means that the php code is compiled and executed in the server and the output is sent to the client.
What you are trying to do is to use PHP as client-side language, like JavaScript.
The porgram is executed when the page is displayed and you want it to be displayed once the user clicks on the button, for that you must use JavaScript.
I would use AJAX to make the request to the server so it can execute the program but if you don't want to use javascript you can set the form to redirect the page with a GET or POST parameter and check in PHP if that parameter is set

PHP: How to open a new window in JS and echo from PHP into it

for debugging I reasons I use very often PHP function var_dump()
<pre>
<?php
var_dump($myVariablesArray);
?>
</pre>
but I need to output its contents (and something more I use for my debugging) into a new popup window.
There are several examples about opening a new JS window, but I cannot find anything helping me opening a new window and printing from PHP into it, all this automatically when the page I'm debugging is loaded.
Any hint?
you could var_export() instead maybe. And then append it to the beginning of a txt on the server. Then you can have a little script in the second window that refreshes every 5 seconds, and shows that txt file. Much like a log.
http://www.php.net/manual/en/function.var-export.php
pro: you can keep the txt between runs, and save the outputs.
con: the format is a bit different than var_dump()
I solved the problem thanks to suggestion on Add content to a new open window
On the page I want to debug, where I need to, I add following code (if session is not started I need to add "session_start();" ):
<?php
$_SESSION['varsLog'] = "<pre>".htmlspecialchars(print_r($myVariablesArray))."</pre>\n";
?>
<script type="text/javascript">
$(document).ready(function () {
var OpenWindow = window.open("/empty.html", "phpLog", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1400,height=640');
});
</script>
and then I have the empty.html page on the root of my domain:
<?php session_start(); if (!isset($_SESSION['varsLog'])) { exit(1); } ?>
<html><body>
<?php
echo date(DATE_RFC822);
$refer = $_SERVER['HTTP_REFERER'];
echo "<br><br>Variables from ".$refer."<br><br>";
echo $_SESSION['varsLog'];
unset($_SESSION['varsLog']);
?>
</body></html>
This way I can add the first code snippet to any page I need to debug, every time I load those pages the new window I previously opened will be just updated with the variables from the last page loaded, with a useful referring url and time stamp to be sure.
Thanks for help to everyone!

which one is executed first

I have a web application in php, I don't know which one will be ever called an processed first,
In my php file, I also have a javascript code
<script>
$(document).ready(function(){});
</script>
In my php code, I will send an array object into that javascript. So I wonder why the php code is executed first instead of the javascript ? Does it mean that all server code will always be executed before the client script runs during the browser view is shown.
The server side code is executed first and the output generated by server side is sent back to client where client side code is executed.
Yes, normally, the whole server processing finished before the page is delivered to the browser. At this moment, JavaScript execution starts.
You can add late execution of PHP code using AJAX.
Of course, the server code (be it PHP,ASP,JSP,etc) runs first in the server, it generates an html page which contains your javascript code, your computer receives this page, renders it in the browser and runs the javascript in it.
if you have html,php,js in a single file you should know these two things only:
1)your file extension must be .php (because php execution required .php extension)
2)you will see the output exactly in the same order as you typed in your file.
<html>
<body>
<?php
echo "i m php upper"."</br>";
?>
<p id="pg">i am html upper</br></p>
<script type="text/javascript">
document.write(" i am javascript upper</br>");
</script>
<?php
echo "i m php bottem"."</br>";
?>
<script type="text/javascript">
document.write(" i am javascript bottom</br>");
</script>
<p id="pg">i am html bottem</br></p>
</body>
</html>
...output looks like this...
i m php upper
i am html upper
i am javascript upper
i m php bottem
i am javascript bottom
i am html bottem

Help Using Javascript Code in a php file

I have the code below in a .php file on my site. I would like to be able to type the link to the .php file in the address bar, and for the php script to run on the current page. How could I do this?
PHP File:
<SCRIPT language=JavaScript>
javascript:document.body.contentEditable='true'; document.designMode='on';void(0)
</script>
The way you suggest to do it it's not possible, but you can have a "bookmarlet" which is javascript code that you drag and drop to your bookmarks bar, and every time you click on it, it runs a script.
Try it here: http://jsfiddle.net/XtHsP/

Categories