Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Play Audio Once Per Session
Old 01-09-2008, 12:31 PM Play Audio Once Per Session
Junior Talker

Posts: 3
Trades: 0
I need a script that will play an mp3 file once per visitor session. In other words, the audio will play on the home page when the visitor first accesses it, then if the visitor goes to another page and comes back to the home page, it doesn't play again. If the visitor closes his browser, then opens his browser again and visits the home page, the audio plays. Can anyone help me with this. Thanks!
clomper is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-12-2008, 08:20 PM Re: Play Audio Once Per Session
Novice Talker

Posts: 6
Name: Marion Sudvarg
Trades: 0
It sounds like this task would be best done with PHP. First, you will need to make sure the server with your website has PHP. Upload a file called phpinfo.php with the following code:

Code:
<? phpinfo(); ?>
Point your browser to this file. If your server has PHP, your browser will display a big table of information. At the top, it will give you the PHP version. The following information is for PHP 4.1 or later.

Now, the next thing to do is to make sure that your home page has the proper extension. Rename the file so that, instead of ending with .html or .htm or the like, it ends with .php.

Now you have two options. You can either set this up with a cookie or with a PHP session. Cookies are easier to implement, but some people set their computers to not allow cookies, so using a PHP session is a better method. In this post I will explain how to use cookies to do what you want. In the future, if I have time, I will write another post describing how to do the task with a PHP session.

Cookies:

Insert the following code at the top of your home page. It must be at the very, very top, above even an empty line or a space in the code.

Code:
<?php setcookie("firsttime","no") ?>
Then, in the body of your document, where you want the music file to go in, put the following code:

Code:
<?php
$firsttime = $_COOKIE[firsttime];
if (@$firsttime != "no") { echo "<embed src='yourmp3.mp3' />"; }
?>
You can, of course, mess with the embed tag to change the style of how the music player is displayed. For example, putting style='width:0;height:0' in the embed tag will effectively hide your audio player, but the audio will still play after it loads. Just be sure that any quotes you put within the echo statement are either single quotes (') or escaped (\").

When the user closes their browser, the cookie is removed, so when the browser is reopened, the music file is played again. But just navigating to a new page doesn't remove the cookie, so when the home page is reaccessed, the browser will see the cookie and not play the music.

I hope this helps.

Marion Sudvarg
__________________
My Website: sudvarg.com
Other Websites I've Designed: sudvarg.com/web
Therion is offline
Reply With Quote
View Public Profile Visit Therion's homepage!
 
Old 01-12-2008, 11:12 PM Re: Play Audio Once Per Session
Extreme Talker

Posts: 238
Location: United States
Trades: 0
I like the idea of doing it server-side (e.g., in PHP) better, but it's still possible to do it in JavaScript.

The steps are something like this in javascript:
Code:
check for a cookie
if there is not a cookie
   create a cookie (don't set an expiration date; this means the cookie will delete when the browser is closed)
   write the audio code to the document
For information on setting and getting cookies in javascript: http://www.w3schools.com/js/js_cookies.asp
frost is offline
Reply With Quote
View Public Profile
 
Old 01-13-2008, 03:28 AM Re: Play Audio Once Per Session
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Why are you guys thinking cookies?
1 - He wants it to play EVERY TIME THE BROWSER IS RE-OPENED. Cookies stay until they expire. True, you can make them expire at the end of the browser SESSION but guess what.
2 - Cookies don't work for everyone.
3 - Anyone can change a cookies value therefore rendering the whole thing useless.
So just use a session :P.
Here's a simple script to do this in PHP -
PHP Code:
<?php
session_start
();
if (
is_null($_SESSION['first_time']))
{
$_SESSION['first_time'] = 'Nope :D';
}
else
{
// It's their first time - Play the music...
print "<embed src='/mp3/file.mp3' />";
}
?>
If you want to do it in JavaScript you'll have to use cookies because JS is client-side and therefore is ran after the "server side cookies" (AKA session variables) are already said and done. Also many people have JS disabled and JS isn't very well supported in older browsers :P also JS will work differently in different browsers (not very differently but there are slight differences).
__________________
PHP Code:
$talkupation++; 

Please login or register to view this content. Registration is FREE
- Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-13-2008, 06:59 AM Re: Play Audio Once Per Session
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Quote:
Originally Posted by Mattmaul1992 View Post
Why are you guys thinking cookies?
1 - He wants it to play EVERY TIME THE BROWSER IS RE-OPENED. Cookies stay until they expire. True, you can make them expire at the end of the browser SESSION but guess what.
2 - Cookies don't work for everyone.
3 - Anyone can change a cookies value therefore rendering the whole thing useless.
1. Cookies without an expiration date expire when the browser is closed. Therefore, when the browser is reopened, the cookie is gone.
2. PHP sessions use a cookie to store the session id. So if I have cookies disabled, your PHP sessions aren't going to work either.
3. Since PHP sessions use cookies, the user could render that useless as well. Yes, cookie tampering is something web developers should always keep in mind, but this sounds like a feature that isn't really that important if the user tampers with the cookie.


Quote:
Originally Posted by Mattmaul1992 View Post
If you want to do it in JavaScript you'll have to use cookies because JS is client-side and therefore is ran after the "server side cookies" (AKA session variables) are already said and done. Also many people have JS disabled and JS isn't very well supported in older browsers :P also JS will work differently in different browsers (not very differently but there are slight differences).
In some cases, JS is less supported in older browsers. However, we are talking about simple cookies and if statements which have been supported in all browsers since, I think, at least IE 4 and Netscape 3.

You do make a good point that many people have JS disabled; I agree, and that's why I mentioned that I like doing this server-side better.
frost is offline
Reply With Quote
View Public Profile
 
Old 01-13-2008, 01:26 PM Re: Play Audio Once Per Session
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Yer... I just flat out hate using JS for stuff you could do in PHP and using cookies when you could use sessions. Didn't really think that one out. Thanks frost :P.
__________________
PHP Code:
$talkupation++; 

Please login or register to view this content. Registration is FREE
- Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-15-2008, 12:45 PM Re: Play Audio Once Per Session
Junior Talker

Posts: 3
Trades: 0
You guys rock. I implemented Therion's code and it works like a charm. There is one hitch, though. When a visitor with IE first visits the site, the information bar drops down and asks if they want to run Windows Media Core activex, or something like that. After agreeing and running it the page refreshes, and since the cookie has be set for the initial contact session to the page, the audio doesn't play. Only after closing the browser and reopening it will the audio play without any glithces. Thanks a lot Micro$oft! Any solutions around this??
clomper is offline
Reply With Quote
View Public Profile
 
Old 01-28-2008, 06:00 PM Re: Play Audio Once Per Session
Novice Talker

Posts: 7
Name: David Hodgson
Trades: 0
Hi,

Just wanted to ask a question regarding this.

I have just been asked to do exactly this by my employer and the code works a treat. My only problem is that the media controls are removed from the page when the viewer goes back to the home page.

I need to give the viewer the option of playing the audio file again if they choose without having to close the browser.

Is there a way of modifying the code so that the controls (i.e. play, stop etc) remain on the page.

I look forward to a response.

Regards
dhodgson1980 is offline
Reply With Quote
View Public Profile
 
Old 07-06-2008, 12:39 AM Re: Play Audio Once Per Session
Novice Talker

Posts: 6
Name: Marion Sudvarg
Trades: 0
Quote:
Originally Posted by dhodgson1980 View Post
Hi,

Just wanted to ask a question regarding this.

I have just been asked to do exactly this by my employer and the code works a treat. My only problem is that the media controls are removed from the page when the viewer goes back to the home page.

I need to give the viewer the option of playing the audio file again if they choose without having to close the browser.

Is there a way of modifying the code so that the controls (i.e. play, stop etc) remain on the page.

I look forward to a response.

Regards
I hope this is still useful for you. I haven't checked in here in a while.

I said before to use the following code:

PHP Code:
<?php
$firsttime 
$_COOKIE[firsttime];
if (@
$firsttime != "no") { echo "<embed src='yourmp3.mp3' />"; }
?>
But if you want people to be able to replay the audio, consider replacing that code with this code. It implements a bit of javascript:

PHP Code:
<?php
$firsttime 
$_COOKIE[firsttime];
if (@
$firsttime != "no") { echo "<embed src='yourmp3.mp3' />"; }
else {
echo 
"
<script type='text/javascript'>
function display (id) {
    var display = document.getElementById(id).style.display;
    if(display == "
none") {    document.getElementById(id).style.display="block"; }
    if(display == "
block") { document.getElementById(id).style.display="none"; }
    return false;
}
</script>
<a href='' onclick='return display('musicplayer')'>Click here to display the music player</a>
<div id='musicplayer' style='display:none'>
<embed src='yourmp3.mp3' />
</div>
"
;
}
?>
I hope I didn't have any errors in that code, but I didn't test it, so that's not unlikely.

Marion Sudvarg
__________________
My Website: sudvarg.com
Other Websites I've Designed: sudvarg.com/web
Therion is offline
Reply With Quote
View Public Profile Visit Therion's homepage!
 
Reply     « Reply to Play Audio Once Per Session
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29685 seconds with 12 queries