setcookie problem

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
Crimpage
Forum Newbie
Posts: 1
Joined: Thu May 20, 2004 2:02 am
Location: Australia

setcookie problem

Post by Crimpage »

Hi,

I'm trying to set a cookie if a user has opted to be remembered. Using:

Code: Select all

<?php
    if (isset($_POST['cookieitup'])) //Does the person want to be remember
    {
        setcookie(session_name(), session_id(), (time() + 60*60*24*367), '/', '', '0');
    }
?>



But I've cleared the cookies on my PC, and its not doing anything. So, I've tried this to check whether or not its setting the cookie:

Code: Select all

<?php
    if (isset($_POST['cookieitup']))
    {
        if (setcookie(session_name(), session_id(), (time() + 60*60*24*367), '/', '', '0') == TRUE)
        {
            echo "Cookie Set";
            exit();
        } else
        {
            echo "Not Set";
            exit();
        }
    } else
    {
    echo "sumthing wrong";
    exit();
    }
?>



So, if the user wants to be remember, it will check to see if the cookie is being set. This works! This will set the cookie and stop on a blank screen saying "Cookie Set" And i can see it on my PC. Then, when i go to the next screen, the cookie is deleted and nothing is remember anymore... This is the only thing that "should" cause a problem on the forwarding page...

Code: Select all

<?php
if (isset($_COOKIE['PHPSESSID']))
{
session_id($_COOKIE['PHPSESSID']);
session_start();
} else
{
session_start();
}
?>



But I cant see how that should cause a problem. Also, I'm testing this on my pc, so the domain is localhost, but it doesnt change if i have in in the "Domain" section in the setcookie function. Any help would be great!

- Crimpage
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

From http://php.net/session_id

"Note: When using session cookies, specifying an id for session_id() will always send a new cookie when session_start() is called, regardless if the current session id is identical to the one being set."
Post Reply