Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Why do my cookies crumble

    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.

    Code:
    <?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.

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    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

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <?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>";
    }
    ?>

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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
    PHP Code:
    error_reporting(E_ALL); 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.
    Last edited by kingtas; 03-16-2008 at 11:30 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •