Solved

delete dynamically created php file

Posted on 2012-04-04
6
325 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 110

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

621 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