Creating A Cookie
<p>I am trying to use cookies to maintain log-in persistence on my website. But, for some reason unknown to me, the cookie is not being created. Only the Session is being created. I am using Sessions to keep track of the logged in user, and cookies to re-create the session when they come back. What am I doing wrong?</p>

<p>Create the session and cookie:</p>

<pre><code>&lt;?php

$connect= // connect variables

    $query= "SELECT * FROM users where email= '$email' AND password=    
'$password'";
    $result= mysqli_query($connect, $query)
        or die('error with query');
        if (mysqli_num_rows($result) == 1) {
            $row= mysqli_fetch_array($result);
            session_start();
            $_SESSION['id']= $row['user_id'];
            $_SESSION['name']= $row['fname'] . " " . $row['lname'];
            setcookie('id', $row['user_id']);
            $profile_url= 'http://'. $_SERVER['HTTP_HOST'] . 
dirname($_SERVER['PHP_SELF']) . '/profile.php';
            header('Location:profile.php');     
            }
        else {
            $message= "Incorrect email/password combination.";
            }

?&gt;
</code></pre>

<p>Check for a cookie to restore the session:</p>

<pre><code>&lt;?php

session_start()
/* if a session does not exist, see if a cookie does to set the session */
if (!isset($_SESSION['id'])) {
    if (isset($_COOKIE['id'])) {
        $_SESSION['id'] = $_COOKIE['id'];
    }
    else {

    }
}

?&gt;
</code></pre>

<p><strong>EDIT</strong></p>

<p>I needed to add an expiration parameter to the cookie!</p>

<pre><code>setcookie('id', $row['user_id'],time()+86400);
Note: The above example sets the cookie for 1 day
</code></pre>
