Jump to content
Sign in to follow this  
miguelfsf

Cookie not working properly inside popup window

Recommended Posts

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 ?
}
(...)
?> 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
// 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 by miguelfsf

Share this post


Link to post
Share on other sites

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 by ginerjm

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.