Using Cookie to show flash movie once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gurdip
Forum Newbie
Posts: 4
Joined: Sun Mar 22, 2009 7:26 am

Using Cookie to show flash movie once

Post by gurdip »

Hi,

I'm new at this.

I created a flash movie that I need to show to each website visitor once in 24 hours (ie cookie expires in 24 hrs). I created the following php file but the flash doesn't show (in the html version it does); but upon reloading the page (since the cookie is now set), I do see the test message "Cookie is set, so Flash won't show". Am I using the escape character incorrectly? Or is something else the issue? Couldn't find an answer on the forum.

Code: Select all

<? ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
 
<script type="text/javascript" src="jquery126.js"></script>
</head>
<body>
 
 
<?php if (!isset($_COOKIE["showflashonce"])){
    setcookie("showflashonce","showflashvalue",time()+86400);
 
    echo "<div id=\"toppanel\" style=\"text-align:center;position:absolute;z-index:20;width:100%;height:450px;display:none;\" ><object width=\"1000\" height=\"400\" style=\"background:#000\"><param name=\"movie\" value=\"http://www.mydomain.com/ms.swf\"><param name=\"wmode\" value=\"transparent\" /><embed src=\"http://www.mydomain.com/ms.swf\" width=\"1000\" height=\"400\"></embed></object>";
    
    echo "<div><a href=\"#\" class=\"toppanelclose\" style=\"padding: 0 30px 10px 30px; background: #af0000;color:#fff;border:2px solid #af0000; font-weight: bold;\">Close [X]</a></div></div>";
    
    echo "<script>var cl=0;\$(document).ready(function() { \$(\'#toppanel\').slideDown(800); window.setTimeout(function() { if(!cl) \$(\'#toppanel\').slideUp(); }, 25000); \$(\'a.toppanelclose\').click(function(){\$(\'#toppanel\').slideUp();cl=1;return false;}); });</script>";
}
else {
    echo "Cookie is set, so Flash won't show.";
}?>
 
 
</body>
</html>
<? ob_flush(); ?>
I've used the \ escape character for the double and single quotations and for $. What have I done wrong?

Thanks in advance.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using Cookie to show flash movie once

Post by php_east »

take out your buffering

Code: Select all

<? ob_start(); ?>
<? ob_flush(); ?>
and, set error level to maximum at the top.

Code: Select all

error_reporting(E_ALL);
and hopefully you will see why it does not go.

once you are happy turn errors off

Code: Select all

error_reporting(0);
gurdip
Forum Newbie
Posts: 4
Joined: Sun Mar 22, 2009 7:26 am

Re: Using Cookie to show flash movie once

Post by gurdip »

Thanks. That helped me realise that I shouldn't have an escape character before a single apostrophe. I amended the last "echo" statement to read like this now:

Code: Select all

echo "<script>var cl=0;\$(document).ready(function() { \$('#toppanel').slideDown(800); window.setTimeout(function() { if(!cl) \$('#toppanel').slideUp(); }, 25000); \$('a.toppanelclose').click(function(){\$('#toppanel').slideUp();cl=1;return false;}); });</script>";
The flash now shows but the cookie does not work. Í see this error when I load the page:

Warning: Cannot modify header information - headers already sent by (output started at /home/mgenesis/public_html/showonce.php:6) in /home/mgenesis/public_html/showonce.php on line 12

Line 12 is:

Code: Select all

setcookie("showflashonce","showflashvalue",time()+86400);
Strange; this line was working earlier and I saw the cookie on my pc- now it doesn't and the flash shows up on each page load. What else can I do to fix this? Í read that this error has to do with whitespace but I can't find any. I'm using Crimson editor if this matters.

gs
gurdip
Forum Newbie
Posts: 4
Joined: Sun Mar 22, 2009 7:26 am

Re: Using Cookie to show flash movie once

Post by gurdip »

Solved!

I added the following again and it worked!

<? ob_start(); ?>
<? ob_flush(); ?>

Not quite sure why, though!

Anyway, thanks for your help - lead from one thing to another :)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Using Cookie to show flash movie once

Post by php_east »

gurdip wrote: Not quite sure why, though!
because headers are already sent before your php, so you need to buffer the output
to get to the cookies.

but in order to see errors, you have to turn off buffering. otherwise, you don't see nothing and will be wondering why nothing happens.
when you are happy with everything, turn buffering on again, and turn errors off, else you may get some small warnings which will not be good for your users to view.

this buffering should rightly be at the php where the headers begin, you are lucky it works here.
gurdip
Forum Newbie
Posts: 4
Joined: Sun Mar 22, 2009 7:26 am

Re: Using Cookie to show flash movie once

Post by gurdip »

Thanks for your explanation. Was struggling with the issue for a couple of days until I found this forum.

gs
Post Reply