Solved

don't want .asx file cached

Posted on 2004-10-02
25
617 Views
Last Modified: 2012-06-27
i have something on my site where the viewer checks a list of checkboxes for what songs in my list they would like to hear.

once they click a "play selected" button, i have some php code that generates an .asx file on the fly and then opens it.

if i do another one, a new .asx file is created but internet explorer chooses to open the .asx file that was previously cached. and i know the one that it is supposed to use is correct, but it won't choose to open THAT one.

how can i prevent this from happening? let me know if you desire further explaining.

thanks,
jeremy
0
Comment
Question by:daleyjem
  • 13
  • 7
  • 3
  • +1
25 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Hi daleyjem,

I know a way to do this would be to instead of using one name for the .asx file, attach time() to it, i.e.

$yourasxfilename="eminem".time().".asx";

Then, open up that file. Later, you can have a PHP script that automatically deletes viewed ones already.

Regards,
Zyloch
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
well that was quick...

hmmm.... not too bad of an idea....
but isn't there other ways?
0
 
LVL 27

Expert Comment

by:Diablo84
Comment Utility
Add the following to the top of you script to prevent caching:

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

http://www.php.net/manual/en/function.header.php
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
won't this prevent every thing else on the page to not be cached?.... like images and all that?
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
I'm hoping Diablo's will work, just not sure if it will work even for the .asx file.

If it doesn't, and you can use .htaccess, and you're using Apache, and that Apache has mod_header, you can do this in .htaccess:

Header append Pragma "no-cache"
Header append Cache-Control "no-store,  no-cache, must-revalidate"
Header add Cache-Control "post-check=0, pre-check=0"

(Information on mod_header)
http://httpd.apache.org/docs/mod/mod_headers.html
0
 
LVL 27

Expert Comment

by:Diablo84
Comment Utility
>>  won't this prevent every thing else on the page to not be cached?.... like images and all that?

Put it within the headers used for the generation of the ASP file, you probably have a line which is something like, header ("content-type: .... add the lines there.

I would have imagined you would have had a seperate script that handles outputting the file so not caching images etc wouldn't be a problem.
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
well, i'm not real advanced with php... all of my script is on the same page
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
For mine above about mod_header:

You'd probably want another folder for your dynamic asx files. Then, put that .htaccess in there, or use <Directory> in main .htaccess

0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
please understand i'm pretty novice so a lot of what you're saying may work, but doesn't make sense to me, so a little explaining might help
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Ok, are you hosting this on your own server or a free webhost?
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
it's not free like angelfire or any of them, but it is bought hosting space.

i can edit just about anything
i have an .htaccess file but i'm not sure what it's all about
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
i'm gonna be leaving for about an hour but i'll keep up with the post as much as i can...

thanks for now,
jeremy
0
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

 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Ok. Check to see if they have mod_header. I've never really tried mod_header, but we'll see.

Open .htaccess file there if possible. If you can't, make one locally. Just make sure if you make it locally, upload it ASCI not binary and chmod permissions to 644.

Add this:

<Files "testing.html">
Header append Location "http://www.google.com/"
</Files>

Then create and save a testing.html and access it to test.

0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
i don't see any mod_header thing

...access it to test what?

what are you having me do here?
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Well, the <Files thing I'm telling you to add into .htaccess. any mod_whatever, be it mod_header, mod_redirect, etc, all have to do with .htaccess files
0
 
LVL 14

Expert Comment

by:ThG
Comment Utility
I don't understand the point. You said you have some code generating an asx file. In this case, you MUST have somewhere two header() calls, one for Content-Type and the other for Content-Disposition. You add Diablo84's code there and the cache control refers to THAT specific file (the .asx).

HTML code and images are still cached per default values..
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Doesn't Content-Type hold for the whole file? If you're going to be providing a link to a .asx file, you have to set Content-Type for the link, which I'm not sure how to do. Possibly you have a better idea than me?
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 total points
Comment Utility
Presumably its contained within an if statement if daleyjem has it all in one page.

eg.

if (isset($_POST['play'])) {
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");
 header ("Content-type: video/x-ms-asf"); //nearest mime type i could find for asx
 header("Content-Disposition: attachment; filename=something.asx");
 //etc
}
else {
 //display page
}

Though frankly it would be better to have to asx handling code in a seperate file in my opinion.
0
 
LVL 14

Expert Comment

by:ThG
Comment Utility
Yes, that is exactly what I had in mind. At least that's the idea asker gave me with his description.
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
<?php
if(isset($_REQUEST['playlist'])){
      $filename ="playlist.asx";
      $myFile= fopen($filename,'w+');
      $server_name = "localhost";
      $user_name = "someusername";
      $password = "somepassword";
      $playlistitems=Array();
      $playlistitems=$_POST['checkSelect'];
      fputs($myFile,"<asx version=\"3.0\">\n");
      fputs($myFile,"<Param Name = \"AllowShuffle\" Value = \"yes\" />\n");
      foreach($playlistitems as $playitem){
            $connection = mysql_connect($server_name, $user_name, $password);
            if (!$connection) {
                  printf("Connection error: %s", mysql_error());
                  exit;
            }
            mysql_select_db("someDB");
            $result=mysql_query("SELECT * from list_mp3s where theIndex='" . $playitem . "'");
            $row = mysql_fetch_assoc($result);
            $string="\n<Entry>\n<Title>" . $row['theArtist'] . " - " . $row['theSong'] . "</Title>\n<ref href=\"" . $row['theURL'] . "\"/>\n</Entry>\n";
            fputs($myFile, $string);
            mysql_close();

      }
      fputs($myFile,"\n</asx>");
      fclose($myFile);
      printf("<script language=javascript>window.open('playlist.asx');</script>\n");
}
?>



that is my php code within my page...it basically erases the old playlist.asx file and then rewrites it.
so then the .asx file would look like this:


<asx version="3.0">
<Param Name = "AllowShuffle" Value = "yes" />

<Entry>
<Title>Artist - Song</Title>
<ref href="http://www.mockURLlocation/Music/somesong.mp3"/>
</Entry>

</asx>

so no where in there do i have anything about headers...
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
the .asx file is a separate file... it is only written from another page with that code.

0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
i have an index.php page
it has a form in it with checkboxes.
when you submit... the action directs it to its self with a "request" variable set.
when it re-opens that page it will not recognize the request variable and then perform the routine.
the routine opens a separate file located on my server called playlist.asx
it then writes to that file.
after the file is written to, it is closed.
the index.php page then opens the .asx file that was just written to and it opens up in windows media player or what have you.

is this a bit more clear?
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
sorry...

"when it re-opens that page it will not recognize the request variable and then perform the routine.
"

is supposed to read...

"when it re-opens that page it will NOW recognize the request variable and then perform the routine.
"
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
ahhhhhhhhhh!!!!!!!!

i got it!!!!!!!!!

i understand what you wanted me to do....

so i set my form action to a "playlist.php" file where i set the headers and wrote the .asx file IN that php file...

i didn't understand the "science" of doing all of that

but anyway... it worked thanks to y'all , especially Diablo except instead of an attachment disposition... i made it inline so it would play

ya dig?

alright yall... thanks again
0
 
LVL 4

Author Comment

by:daleyjem
Comment Utility
... and yeah... that's the right mime-type as shown on microsofts support page.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

17 Experts available now in Live!

Get 1:1 Help Now