473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("firs tname",$firstna me,time() + (7 * 86400));

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

What can be wrong?

Thanks

Jul 17 '05 #1
3 1688
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
9168
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 performing a redirect when I had left in print "Content-type:text/html\n\n";
4
3814
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"> if(document.cookie.indexOf("beenHere1=true")!=-1) else
1
3473
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 modified from The JavaScript Source) is broken between domains, because when I click my "store" link that should read the cookie and send me to store1.mydomain.com or storeN.mydomain.com I get the default template store.
4
1611
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 the displayed variable in a string only! But if I look at Firefox in the cookie cache if the variables are set... I didn't find anything. what could be the problem?! Apache 2.0.54 with PHP 5.1 RC1 on Gentoo Linux 2005.1
0
1467
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 username/password. This piece works fine and authenticates against the soap header but my authorization cookie will not set.
2
2577
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 browser cache setting (and OS), the program behaves differently. This is how it should work: 1. The client program opens a webbrowser control, using navigate2 method to post xml data to my aspx pages, eg abc.aspx
4
6450
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 examples of setting cookies (although I am still not sure about timestamps and cookie lifespan), but no reference to getting the currently set cookies. For instance, I want to see if there is any 'user' value, to check whether the user has logged in....
1
6493
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
4
7353
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 in each page refresh or redirect. I really do not have any coding against how the session works. And I have not changed any setting in IIS. I even compared the 2 code(old and new) bases and didn't see anything funny
5
1690
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
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.