Solved

delete dynamically created php file

Posted on 2012-04-04
6
319 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 16

Expert Comment

by:Chris Harte
Comment Utility
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
Comment Utility
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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:bartvd
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now