...

View Full Version : Why do my cookies crumble



kingtas
03-16-2008, 06:39 AM
This is not working. The music plays regardless and the cookie doesn't seem to be set. I don't want the music to play if the cookie is set. Help please.


<?php
if (isset($_COOKIE["music"]) == false)
{
setcookie("music", "play", time()-3600, "", "", 1);
PlayMusic();
}
function PlayMusic()
{
echo "<embed src='images/stdr.mp3' hidden='true' autostart='true'>";
echo "<noembed><bgsound src='images/stdr.mp3'></noembed>";
}
?>

Don't laugh. I know I'm a noob. Thanks.

fl00d
03-16-2008, 07:21 AM
You've set the cookie to expire in -3600 seconds. since it's impossible to go back -3600, PHP deletes the cookie. Change the -3600 to +3600 and you should be set.

Also, a quicker way to do your isset statement would be to use
if(!isset($_COOKIE['music']))

It doesn't make a difference but its less to type. Whichever you prefer :p

kingtas
03-16-2008, 09:39 PM
Thanks for the reply. I have made the change you suggested but it plays the sound every time the page is navigated to or refreshed, instead of the first visit only. When i add an echo $_COOKIE["music"]; it doesn't display "play" on the screen either. Any ideas. Thanks.


<?php
if(!isset($_COOKIE['music']))
{
setcookie("music", "play", time()+3600, "", ".shiftoneballers.com", 1);
PlayMusic();
}

function PlayMusic()
{
echo "<embed src='images/stdr.mp3' hidden='true' autostart='true'>";
echo "<noembed><bgsound src='images/stdr.mp3'></noembed>";
}
?>

_Aerospace_Eng_
03-16-2008, 11:45 PM
Hmm make sure you turn on full error reporting. Its likely you are trying to create the cookie after any output has been sent to the browser. If this is the case then you should be getting a headers already sent error.

Add this to the top of your code

error_reporting(E_ALL);

kingtas
03-17-2008, 12:20 AM
I added that line first and get no errors reported. Only if I add this line next do I get errors.

echo $_COOKIE["music"];

Something about that line and headers already sent at setcookie line.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum