Redirect after setcookie

Hi all,

I’m struggling to redirect the customer to the view account page after login.
There’s a checkbox for “remember me” on the login page. If cutomer check this box, a cookie is set, otherwise a session is set.

There’s no problem at all to redirect the customer if session is set. But if the customer check the “remember me” box, the page hangs there forever.

It seems there’s a problem use SETCOOKIE and HEADER(Location) together. One solution is use Javascript to redirect page, but I’m afraid some customer might have js disables.

So is there a way round this problem?

Many thanks!

Here’s my script:


if ($rememberme == '0')
{
   $_SESSION['username'] = $username;
   header("Location: view_account.php");
   exit();
}

if($rememberme == '1')
{
setcookie("username",$username,time()+60*60*24*100);
header("Location: view_account.php");
exit();
}


The main problem is headers always have to come before a function that can output content which [B]setcookie/B can do, see the below code and let me know if it works.

if ($rememberme == '1') 
{
     header("refresh: 5; url=view_account.php");
     setcookie("username", $username, time() + 60 * 60 * 24 * 100);
     exit();
}

thanks SgtLegend, your code did redirect to the view_account page, but IE can’t open the page. On the view_account.php page, I have the following code:

if (!isset($_SESSION[‘username’])||isset($_COOKIE[‘username’]))
{
//redirect to login page
}

I changed it to:

if (!isset($_SESSION[‘username’])&&!isset($_COOKIE[‘username’]))

Both your code and my original code worked! why is that?

thanks!