Posted by on Jan 19th, 2006 -
Subscribe Mood: PHP
KeyWords: Apache Localhost with PHP
PHP SetCookie with Localhost and Apache
by FrosT
Introduction
As some developers may not know working with a localhost Apache and PHP server requires different setups than working with an actual domain name on a remote server. This article will discuss how to setup your script correctly to store localhost cookies with FireFox and Internet Explorer without too much of a hassel.
Development Coding
You may want to have a development PHP and Apache server on your own local computer to program and test data and new changes to your current web development software. A local Apache and PHP server allows for easy programming with a plus of not having to upload programmed files to the production server. But what about storing cookies on Localhost? Well I recently came across an issue with the setcookie function on the localhost Apache and PHP server. I searched sometime to find the answers I was looking for. Lets dive in!
SetCookies with Localhost and Apache
Setting cookies on localhost with Apache and PHP running is not to much of a chore. I would recommend coding a particular part for your development box that is strictly for it to house all the necessary modified settings, such as setcookies. Here is the correct format needed to SetCookies on localhost box with Apache and PHP.
setcookie("username", "George", false, "/", false);
Using that code to set the cookie the web application needs will save someheartache. I do not know the whole reasoning why the setcookie must be set up in that format, but I do know using the setcookie formatted as above with your development system does work!
Ending Notes
Using the format of the setcookie function with PHP and Apache works great on a Localhost. I love this for my development server, grant it I have variables set for domain and time so I can change all this with one swoop of a config file. Set it up the way you want on your localhost Apache and PHP server. Now you have the correct setcookie code, use it at your own risk!
Posted by david on January 20th, 2006
Dude you are certainly the master of php and cookies.
Posted by converge on January 23rd, 2006
man i wish i was as smart as you and david on this html and pho stuff.. wow. how did you all learn it? i added you as a friend by thw way if that is ok...
-clark
Posted by frost on January 23rd, 2006
Took us a while, me its been about 7 years since I started.
--FrosT
Posted by converge on January 23rd, 2006
can you tell me how i can change the picture on my background? like where do i put the url for the picture i want to put on my blog?
also, if i want my blog in the CENTER of the screen versus the left side of the page, how and where do i go in and fix that? can you help me if you arent too busy there docta frost. thanks chief
Posted by frost on January 24th, 2006
I will look into it tommorow. Night.
--FrosT
Posted by converge on January 25th, 2006
frost man, check out my new blog look!!! its off da chain. haha. amigone helped me and i like it. i am learning more and more shit about this css.. i got html down decently. i just wanna thank you and david again for giving us all this great site for FREE! and no ads... wow, this is the best! stay safe man! god bless!!!
-c-
Posted by frost on January 25th, 2006
Thanks, appreciate it.
--FrosT
Posted by aeonity on January 25th, 2006
Great Article!
Posted by converge on January 25th, 2006
frost can you you change my "stabbingwe" name and register it as "converge"? please this would be great. get back 2 me man
Posted by aeonity on January 25th, 2006
I will see what I can do for you.
Posted by converge on January 25th, 2006
who is aeonity? frost is that you? well thanks to whomever changed it for me. i greatly appreciate it men
Posted by frost on January 25th, 2006
Aeonity = David and I.
--FrosT
Posted by anonymous on February 15th, 2006
Just thought I'd point out that I was having some issues with IIS and Localhost using cookies, and adding the "/" solved my problems.
ex. setcookie("user","george",time()+3600,"/"
; works whearas
setcookie("user","georgy",time()+3600); doesn't
Posted by frost on February 15th, 2006
Awesome thanks for the insight with IIS
--FrosT
Posted by anonymous on September 17th, 2006
Great! It started to work for me, finally1!!
Thank you!
Posted by anonymous on December 16th, 2006
In php.ini put
output_buffering = On
(output_buffering = Off)
; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
RGS mfunk
Posted by anonymous on May 10th, 2007
Thanks FrosT for this super hint, but what if I want to destroy a cookie?
Posted by anonymous on July 12th, 2007
Yeah, that was great man. Thanks! Just what I needed.
Ps. to the previous commenter... you destroy the cookie the same as you do regular cookies: you eat it! :-) Just kidding...
Simply replace the expiration time to sometime in the past like: time()-1 (it's the third parameter)
Hope that helps.
-Steve
Posted by anonymous on November 27th, 2007
The reason setcookie doesn't work on localhost is because by default the setcookie function passes the current domain name to through the HTTP request.
Most browsers will throw a domain name with less than two dots away. If you don't send a domain through with the cookie request the browser will save it with the current domain of the site (localhost).
Adam
Posted by anonymous on February 17th, 2008
the buffering =on doesnt allow cookie to be sent after i send a thing , atleast it dint in my case!
Posted by anonymous on March 29th, 2008
Thank you SO MUCH for that tip on running cookies on localhost. This is something that has annoyed me time and time again, and your solution is so simple! 
Posted by anonymous on May 08th, 2008
Man this should be added to the documentation as people are still having issues.
Spot on, shame I just put my foot through my latest computer though due to this.
Thanks for taking the time to document it.
I'm off to get another computer so I can try it.
Cheers.
Jaz
Posted by anonymous on May 13th, 2008
I have downloaded the Win32 version Apache 2.2.8 as I want to use it as a local server for developing PHP web pages on my PC. I used to have it installed and running, but now I've rebuilt my PC I can't remember how to set it up!
Can you please tell me what to put in the Network Domain, Server Name and Admin Email Address fields requested in the installation? I know these are used in the config file but I can't remember what to put.
If you can help I would grateful.
Regards,
Chris
Posted by anonymous on May 16th, 2008
Thanks for the tip! Completely noob question here but do you know why changing the second "false" to a real domain name causes problems (the cookie doesn't appear to be setting)? Is it just not possible to do that from localhost or something? Thanks again!
Posted by anonymous on May 18th, 2008
Thank you good sir for this help, it was driving me insane only being able to use Opera for localhost testing.
@Anonymous on May 16th, 2008:
Try an if($_SERVER == "localhost"
{ } else { } construct ^^ That's how I save myself effort between my testing and live servers.
Posted by anonymous on May 18th, 2008
$_SERVER['HTTP_HOST'] == "localhost" I meant. Sorry, it's late here.
Posted by jgooda on May 20th, 2008
excellent php advice again, thanks
Posted by anonymous on November 13th, 2008
Thank u for all U....hey i got it
Posted by anonymous on January 14th, 2009
YES, looking all night how to fix this, thanks
saved me a lot of coding to work around my off server domain testing lol
Posted by anonymous on March 06th, 2009
This saved me a lot of trouble.
Posted by anonymous on March 26th, 2009
big help.. thanks!
still stuggling with the timestamp on localhost using firefox 
but I'm sure all will be fine when I go live.
thanks again!
Posted by anonymous on May 06th, 2009
Very simple solution. Thank you very much for posting it. It worked well for my needs.
Posted by anonymous on May 12th, 2009
Work! Finally!! thks!!