PHP Cookie not rewriting

Hello,

I am trying to make it so that when someone posts a search it saves their parameter so that I can auto populate it for the next time.

My problem is that it is submitting the form to the same page and it takes one refresh or reload to clear the old cookie... it is quit annoying.

Thanks for any help,
Matt
<?php
session_start(); 

if (empty($_POST['param'])) { 
echo '';
} else {
setcookie ("locationParam", $_POST['param'], time() + 129600);
}

echo $_COOKIE["locationParam"];
?>

Open in new window

LVL 1
movieprodwAsked:
Who is Participating?
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.

steveo225Commented:
setcookie sets the cookie in the browser, but does not set the cookie in the the super global $_COOKIE array (at least in some configurations). Whenever you call setcookie, also do $_COOKIE['locationParam'] = $_POST['param']; as well.

When you naviage to the page later, the cookie will be set without doing anything.
0

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
i-CONICACommented:
You could put it into a session variable instead? Or is that even needed?

The question's a bit vague...
<?php
session_start(); 
if (empty($_POST['param'])) { 
echo '';
} else {
$_SESSION['locationParam'] = $_POST['param'];
}
echo $_SESSION['locationParam'];
?>
//or maybe just?
<?php
$searched = (isset($_POST['param'])) ? $_POST['param'] : '';
echo $searched;

Open in new window

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