/    Sign up×
Community /Pin to ProfileBookmark

PHP set cookie problem

Hello all,

Ive uploaded a site on a UNIX server (first time using one) and im having trouble setting a cookie. It works fine on my home wamp server.

This is the code ive been using to set my cookie

[code]
setcookie(“user”, “broidery”, time()+23600);
[/code]

And using

[code]<?php
if (isset($_COOKIE[“user”])){
?>
blah blah
<?php
} ?>
[/code]

The cookie doesnt seem to be setting. Anyone any idea where im going wrong?
Thanks

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@frankvwMar 30.2011 — Hello all,

Ive uploaded a site on a UNIX server (first time using one) and im having trouble setting a cookie. It works fine on my home wamp server.[/QUOTE]


Not sure what's going on, but if it were me I would first compare phpinfo() output from my home WAMP server with that of the Unix server and see what permissions and options you have.

Also with some hosting providers who use virtualization sometimes the domain in which the cookie is set may get mashed up. Some custom code to debug the problem is probably the quickest way to find out. Set a cookie and read it back, experiment with various methods and options and see what happens.

// Frank
Copy linkTweet thisAlerts:
@aj_nscMar 30.2011 — I would first check to see if you've fallen victim to the most common problem with people using php to set cookies - make sure there is no output, not even an empty line, blank space, or BOM before you open the php tag to try and set the cookie.

Post the full code of the page (or at least the first X number of lines until you get to the setcookie() statement).

I've got $1000 on that being your problem....any takers?
Copy linkTweet thisAlerts:
@DerokorianMar 30.2011 — to clear-*** setcookie does not need to be the first line of your code, however it does need to be the first to to send ANY output to the end user. IE [code=php]
<?
if(something == something else) {
setcookie(stuff);
} else {
echo "if failed no cookie for you!";
}
[/code]
Copy linkTweet thisAlerts:
@svidgenMar 30.2011 — Hmm ... but, if you're using this to flag authenticated users, you ought to consider just using PHP's sessions. Simplified:

[code=php]// near the very top of your script(s)
session_start();

// after authentication
$_SESSION['auth_user'] = $user;

// when testing for user authentication
if (isset($_SESSION['auth_user']) && $_SESSION['auth_user']) {
// do authenticated user stuff
} else {
// do un-authenticated user stuff
}

// logout
unset($_SESSION['auth_user']);[/code]


Use ini directives to ensure that your sessions are being stored in a secure location and that they last as long as you need them to.
Copy linkTweet thisAlerts:
@jimmygauthorMar 30.2011 — Thanks huys appreciate it a lot!

Much thanks to aj_nsc that was the issue I was had. Works fine now!

Thanks again guys
Copy linkTweet thisAlerts:
@DerokorianMar 30.2011 — aww too bad AJ... no one took your bet...
Copy linkTweet thisAlerts:
@aj_nscMar 30.2011 — Haha, yes...too bad but I guess the readers of this thread probably knew that the odds were in my favor ?

Thanks for clarifying my post Derokorian, too, I worded it poorly, I had considered saying "before any output is sent", but that can be very confusing to users who don't consider a blank space "output" - I know it was confusing to me when I started.
Copy linkTweet thisAlerts:
@DerokorianMar 30.2011 — Well the simple explantion of output is any information that leaves the server and arrives at the end user, that definition helped me understand the concept of blank space output. Hopefully it helps someone else now too!
Copy linkTweet thisAlerts:
@artie-1Feb 03.2012 — The above things are true, but what you might find for new installations / first time on a new site, is the php.ini is not set up. I spent a few hours looking for coding errors to find that the web-host provider was using a not so friendly php.ini So I was able to copy my local php.ini to override the hosted version.


Primary culprit for my "php set cookie problem" was no buffering set in php.ini to accommodate any random spaces sent.

; output_buffering

; Default Value: Off

; Development Value: 4096

; Production Value: 4096

output_buffering = Off
×

Success!

Help @jimmyg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...