Jump to content

Cookie not working properly inside popup window


miguelfsf

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

Link to comment
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..
 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.