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..
 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.