PHP SetCookie with Localhost and Apache

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 frost on Jan 19th, 2006 17:35 - Subscribe Bookmark and Share


Post a comment:


ReCaptcha:

Posting as anonymous Anonymous guest, why not register, or login now.



Posted by david on January 19th, 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 23rd, 2006

I will look into it tommorow. Night.

--FrosT



Posted by converge on January 24th, 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,"/"wink.gif; 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! grin.gif



Posted by anonymous on May 07th, 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 15th, 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"wink.gif { } 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 13th, 2009

YES, looking all night how to fix this, thanks happy.gif 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 sad.gif
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 11th, 2009

Work! Finally!! thks!!



Posted by anonymous on September 28th, 2009

cookies already work on my localhost, i just cant get it to work with sub directories
help please !!!



Posted by anonymous on March 16th, 2010

a great thanks from my core



Posted by anonymous on June 11th, 2010

Thanks Frost... I wish I had googled this earlier, would have saved me two hours of bashing my head on my computer screen!



Posted by anonymous on June 23rd, 2010

Hey thanks a lot for this article..... I was just fed up with cookie problem in localhost but now it works...



Posted by anonymous on June 29th, 2010

NOTE:

You should put a . in front of your host for subdomains, but for localhost use false, '', or null, etc (evaluates to a false value). Otherwise your PATH, TIME, etc should be set correctly and accordingly.



Posted by anonymous on August 19th, 2010

Just helpful. Thank you happy.gif



Posted by anonymous on November 28th, 2010

Thank you! .... almost throwed my laptop out the window...btw the explanation is in the manual :

"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.
"

...and myself was focusing only on the 'domain' parameter. I guess the 'issue' isn't stressed enough in the manual: the obvious fact that a cookie server access is different from the cookie client access.




Posted by anonymous on April 29th, 2011

Thank you! You save a lot of my time! :-)



Posted by anonymous on August 08th, 2011

gr8 work man



 

Widget

Hypersmash.com

Extras

  • Not Implemented