Denver Colorado Blog

Denver Colorado Blog

created by James Worthen

PHP SetCookie with Localhost and Apache

Jan 19th, 2006 - Subscribe
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!

Comments:

david
on January 20th, 2006
Dude you are certainly the master of php and cookies.

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

frost
on January 23rd, 2006
Took us a while, me its been about 7 years since I started.

--FrosT

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

frost
on January 24th, 2006
I will look into it tommorow. Night.

--FrosT

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-

frost
on January 25th, 2006
Thanks, appreciate it.

--FrosT

aeonity
on January 25th, 2006
Great Article!

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

aeonity
on January 25th, 2006
I will see what I can do for you.

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

frost
on January 25th, 2006
Aeonity = David and I.

--FrosT

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

frost
on February 15th, 2006
Awesome thanks for the insight with IIS

--FrosT

anonymous
on September 17th, 2006
Great! It started to work for me, finally1!!
Thank you!

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

anonymous
on May 10th, 2007
Thanks FrosT for this super hint, but what if I want to destroy a cookie?

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

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

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!

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

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

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

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!

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.

anonymous
on May 18th, 2008
$_SERVER['HTTP_HOST'] == "localhost" I meant. Sorry, it's late here.

jgooda
on May 20th, 2008
excellent php advice again, thanks

Add Comments:


Image Verification: Verify Image

Posting as anonymous Anonymous guest, why not register, or login now.
eXTReMe Tracker
Denver DVR