Solved

delete dynamically created php file

Posted on 2012-04-04
6
323 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

710 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