miguelfsf Posted November 8, 2014 Share Posted November 8, 2014 Hello, I have a song streaming website, and i want to set a cookie inside a player popup to avoid that users increment the number of times that an album was played every time they access it. I want the cookie to last at least 6h, but every time i test the code with 1,2 or 5 minutes it never ends in the time that i specify. For example i want it to last 3min and it may take 5 or 2min to end. Cant figure out why, because i used the same cookie code in a none popup window and it worked. // player.php (POPUP) <?php session_start(); include("include/player-functions.php"); uniqueAlbumListens($db,$_GET['id']); (...) ?> //player_functions.php <?php (...) function uniqueAlbumListens($db,$id){ if(!isset($_COOKIE["ml".$id])){ updateListens($db,$id); } setcookie("ml".$id, "listens", time()+60*2); // 2minutes -> 60*2 ? || 6h -> 60*60*6 ? } (...) ?> Quote Link to comment https://forums.phpfreaks.com/topic/292366-cookie-not-working-properly-inside-popup-window/ Share on other sites More sharing options...
ginerjm Posted November 9, 2014 Share Posted November 9, 2014 C'mon. Really? You expect us to debug your code with such a small sample of WHAT? None of this makes sense to us. One hint. You do realize that a cookie is not readable (after being set) until the page is refreshed, ie reloaded, right? So if you are setting is at the top of a script and then trying to read it a second later you're not going to see it. Quote Link to comment https://forums.phpfreaks.com/topic/292366-cookie-not-working-properly-inside-popup-window/#findComment-1496132 Share on other sites More sharing options...
miguelfsf Posted November 9, 2014 Author Share Posted November 9, 2014 (edited) // player.php (popup) <?php session_start(); include("include/player-functions.php"); uniqueMixtapeListens($db,$_GET['id']); if(!isset($_GET['id'])){ header('location: index.php?p=home');exit(); } ?> <!DOCTYPE html> <html><head><title>Player</title></head> <body> // player code </body> </html> //player_functions.php <?php function uniqueAlbumListens($db,$id){ if(!isset($_COOKIE["ml".$id])){ updateListens($db,$id); } setcookie("ml".$id, "listens", time()+60*2); // 2minutes -> 60*2 ? || 6h -> 60*60*6 ? } function updateListens($db,$id){if(isset($_GET['id']) && isset($_GET['host'])){ $sql = "UPDATE mixtape SET listens = listens + 1 WHERE id=?"; $stm = $db->prepare($sql); $stm->execute(array($id)); } } ?> Code updated. Hope you can test it. What do you mean with "if you are setting is at the top of a script and then trying to read it a second later you're not going to see it." ? Can you explain better..? I cant really understand sorry.. Edited November 9, 2014 by miguelfsf Quote Link to comment https://forums.phpfreaks.com/topic/292366-cookie-not-working-properly-inside-popup-window/#findComment-1496212 Share on other sites More sharing options...
ginerjm Posted November 10, 2014 Share Posted November 10, 2014 (edited) What's not to understand? If you are setting it in your script and then attempting to read it a few lines later you're not going to see it. Cookies are set but not read by the script until the script is re-loaded. Edited November 10, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/292366-cookie-not-working-properly-inside-popup-window/#findComment-1496217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.