imgrooot Posted November 26, 2017 Share Posted November 26, 2017 Here's a scenario. I am setting up a sponsor referral link. If any user signs up on the site through that referral link, they will be matched with the sponsor of that referral link. Normally I can do this using a simple GET method but I want to use cookies so that the referral link will be valid for 30 days. So if a user decides to come back to the site a week later, they will still be matched with that sponsor assuming they haven't deleted their cookies. For eg. // referral link mysite.com/signup?sponsor=john Here is my cookie code. The issue i am having is that if I go to a different page on the site and come back to the signup page, the cookie gets reset or becomes invalid. Can you tell me what i'm doing wrong? Do I have to use the database to store the cookies or what? $get_user = $_GET['sponsor']; $number_of_days = 30 ; $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ; setcookie( "sponsor", $get_user, $date_of_expiry); if(isset($_COOKIE['sponsor'])) { echo 'set'; } else { echo 'not set'; } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 26, 2017 Share Posted November 26, 2017 Are you executing that same code when you come back? If so, you are writing over your cookie. Based on your error configuration, you should be getting a warning stating something like index sponsor not defined. Use an IF statement to hope over it if $_GET['sponsor'] is not set (and based on your desire, also hope over it if the cookie is already set and you don't want the second link to override the first one). 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 26, 2017 Share Posted November 26, 2017 Note also that when you set a cookie you cannot simply check for it immediately. You have to refresh the page in order for the new cookie to be visible. Quote Link to comment Share on other sites More sharing options...
Solution imgrooot Posted November 27, 2017 Author Solution Share Posted November 27, 2017 Are you executing that same code when you come back? If so, you are writing over your cookie. Based on your error configuration, you should be getting a warning stating something like index sponsor not defined. Use an IF statement to hope over it if $_GET['sponsor'] is not set (and based on your desire, also hope over it if the cookie is already set and you don't want the second link to override the first one). You are correct. I only have to set the cookie if there is a sponsor parameter. I retrieve the cookie if the parameter is not set. Here is the updated code and it works. $get_user = $_GET['sponsor']; if(!empty($get_user)) { $number_of_days = 365; $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days; setcookie( "sponsor", $get_user, $date_of_expiry); } else if(empty($get_user)) { if(isset($_COOKIE['sponsor'])) { // find the set cookie $user_cookie = $_COOKIE['sponsor']; } } else {} Quote Link to comment 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.