Solved

delete dynamically created php file

Posted on 2012-04-04
6
321 Views
Last Modified: 2012-05-02
I use jquery ajax to have php write a file that populates a popup. Dynamic database stuff from php. I'd like to delete the html file when the user closes the window, or after a couple minutes. What is the best way to do this? onbeforeunload just seems unreliable. And it seems chrome totally blocks access to ajax calls made with onbeforeunload, so there's no calling another file like

onbeforeunload="cleanUp(filename)" 

function cleanUP(filename) {
//ajax call cleanUp.php and send the filename
}
//cleanUp.php
$filename = $_POST['filename']; 
unlink($filename)

Open in new window


Also, is there a way to have php write php? So that at the top of the dynamically created file, I can do a php cookie check, so that only authorized users can see the page. Whenever i have something like

$stringToWrite = "<?php if ( !isset ($_COOKIE['key']) ) { exit(); }?>" 

Open in new window


php gives me some WHITESPACE error and dreamweaver tells me there's an error in my code (is this best software for writing php by the way)

Or should I make a copy of the php cookie with javascript, and have javascript do the check?

Thanks!
0
Comment
Question by:hibbsusan
6 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 37806704
Cant help you with the ajax but make sure the file is not in use and has been closed before you try to delete it.

To handle users to your page, authorised or not, you use sessions

http://uk3.php.net/manual/en/book.session.php

You will get a lot of answers to which is the best IDE, though I do not think anybody will say Dreamweaver. Personally and professionally I use Eclipse PDT, which is free.

http://www.eclipse.org/pdt/downloads/
0
 
LVL 6

Expert Comment

by:bartvd
ID: 37807095
Do you really need to save te html page? I think you can create the html page with php and just echo it, you don't need to save it, so you don't have to delete it.
0
 

Author Comment

by:hibbsusan
ID: 37807120
Okay. Say for example I have a string with all the content I want in the popup. It includes links to an external javascript file and a stylesheet just FYI. How can I get that into a popup?

Thanks
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 6

Expert Comment

by:bartvd
ID: 37809650
If you make a javascript ajax call to popup.php

<?php
$content="yourwholehtmlfile";
echo $content;
?>
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 250 total points
ID: 37809683
As 'popup.php' doesn't exist in this scenario (wasn't that your own suggestion Bart?) I was thinking along these lines:

<html>
<?
	$content = "<html><body>yourwhole<em>html</em>file</body></html>"
?>
<head>
<script type="text/javascript">
function pop() {
	var content='<? echo str_replace("'", "\\'", $content) ?>';
	var pw = window.open("", "pw", "left=100,top=100,width=200,height=200");
	pw.document.write(content);
}
</script>
</head>
<body>
  <button onclick="pop()">pop!</button>
</body>
</html>

Open in new window


Not a 100% sure about the escaping but it should be close... Also, there may be easier ajax/jquery code for that.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37809868
I am not a fan of Dreamweaver for writing PHP code.  There are IDEs like Eclipse if you like those things.  Or you can use Notepad++, Textpad, BBEdit, etc.  My prejudice against Dreamweaver arises from the fact that a great many questions I see at EE indicate that the programmer is trying to learn PHP by reading Dreamweaver-generated PHP code.  This is like trying to learn to bake by looking at an apple pie.  It's never going to happen; the outcome will be no better than trial and error.  The Dreamweaver PHP code is terribly badly written and the novice programmer has no clue that they are looking at an unmaintainable mess.

That aside, it sounds like what you want is a thing called a "garbage collector."  PHP already has a garbage collector built in - it's part of the session initialization module.  So instead of writing a file and building your own garbage collector, you might try just using the PHP session to hold the data you need. The data will persist across requests until the client closes the last instance of the browser or the data expires from lack of use.   With the AJAX part of things you may find that you need to use session_write_close() where you would not otherwise expect to need it - not sure about that.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question