Jump to content

The cookies don't show up until the page is reloaded. Is there a way to show them the first time around?


imgrooot

Recommended Posts

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
}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.