Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete dynamically created php file

Posted on 2012-04-04
6
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

730 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