Hello, I created a site where you have to log in and it begins a session. I wanted to make a stay logged in feature so I started using cookies. I made it so if there was no session then check for cookies. My site also sends out emails with links. While on the site with cookies enabled, I would go to my email and click the link and it would say that there is no session or cookies. Then if I sign in for a second time and then click the link in the email it says it works.

So for Cookies to work from the email link I have to sign in twice and enable cookies twice. But when I sign in once and shut down and come back the cookies seem to work fine. Any suggestions?

A couple of basic questions:

When you click a link in your email, does it open the link in the same browser in which you were previously signed-in? I ask, because I know I read my email in one browser, and develop in another.

Does the script that you're taken to from the email have session_start(); declared before any output or checking for the existence of a session?

Are you setting an expiry date/time for the cookies?

Beyond that, you're going to have to post some code.

Thanks for the reply blocblue.

When I sign in and start a session in Chrome, and then click a link in my email in Chrome it will open a new tab in Chrome and there will be no session.

I have a global php page called "config.php" that declares the timezone, date, time, etc and it is where it checks for sessions and cookies. On all my pages it starts wil "

session_start(); 
require "config.php";

So here is the code that I use...
When you log in and check stay logged in this is what happens:

setcookie ('uusername', $username, time()+1209600); 
setcookie ('usernamett', $usernamett, time()+1209600); 
setcookie ('upassword', $mpassword, time()+1209600);

PROFILE PAGE:

session_start(); 
require "config.php";

CONFIG PAGE:

if (!isset ($_SESSION['uloggedin']) && !isset ($_SESSION['utime']))
{
if (isset ($_COOKIE["usernamett"]) && isset ($_COOKIE["upassword"])){
            //start the sessions
            $_SESSION['uloggedin'] = $_COOKIE["usernamett"];
            $_SESSION['upasskey'] = $_COOKIE["upassword"];
            $_SESSION['utime'] = time();}}

PROFILE PAGE AGAIN:

    if (isset ($_SESSION['uloggedin']) && isset ($_SESSION['utime']))
    {
    $username = $_SESSION['uloggedin'];
    $upasskey = $_SESSION['upasskey'];
    $query = "SELECT * FROM users WHERE username = '$username' AND password = '$upasskey'";
    $result = mysql_query($query) or die("Could not execute query:<br>$query");
    }
    if(mysql_num_rows($result) == 0){
    unset ($_SESSION);
    session_destroy();
    header("Location: index.php?error=deactivated");
    exit;
    }
    }

Do I have to re setcookie?
When I click the link the first time it says I'm not signed in and there are no cookies or a session but when I sign in on that page and then click the link from the email again it says I am signed in. So it's like I have to sign in twice for them to work.

I cannot see anything immediately wrong with your code. And no, you shouldn't have to set a cookie again until it expires - unless the data changes of course.

I'd suggest adding debugging output throughout the code to find where it's losing the data.
echo '<pre>'; var_dump($_SESSION, $_COOKIE); echo '</pre>'; die;

Thanks I will try that and let you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.