php cookies not working

In the head of my file, i set a cookie:

	
 if (!isset($_COOKIE['curUrl']) || empty($_COOKIE['curUrl'] ) ) { 
setcookie('curUrl','1' , time() + (86400 * 365), "/");
 }

Open in new window


Then later on the page I have this if statement
<?php if ( isset($_COOKIE['curUrl'])  && $_COOKIE['curUrl'] == "1"  ) { ?>

Open in new window


For some reason, when the page loads, it's as if the cookie wasn't set yet.
Then if I reload the page, it recognizes the cookie.

What am I doing wrong that it's not picking it up right away.
rivkamakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
The cookie isn't set until the response is sent back to the client, and isn't available in your PHP until the next request from the client.
So you can define and assign a variable to this cookie and use it something like this.
if (!isset($_COOKIE['curUrl']) || empty($_COOKIE['curUrl'] ) ) { 
setcookie('curUrl','1' , time() + (86400 * 365), "/");
$varCookie = $_COOKIE['curUrl'];
 }
//code here
<?php if ( isset($varCookie)  && $varCookie == "1"  ) { ?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Leonidas is correct.  Any cookie you set with 'setcookie' will be sent to the browser in the page response.  It will not be available in $_COOKIE[] until the browser sends the next page request.
rivkamakAuthor Commented:
Is there any other way to go about this besides using session?
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Dave BaldwinFixer of ProblemsCommented:
Sessions have nothing directly to do with cookies.  It's not clear to me what problem you're having.  Have you read this page?  http://php.net/manual/en/function.setcookie.php
Dave BaldwinFixer of ProblemsCommented:
I should add a note.  Chrome will Not set a cookie on 'localhost'.  They feel that it is not a unique identity.  The rules of cookies are that the browser will return a cookie ONLY when the page requested matches the page that the cookie was set on.
hieloCommented:
>> So you can define and assign a variable to this cookie and use it something like this.
But like you correctly pointed out, "The cookie isn't set until the response is sent back to the client, and isn't available in your PHP until the next request from the client."

Thus, the following suggestion will not work:
$varCookie = $_COOKIE['curUrl'];

Open in new window


because $_COOKIE is still empty.  Instead, you need to explicitly initialize $_COOKIE not $varCookie:
if (!isset($_COOKIE['curUrl']) || empty($_COOKIE['curUrl'] ) ) { 
  setcookie('curUrl','1' , time() + (86400 * 365), "/");
  $_COOKIE['curUrl']='1';
 }

//code here
<?php if ( isset($varCookie)  && $varCookie == "1"  ) { ?>

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.