473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Setting a Cookie

Hi there

New to PHP. Im trying to set a cookie but for some reason it wont work.
The code is inside a Submit button and happens after I have read the MySQL
database and succesfully retrieved the record. For some reason the cookie is
not created.

Here is the code

setcookie("firstname",$firstname,time() + (7 * 86400));

$firstname contains a string value. It is not empty.

What can be wrong?

Thanks

Jul 17 '05 #1
3 1677
Agent M wrote:
New to PHP. Im trying to set a cookie but for some reason it wont work.
The code is inside a Submit button and happens after I have read the MySQL
database and succesfully retrieved the record. For some reason the cookie is
not created. (snip) What can be wrong?


Has the client /sent/ the cookie to the server?
Only /received/ cookies are available in the $_COOKIE (or equivalent)
variable.

Try this:

<?php
echo '<p>cookie not set yet!</p>';
if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>', $_COOKIE['test'], '</b></p>';
setcookie('test', 'true');
echo '<p>cookie has been set!</p>';
if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>', $_COOKIE['test'], '</b></p>';
?>

Now open this page in your browser and see what happens ... then refresh
the browser and see what happens
The first time you open the page, the client will not have send the
cookie (it doesn't have it); the second time the client sends the cookie
(once, before the script starts -- it's the web server job to make it
available to the script) and you'll see it in both echo's
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Pedro Graca <he****@hotpop.com> wrote in
news:c0*************@ID-203069.news.uni-berlin.de:
Try this:

<?php
echo '<p>cookie not set yet!</p>';
if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>',
$_COOKIE['test'], '</b></p>'; setcookie('test', 'true');
echo '<p>cookie has been set!</p>';
if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>',
$_COOKIE['test'], '</b></p>'; ?>

Now open this page in your browser and see what happens ... then
refresh the browser and see what happens


Not what you intend; setcookie() will always fail there because it has to
send a header, which it can't do because you've already output non-header
text.
Jul 17 '05 #3
Eric Bohlman wrote:
Pedro Graca <he****@hotpop.com> wrote in
news:c0*************@ID-203069.news.uni-berlin.de:
<?php
echo '<p>cookie not set yet!</p>';
setcookie('test', 'true');
Error in this line: headers already sent
Now open this page in your browser and see what happens ... then
refresh the browser and see what happens


Not what you intend; setcookie() will always fail there because it has to
send a header, which it can't do because you've already output non-header
text.


You're right, I was wrong; sorry everyone.

The idea was to let the OP verify that cookies are not available for the
script/page/access that sets them.

The script that set the cookie send it to the client, and that's it!

When the client revisits *it* send the cookie along with the request.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
1
by: Jose Olivas | last post by:
I am setting a cookie on a subdomain: http://store1.mydomain.com Then the store takes me to a shopping cart that is at: http://shopping.mydomain.com Somewhere the following code (taken and...
4
by: Tamer Higazi | last post by:
Hi! I wrote a small script setting a cookie.... but nothing is being set. What could be the problem?! Did I make something wrong?! One script is used to ask for the cookie and the other one shows...
0
by: Chris | last post by:
Hi all, I'm having trouble setting an authorization cookie from a web service. I need to set an authorization cookie on the client machine when the proper soap header is sent with...
2
by: junlia | last post by:
Hi All, I am working on a project that acts as a bridge. It does some checking with post xml data, and then redirects the request to an appropriate page. However, we find that depends on the...
4
by: Vlad Dogaru | last post by:
Hello, I am trying to use cookies and Python to create a simple login example. But I am very disoriented at the existence of two cookie libraries, namely Cookie and cookielib. I have seen...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: SevDer | last post by:
Hi, I've done some coding in my web application however right now for an unknown reason my asp.net 2.0 site is not setting asp.net_sessionid cookie and as a result, I am losing the session data...
5
by: sophie_newbie | last post by:
Does anyone know how to do this? I can't seem to make it work. I'm using: c = Cookie.SimpleCookie() c = "unamepwordwhatever" c.expires = time.time() + 300 print c
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.