Problem with PHP cookies

Status
Not open for further replies.

murdocsvan

Web Programmer
Messages
896
Location
Surrey, UK
For some reason im having a great deal of problems with using cookies on my website. I have a login system which i initially set to use sessions, but decided to use cookies as sessions was a bit glitchy. The order of my website goes:

Login form > Check Login Script > Login Success Script > index.htm

on the first script, this is code that sets the cookie:

PHP:
		// Register $myusername, $mypassword and redirect to file "login_success.php"
		$expire=time()+60*60*24*5;
		setcookie('user', $username, $expire);
		header("location:login_success.php");

And this is the piece of code on each page that checks that the cookie is there:

PHP:
<?php

if(!isset($_COOKIE['user']))
    {
    header("location:login/main_login.htm");
    }

?>


Now for some reason, it will go to the Login Success page, and not have a problem even though that page has the cookie checking code on it. However, when the Login Success page then redirects to the index.htm page, it goes straight back to the login form, thinking the cookie's not there. The second bit of code is identical on all the pages i have protected by the login system.

Thanks in advance.
 
Try the Cookie Editor plugin for Firefox to make sure the cookie is actually being set, and with the right parameters.
 
Its probably because on every page you MUST use
session_start();
even when you are not using the sessions / variables on that page. Otherwise they are lost.

Make sure session_start(); is on top of each page BEFORE any output is made
 
Status
Not open for further replies.
Back
Top Bottom