I am having problems getting my cookies to work on my site where I am using a combination of session_start, setcookie and include.

I believe I have followed all of the rules by declaring session_start() as the first statement in every page on the site, however, I am also using the include statement to generate my page header and page footer later in the code, and somewhere in the middle I am using the setcookie statement.

Unfortunately a combination of all three seem to be stopping my cookie being generated.

For example I have the following page;

<?PHP
session_start();
include 'dbconnect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to site</title>
<link href="container.css" rel="stylesheet" type="text/css" />
<link href="format.css" rel="stylesheet" type="text/css" />
<script src="myjs.js" type="text/javascript"></script>
<body>
<?PHP
include 'header.html';
......mysql_query.....
if ($username)
{ 
setcookie('mycookie',$username, time()+3600);
}
?>
<div class="maincontainer"><h1>Welcome to the site<a href="nextpage.php"> Click here to continue </a></h1></div>
<?PHP
include 'footer.html';
?>
</body>
</html>

where the header.html page looks like this

<div class="headercontainer"><p>Site title</p>

however if nextpage.php looks like this

<?PHP
session_start();
include 'dbconnect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to site</title>
<link href="container.css" rel="stylesheet" type="text/css" />
<link href="format.css" rel="stylesheet" type="text/css" />
<script src="myjs.js" type="text/javascript"></script>
<body>
<?PHP
include 'header.html';
.....mysql_query.....
$display=$_COOKIE['mycookie'];
echo $display;
?>
</body>
</html>

I am getting an error "variable $display is not valid" and I can confirm that the cookie has not been written to my local drive.

I have read somewhere (not in the PHP manual) that all include statements must be the first declared (after session_start) but I am not sure if a) this is true, b)if this is true how you can include a footer or c) how you can declare CSS links after the include statement has been read and expect the resulting page to be formatted correctlly.

I can confirm that the PHP.INI is correctly configured to generate cookies, but given the above my cookies are not being generated.

Any ideas?

Change your code as,

<?PHP
session_start();
//use mysql query 
$username="xxxx";
if ($username)
{ 
setcookie('mycookie',$username, time()+3600);
}
include 'dbconnect.php';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to site</title>
<link href="container.css" rel="stylesheet" type="text/css" />
<link href="format.css" rel="stylesheet" type="text/css" />
<script src="myjs.js" type="text/javascript"></script>
<body>
<?PHP
include 'header.html';

?>
<div class="maincontainer"><h1>Welcome to the site<a href="nextpage.php"> Click here to continue </a></h1></div>
<?PHP
?>
</body>
</html>

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Cookies must be sent before any output from your script .This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Thanks divyakrishnan,

As my statement to setcookie followed quite a lot of logic, I first run the logic, and if all is OK, I jump to a new page (with required variables) to setcookie.

Many thanks for your valuable help.

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.