Link to home
Start Free TrialLog in
Avatar of killyman
killyman

asked on

Random flash swf movie

Hi,

I need help to create or use a script that will randomly load three or four swf files in my main homepage. The idea is that when a visitor comes back again, he/she may see a different swf movie from the one seen on the first visit.

Is there a simple way to do this that will work wtih FF, IE and Safari browsers?

Thanks,
Clark
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

killyman,

You would need to use a cookie to "store" the fact the visitor has been there before and which swf file they saw last (so no repeats).  Without cookies there is no way to do it with the situation you described.  One key since you mentioned different browsers is the cookie is browser specific.  If a user came to your site in IE and then FF it would seem the FF visit was the first.  There is no way to avoid that without requiring some login.

Can you use a server language?  Are your pages just htm or are they some server page (e.g. PHP, .NET, ASP)?  The method of making the cookie, reading the value, changing it value and setting the html for the swf file will depend on whether you use Javascript or some server language.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of jaldhous
jaldhous

Here is some example JavaScript code.

I hope this helps.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body bgcolor="#FFFFFF" text="#000000">
 
</body>
<script language="JavaScript">
 
// Generate a Random Number
var randomnumber = Math.round(Math.random()*3);
 
// Select a movie and execute the corresponding function
if (randomnumber == 1)
 
{movie1();}
 
else if (randomnumber == 2)
 
{movie2();}
 
else {movie3();}
 
//Functions to write out the correct flash movie resource.
 
function movie1(){
document.write("<object classid=\"clsid27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\" width=\"300\" height=\"200\"><param name=movie value=\"xxxxxxxxxxxxxxxxx"><param name=quality value=high><embed src=\"xxxxxxxxxxxxxxxxx" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"200\"></embed></object>")
}
 
function movie2(){
document.write("<object classid=\"clsid27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\" width=\"300\" height=\"200\"><param name=movie value=\"xxxxxxxxxxxxxxxxx"><param name=quality value=high><embed src=\"xxxxxxxxxxxxxxxxx" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"200\"></embed></object>")
}
 
function movie3(){
document.write("<object classid=\"clsid27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\" width=\"300\" height=\"200\"><param name=movie value=\"xxxxxxxxxxxxxxxxx"><param name=quality value=high><embed src=\"xxxxxxxxxxxxxxxxx" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"200\"></embed></object>")
}
 
</script>
 
</html>

Open in new window

i recommend use of php to do this,
in the flash code i recommend you enter the height and width of the movies if they are all to be the same, but if they are different sizes you should be fine with removing the height and width attributes from the code altogether
you will need to rename your homepage file from .html to .php if it isnt already and place the following code where you want the video
if you already have a video in place, you can just copy the php code and place it above the flash, then insert <?=$movie; ?> in the flash code where it asks for the file name
hope this helps
<?php
$random = ($rand()%4);
switch ($random) {
  case 1:
    $movie = "movie1.swf";
    break;
  case 2:
    $movie = "movie.swf";
    break;
  case 3:
    $movie = "movie3.swf";
    break;
  case 4:
    $movie = "movie4.swf";
    break;
}
?>
<object>
    <param name="movie" value="<?=$movie; ?>" width="" height=""/>
    <param name="quality" value="high" />
    <param name="wmode" value="opaque" />
    <param name="swfversion" value="6.0.65.0" />
    <!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you dont want users to see the prompt. -->
    <!-- <param name="expressinstall" value="Scripts/expressInstall.swf" /> -->
    <!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="<?=$movie; ?>" width="" height="">
      <!--<![endif]-->
      <param name="quality" value="high" />
      <param name="wmode" value="opaque" />
      <param name="swfversion" value="6.0.65.0" />
      <param name="expressinstall" value="Scripts/expressInstall.swf" />
      <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
      <div></div>
      <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
  </object>

Open in new window

Avatar of killyman

ASKER

Thank you everyone for the quick responses.

SQUR3L: I tried your idea by placing just the PHP code above my flash embed tag and changing the flash source name to <?=$movie; ?> in the flash code where it asks for the file name. I changed the extension of my page to .php too. However, I get the following error.

Fatal error: Function name must be a string in /home/path/www/www/beta3/index2.php on line 150

I'm attaching the code snippet in this reply.


B0LSC0TT: I like your idea to use cookies and have returning visitors see the remaining swf files in a specific order. Right now I have four swf files total that I would like to try arranging in order for returning visitors. I can use PHP on my web server. Can you help me with the code?

Clark

<?php
$random = ($rand()%3);
switch ($random) {
  case 1:
    $movie = "flash/flash_intro.swf";
    break;
  case 2:
    $movie = "flash/flash_intro2.swf";
    break;
  case 3:
    $movie = "flash/flash_intro3.swf";
    break;
 
}
?>
 
               
               <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="804" HEIGHT="270" id="<?=$movie; ?>" ALIGN="">
<PARAM NAME=movie VALUE="<?=$movie; ?>"> <PARAM NAME=quality VALUE=high> <param name="wmode" value="opaque"> <EMBED src="<?=$movie; ?>" WIDTH="804" HEIGHT="270" ALIGN="" quality=high bgcolor=#ffffff NAME="flash_intro" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque"></EMBED> </OBJECT> 

Open in new window

i do not know if this will fix the error that is being thrown, but if you are running php older than 4.2.0 you will need to add another line of code before the $random
so your first 3 lines would look like this.
this could only fix the error if you are not running up-to-date php, so if you know you are current, it may not be of use to you
good luck
<?php
srand(time());
$random = ($rand()%3);

Open in new window

squr3l, I have PHP 5 on my server, so your suggested fix will probably not help. Thanks anyway.

I like b0lsc0tt's idea the best which was to use cookies and have returning visitors see the remaining swf files in a specific order.

Right now I have four swf files total that I would like to try arranging in order for returning visitors. I can use PHP on my web server.

Can anyone help me with the code to accomplish this?
Clark,
Sorry I have not responded earlier.  I can help you with code for this.  I have just not had the free time to make it yet.  I doubt I will today but should be able to tomorrow, unless another expert provides it first.  I just wanted to follow up at least so you knew I wasn't gone for good or missing your responses.
bol
bol,

Thanks for responding. I can wait. Let me now if you need to see what I have up now to give you an idea of what I'm working with.

Clark
Clark,
That would help and might make it so my code is less of an example and more of a solution for your page.  If the page is really complicated or has sensitive info then don't worry about providing it.  I can make a working solution with what I have here.
bol
Bol,

This is the link:

http://www.pathoftheblueeye.com/beta3/index.html

Thanks,
Clark
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much bol!

A few questions...

1.  What code do I need to put in the cookie file?

2. Where do I place the PHP code you provided me with?

3. Do I need to change/remove the current SWF code (see code below)?

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="804" HEIGHT="270" id="flash_intro" ALIGN="">
<PARAM NAME=movie VALUE="flash/flash_intro.swf"> <PARAM NAME=quality VALUE=high> <param name="wmode" value="opaque"> <EMBED src="flash/flash_intro.swf" WIDTH="804" HEIGHT="270" ALIGN="" quality=high bgcolor=#ffffff NAME="flash_intro" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque"></EMBED> </OBJECT>


>> What code do I need to put in the cookie file? <<
I don't really know what you are asking.  A cookie doesn't keep code.  In my recommendation the cookie keeps the value for the last swf movie used.  It does this by storing the key of that movie's location in the array (i.e. 0, 1, or 2 in my example).  Please clarify your question if this doesn't answer it.
>> Where do I place the PHP code you provided me with? <<
I should've mentioned this.  That code needs to go before any html or before your echo/print anything.  It even has to go before lines that might set the http header.  The reason is for the cookie to be set it has to be there.
Another detail I should've provided in the code is $days can be changed by you.  It sets how long the cookie is saved.  Don't get carried away but you can increase it if you feel the visitor may not visit that regularly.
If you have more than 3 movies not only would you need to change the array (to add the other file names) but you would need to modify the line below:
$nMovie = ($nMovie == 2)? 0 : $nMovie + 1;
I can give you details if this could be an issue.
>> Do I need to change/remove the current SWF code (see code below)? <<
Is that code exactly the same besides the movie file's name?  If so then no you don't have to remove the code and the change would be very minor.  Where you have the movie's "URL" you would put the code ...
"flash/<?php echo $aMovies[$nMovie] ?>"
Double check the resulting html to make sure the result looks right but that should write the file name correctly (with path).  Since you use the PHP variable and echo it will be dynamic and change based on the earlier PHP code.
Let me know how this helps or if you have a question.
bol
Okay. I've got it up and running and it seems to be working fine. Great Job!!!
Here is the link: http://www.pathoftheblueeye.com/beta3/index.php

Now, if I want to add another movie to the equation I would just need to change the array (to add the other file name) and the  "$nMovie" code?

For example, I want  to have 4 movies total instead of 3 movies, I would change the code to???
$nMovie = ($nMovie == 3)? 0 : $nMovie + 1;

Thanks,
Clark

Clark,
Those are the changes you would need to make.
I am glad this worked for you and I could help.  Thanks for the fun question, the grade and the points.
bol
Oops.  Disregard the last part.  I thought this was closed when I responded to your comment.  I confused it with another question.  I am still glad it is working. ;)
bol
Thank you very much bol!!! The script works perfectly. Great job!
Your welcome!  I am glad I could help.  With the other great suggestions and posts I wasn't sure you would decide on mine. :)  Thanks for the interesting question!
bol
b0lsc0tt, your code seemed like the best choice for my needs especially with the cookie feature.

I want to thank both jaldhous and squr3l, too for your prompt responses and suggestions. Thanks!