• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

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

0
movieprodw
Asked:
movieprodw
1 Solution
 
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
 
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now