imgrooot Posted January 22, 2019 Share Posted January 22, 2019 I want to show a cookie of a referral's username on a sign up page. The link is like this, www.mysite.com/signup?ref=johnsmith. The cookie doesn't show if I go to that url page. But it does show up once I reload the page. So I'm wondering if it's possible to show the cookie the first time around, instead of reloading the page? Here is my code. // This is in the header $url_ref_name = (!empty($_GET['ref']) ? $_GET['ref'] : null); if(!empty($url_ref_name)) { $number_of_days = 365; $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days; setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); } else if(empty($url_ref_name)) { if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; } } else {} // This is for the sign up form if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; ?> <fieldset> <label>Referred By</label> <div id="ref-one"><span><?php if(!empty($user_cookie)){echo $user_cookie;} ?></span></div> <input type="hidden" name="ref" value="<?php if(!empty($user_cookie)){echo $user_cookie;} ?>" maxlength="20" placeholder="Referrer's username" readonly onfocus="this.removeAttribute('readonly');" /> </fieldset> <?php } Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/ Share on other sites More sharing options...
requinix Posted January 22, 2019 Share Posted January 22, 2019 $_COOKIE is only set when the cookies are being sent to the server. If you're telling the browser to create a new one it certainly couldn't have already been sent. But there's nothing stopping you from adding to $_COOKIE. It's just an array, after all. You can add the value and pretend it was sent. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563770 Share on other sites More sharing options...
imgrooot Posted January 22, 2019 Author Share Posted January 22, 2019 (edited) 1 hour ago, requinix said: $_COOKIE is only set when the cookies are being sent to the server. If you're telling the browser to create a new one it certainly couldn't have already been sent. But there's nothing stopping you from adding to $_COOKIE. It's just an array, after all. You can add the value and pretend it was sent. Not sure if I understand you correctly. But I have found a fix around. Instead of having the referral link go directly to the sign up page, I instead link it to the home page. Like this "www.mysite.com/?ref=johnsmith". This way when the user goes to the sign up page, the cookie will already be set. I tried it and it seems to work fine. Edited January 22, 2019 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563771 Share on other sites More sharing options...
requinix Posted January 22, 2019 Share Posted January 22, 2019 setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); $_COOKIE["ref"] = $url_ref_name; Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563778 Share on other sites More sharing options...
imgrooot Posted January 22, 2019 Author Share Posted January 22, 2019 2 hours ago, requinix said: setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); $_COOKIE["ref"] = $url_ref_name; Of course it was that simple. I should have thought of that. It works like a charm now. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563781 Share on other sites More sharing options...
ginerjm Posted January 22, 2019 Share Posted January 22, 2019 (edited) And how long will an entry in the $_COOKIE array actually last? I see no expiration date in this example (nor any of the other things that one usually provides with a setcookie() call). Edited January 22, 2019 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563782 Share on other sites More sharing options...
requinix Posted January 22, 2019 Share Posted January 22, 2019 3 hours ago, ginerjm said: And how long will an entry in the $_COOKIE array actually last? Until the script ends or something unsets it. $_COOKIE isn't magical. Adding stuff doesn't actually set any cookies. PHP fills the array with cookies sent in the request and that's it, just like $_POST and $_GET. You're free to (ab)use the array as much as you want. Really, $_SESSION is the only magical one, and calling it that is a stretch. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563786 Share on other sites More sharing options...
ginerjm Posted January 22, 2019 Share Posted January 22, 2019 Ok then. More like I thought. So - to the OP - if cookies are your 'must-use' choice you have to realize that the cookie you set NOW is not available until the server is called upon to send a page to your client and only at that time will the cookies that exist AT THAT TIME be delivered to you for reference. If all you want is a user name, why not just set the SESSION array? Or even better just use the var that you create when you grab the url argument. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563787 Share on other sites More sharing options...
imgrooot Posted January 23, 2019 Author Share Posted January 23, 2019 5 hours ago, ginerjm said: Ok then. More like I thought. So - to the OP - if cookies are your 'must-use' choice you have to realize that the cookie you set NOW is not available until the server is called upon to send a page to your client and only at that time will the cookies that exist AT THAT TIME be delivered to you for reference. If all you want is a user name, why not just set the SESSION array? Or even better just use the var that you create when you grab the url argument. Because if I'm not mistaken, the SESSION only lasts until you close the browser. I want to use the COOKIES so that the referral name is saved for up to a year, unless someone deletes their browser cookies or uses a different referral link. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563792 Share on other sites More sharing options...
requinix Posted January 23, 2019 Share Posted January 23, 2019 7 minutes ago, imgrooot said: Because if I'm not mistaken, the SESSION only lasts until you close the browser. $_SESSION is tied to the session cookie which may or may not last after the browser closes, depending on your session cookie settings. And for the most part sessions should end with the browser because that's a fairly standard practice. However it's quite possible to set the session cookie with an expiration of, say, a year, so it will survive after the browser closes. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563793 Share on other sites More sharing options...
imgrooot Posted January 23, 2019 Author Share Posted January 23, 2019 1 hour ago, requinix said: $_SESSION is tied to the session cookie which may or may not last after the browser closes, depending on your session cookie settings. And for the most part sessions should end with the browser because that's a fairly standard practice. However it's quite possible to set the session cookie with an expiration of, say, a year, so it will survive after the browser closes. So in your opinion, which method is better to use? The $_COOKIE method I used above or a $_SESSION one? Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563796 Share on other sites More sharing options...
requinix Posted January 23, 2019 Share Posted January 23, 2019 If you need to work with cookies then stick with cookies. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563798 Share on other sites More sharing options...
imgrooot Posted January 23, 2019 Author Share Posted January 23, 2019 4 hours ago, requinix said: If you need to work with cookies then stick with cookies. Got it. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563806 Share on other sites More sharing options...
ginerjm Posted January 23, 2019 Share Posted January 23, 2019 I agree with Requinix (as any sane forum-reader would) in that cookies are what you need to use for the purpose you just described. That means that to solve your original-posed dilemma you should reference your incoming (from the url query) value using a php variable name. And it may also help to not only check for that url value but the cookie value and assign whichever value you find to the same php variable so that once this startup housekeeping is complete the rest of your code will not need to be altered. Quote Link to comment https://forums.phpfreaks.com/topic/308208-the-cookies-dont-show-up-until-the-page-is-reloaded-is-there-a-way-to-show-them-the-first-time-around/#findComment-1563816 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.