Solved

Random flash swf movie

Posted on 2009-05-11
19
1,148 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:killyman
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24359664
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
0
 

Expert Comment

by:jaldhous
ID: 24359700
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

0
 
LVL 1

Expert Comment

by:squr3l
ID: 24359712
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

0
 

Author Comment

by:killyman
ID: 24360841
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

0
 
LVL 1

Expert Comment

by:squr3l
ID: 24361020
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

0
 

Author Comment

by:killyman
ID: 24398740
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?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24400007
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
0
 

Author Comment

by:killyman
ID: 24400240
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
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24400265
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:killyman
ID: 24401300
Bol,

This is the link:

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

Thanks,
Clark
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 24418325
The PHP below will work for what you need.  Let me know if you have a question.  It shows how to create the cookie and work with it.
bol

<?php

$aMovies = array("flash_intro.swf",

	"flash_intro2.swf",

	"flash_intro3.swf");
 

$days = 30;
 

if (!isset($_COOKIE["swfMovie"])) {

	setcookie("swfMovie", "0", time()+60*60*24*$days);

	$nMovie = 0;

} else {

	$nMovie = (int) $_COOKIE["swfMovie"];

	$nMovie = ($nMovie == 2)? 0 : $nMovie + 1;

	setcookie("swfMovie", $nMovie, time()+60*60*24*$days);

}

?>

Open in new window

0
 

Author Comment

by:killyman
ID: 24419494
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>


0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24423641
>> 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
0
 

Author Comment

by:killyman
ID: 24424456
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

0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24425194
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
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24425223
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
0
 

Author Comment

by:killyman
ID: 24437586
Thank you very much bol!!! The script works perfectly. Great job!
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24443860
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
0
 

Author Comment

by:killyman
ID: 24444181
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!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

760 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

23 Experts available now in Live!

Get 1:1 Help Now