Hi,
I want to store a cookie in user's system which will stay there until they remove it from their browser by "Clearing Cookies". Please tell me how do i do it ?
Thanks.
Printable View
Hi,
I want to store a cookie in user's system which will stay there until they remove it from their browser by "Clearing Cookies". Please tell me how do i do it ?
Thanks.
You need to use the setcookie() function.
Hi visualAd,
Thanks for the tip, i figured it out but now i am facing another problem. I setup 2 cookies...for example:
If user comes to page1.php i set a cookie: testcook1 with value step1
then user is taken to next page page2.php which sets another cookie named testcook2 with value step2
now after all 2 cookies are set i want to display the cookies value:
So the testcook1 is displaying successfully but the testcook2 is not showing its value ...can you please tell me what i am doing wrong with this one ? All the code is same as testcook1...just names changed etc. and still its not working please tell me why..
Thanks
Without seeing code, not sure, but this is a very basic example:
page1.php
page2.phpPHP Code:<html>
<head>
</head>
<body>
<?php
$value = 'Test 1';
setcookie("TestCookie", $value);
?>
<a href="page2.php">Next page</a>
</body>
</html>
page3.phpPHP Code:<html>
<head>
</head>
<body>
<?php
$value = 'Test 2';
setcookie("TestCookie2", $value);
?>
<a href="page3.php">Next page</a>
</body>
</html>
It'd easier to help if you post up your code... :eek:PHP Code:<html>
<head>
</head>
<body>
<?php
echo $_COOKIE['TestCookie'] . "<br>" . $_COOKIE['TestCookie2'];
?>
</body>
</html>
Hi,
Cani see them on page3 only or can they be seen on page2 only ? I am setting at page1 then at page 2 and after setting on page2 i am displaying them both but only 1 is displaying and 2 is not.
Thanks!
A cookie is probably best described as a small information token. After the cookie has been set by the server, the client sends it back thereafter with each request. The parameters for the setcookie() function allow you to specify the cookies constraints, i.e: path, domain, secure. These tell the client which pages it should pass the cookie back to.
In answer to your question a cookie is valid for all pages within the specified path and/or the domain. If none are given its available globally throughout your site. It is worth however noting that the cookie will not be visible until the page request after you have set it.
If you are thinking of storing a lot of information inside the cookie, or find yourself needing more than one, or will be using it to identify users then I would recommend you use PHP's built in session module instead.
Hi,
I shifted to cookies becuase i already tried session. Anyways do i have to put session_start() on top of the every page ?
Thanks!
Yes, when you start a session for the first time it attempts to set a cookie, so you should use the session_start() function like you would use the setcookie() function.
That code will not work, because you have called the setcookie() function after output as started. It should be at the very top of the page before and other echo or print statements and before and HTML.Quote:
Originally Posted by Brandoe85
Hi,
Okay thanks i will try it out and let you know if i get any problems.
Thanks.