Cookie not created

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Cookie not created

Post by tecktalkcm0391 »

I have a class and in it there is a function that makes a cookie:

Code: Select all

class login {
    
    function setCompanyId($company_id){
        setcookie('company_id', $company_id, 2592000 + time() );
    }
}
And it never seems to be created. Any ideas why? And how to fix it?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Cookie not created

Post by jackpf »

What does

Code: Select all

print_r($_COOKIE);
show?

Also, try setting the cookie with the path and domain name as well. It shouldn't make any difference, but you never know...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Cookie not created

Post by tecktalkcm0391 »

The output shows only

Code: Select all

Array ( [token] => fd3f90c46f59b53832578ed188d5156b )
Token in the PHP Session Name.

Any more ideas, I added the path and domain name too, no luck.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Cookie not created

Post by jackpf »

Have you got error reporting turned on? Are you calling this function before anything is output to the page?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Cookie not created

Post by tecktalkcm0391 »

I had ob_start running which hide the header errors, but I am getting "headers already sent by" message. Is there anyway to find out where its coming from, cause the line it says, it tells me it the setcookie() line. Anyway to work around it?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Cookie not created

Post by jackpf »

What's the exact error?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Cookie not created

Post by tecktalkcm0391 »

A line of code above the line with the cookie was causing the error. Thanks though :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Cookie not created

Post by jackpf »

Ahh right. Cool. No problem.
Post Reply