Jump to content

Recommended Posts

I've a website with approx 100 pages, I need to store two bits of data into two cookies. I'm able to set a cookie but the it loses it's value as soon as the user leaves that page. Any ideas, I can't see what I'm doing wrong.

 

Set the cookie with data posted via a form:

 

<?php 
if (isset($_POST['qty'])) 
{

setcookie("qty", $_POST['qty']);
setcookie("tot", $_POST['tot']);
}
?>

<?php
//Check to see if the $_POST qty has data. If it has data we want to use it to display the cart contents information.
if (isset($_POST['qty'])) {
  $qty = number_format($_POST['qty'],0);
  $tot = "£" . $_POST['tot'];
}
//If the $_POST var does not have data let’s check to see if the $_COOKIE qty has data.
elseif (isset($_COOKIE['qty'])) {
  $qty = number_format($_COOKIE['qty'],0);
  $tot = "£" . $_COOKIE['tot'];
  }
else 
  { 
  $tot="$" . 0;
  $qty=0;
  }
?>
<table>
<tr>
	<th>Cart Contents</th>
</tr>
<tr>
	<td>Items:</td>
	<td>
		<?php 
			echo $qty;
		?>
	</td>
</tr>
<tr>
	<td>Total</td>
	<td>
		<?php
			echo $tot;
		?>
	</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/74977-solved-problem-with-cookies/
Share on other sites

Have a look at the setcookie() syntax:

 

http://dk2.php.net/manual/en/function.setcookie.php

 

You have omitted the expire argument.

 

expire

 

    The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

 

        Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

 

        expire is compared to the client's time which can differ from server's time.

 

 

This causes your cookie to expire at the end of the session which is when the browser windows closes.

 

it loses it's value as soon as the user leaves that page.

 

Could be interpreted as the user closing the browser and returning.

 

 

I've done that but still the same problem, the cookie exists for one page and then back to zero as soon as its on the home page

 

Included on every page

<?php

if (isset($_POST['qty']))

{

 

setcookie("qty", $_POST['qty'],time()+3600);

setcookie("tot", $_POST['tot'],time()+3600);

}

?>

 

 

<?php

//Check to see if the $_POST qty has data. If it has data we want to use it to display the cart contents information.

if (isset($_POST['qty'])) {

  $qty = number_format($_POST['qty'],0);

  $tot = "£" . $_POST['tot'];

}

//If the $_POST var does not have data let’s check to see if the $_COOKIE qty has data.

elseif (isset($_COOKIE['qty'])) {

  $qty = number_format($_COOKIE['qty'],0);

  $tot = "£" . $_COOKIE['tot'];

  }

else

  {

  $tot="$" . 0;

  $qty=0;

  }

?>

<table>

<tr>

<th>Cart Contents</th>

</tr>

<tr>

<td>Items:</td>

<td>

<?php

echo $qty;

?>

</td>

</tr>

<tr>

<td>Total</td>

<td>

<?php

echo $tot;

?>

</td>

</tr>

</table>

 

 

         

 

 

 

This is to set the cookie

 

setcookie("name", $_POST['name'], time()+(3600 * 2));

 

 

This is at the top of every page you are looking for the cookie

 

if (!isset($_COOKIE['name'])) die ("you cannot access this page");

 

This is what I use, and it works fine.

So you are saying that you set a cookie on page1.php and when you check for the cookie on page2.php you get a value of 0 for the cookie?

 

Yes, for example, page1.php sets up the cookie but if I then go to page2.php zero the cookie doesn't exist. However, if I return to page1.php the cookie is back....???...

 

I'm lost to be honest

It sounds like you have a issue with the path in which the cookie is available.

 

Try setting the cookie with the path parameter to '/' so it's available within the entire domain:

 

path

    The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

 

setcookie("qty", $_POST['qty'], time()+3600, "/");

 

The default value of the path parameter is the current directory from which the cookie is being set. So if you set the cookie here www.domain.com/pages/page1.php then it will not be available here www.domain.com/scripts/page1.php.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.