Include vs. header

in a php page, I have the code

setcookie("userid", $userid, time() + 3600 * 24 * 30);
setcookie("autosignin", $autosignin, time() + 3600 * 24 * 30);

session_start();
$_SESSION["loggedIn"] = TRUE;
$_SESSION["userid"] = $userid;
$_SESSION["email"] = $email;
$_SESSION["username"] = $username;

$_COOKIE["userid"] = $userid;
$_COOKIE["autosignin"] = $autosignin;

/* include $_SERVER[‘DOCUMENT_ROOT’] . ‘/cookies.php’;*/
header(‘Location: /cookies.php’);
exit();

Cookies.php is a simple page that just echos out variable values. When I uncomment and use the include to go to cookies.php, it echos out all the variable values just fine. When I use the header to go to that page, it doesn’t echo out any of the variables, including $_SESSION and $_COOKIE variables. Why is it that when I use header, I lose all my variable values?

The code for the cookies.php file is:

<html>
<body>

<?php
// Print individual cookies
echo $_COOKIE[“autosignin”];
echo “<br />”;
echo $_SESSION[“userid”];
echo “<br />”;
echo "sess email = ";
echo $_SESSION[“email”];
echo “<br />”;
echo “user id = $userid”;
echo “<br />”;
echo “email = $email”;
echo “<br />”;
echo $_COOKIE[“userid”];
echo “<br />”;

// Print all cookies
print_r($_COOKIE);
?>

</body>
</html>

For anyone interested, I was using Notepad++ to do the coding. There is an option in there to Encode the document with UTF-8 without BOM. When that is selected the problem goes away.

Immerse, actually you can set cookies like that for local use. It doesn’t change the value of the cookie on the users machine but will change the value on the server. On the first posting I made above, you will see that I use setcookie() and then assign the cookie value locally because it won’t be refreshed on the server until another page is called.

Is there an enter or a space before <?php ?
The should be absolutely nothing before <?php

As I said, I tried that. Here is the file and I still get the message.

<?php session_start(); ?>
<!DOCTYPE html>
<html lang=“en”>
<body>
<?php
// Print individual cookies
echo $_COOKIE[“autosignin”];
echo “<br />”;
echo $_SESSION[“userid”];
echo “<br />”;
echo "sess email = ";
echo $_SESSION[“email”];
echo “<br />”;
echo “user id = $userid”;
echo “<br />”;
echo “email = $email”;
echo “<br />”;
echo $_COOKIE[“userid”];
echo “<br />”;
// Print all cookies
print_r($_COOKIE);
?>
</body>
</html>

And here is the result from running that

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\htdocs\cookies.php:1) in C:\htdocs\cookies.php on line 1

34
sess email = uyuy
user id =
email =

Array ( [PHPSESSID] => 384f18bda1790f9303d699f439fc0588 )

Immense, I added a head with <meta charset=“utf-8”> and it is still doing the same thing. Now the code looks like:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang=“en”>
<head>

<meta charset=“utf-8”>
</head>
<body>
<?php
// Print individual cookies
echo $_COOKIE[“autosignin”];
echo “<br />”;
echo $_SESSION[“userid”];
echo “<br />”;
echo "sess email = ";
echo $_SESSION[“email”];
echo “<br />”;
echo “user id = $userid”;
echo “<br />”;
echo “email = $email”;
echo “<br />”;
echo $_COOKIE[“userid”];
echo “<br />”;
// Print all cookies
print_r($_COOKIE);
?>
</body>
</html>

Include doesn’t get you to a new page. Include does what the word says, include the content of another file in the current script being executed.

Also, as a matter of best practice, what is the difference between header and include in this case and which one should be used, i.e. when should you use header and when should you use include to get to another page.

You need to do session_start() BEFORE any output to the screen.


<?php session_start(); ?>
<!DOCTYPE html>

Edit: Ha, you beat me to it!

No, it’s on line 1 and there are no spaces before it. Could it be something from the page that is calling it as a header? Or I am assuming once I issue the header(location:… command it clears the headers, correct?

This is a copy of the code and it is all left justified.

<?php session_start(); ?>
<!DOCTYPE html>
<html lang=“en”>
<body>
<?php
// Print individual cookies
echo $_COOKIE[“autosignin”];
echo “<br />”;
echo $_SESSION[“userid”];
echo “<br />”;
echo "sess email = ";
echo $_SESSION[“email”];
echo “<br />”;
echo “user id = $userid”;
echo “<br />”;
echo “email = $email”;
echo “<br />”;
echo $_COOKIE[“userid”];
echo “<br />”;
// Print all cookies
print_r($_COOKIE);
?>
</body>
</html>

No matter where I put the session_start() in the cookies.php file, I get the message

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\htdocs\cookies.php:3) in C:\htdocs\cookies.php on line 4

And yes I tried it at the very top of the file, inside the php script with the echos, and where it is now and I get the same message all the time. Good news is I do get the session variable values now showing up but still not the cookie values.

<!DOCTYPE html>
<?php
session_start();
?>
<html lang=“en”>
<body>

<?php
// Print individual cookies
echo $_COOKIE[“autosignin”];
echo “<br />”;
echo $_SESSION[“userid”];
echo “<br />”;
echo "sess email = ";
echo $_SESSION[“email”];
echo “<br />”;
echo “user id = $userid”;
echo “<br />”;
echo “email = $email”;
echo “<br />”;
echo $_COOKIE[“userid”];
echo “<br />”;

// Print all cookies
print_r($_COOKIE);
?>

</body>
</html>

It doesn’t echo any of the session variables because cookies.php doesn’t call session_start();

Why it doesn’t show the cookies beats me …

You cannot set cookies in this manner:


$_COOKIE["userid"] = $userid;
$_COOKIE["autosignin"] = $autosignin;

Cookies should be set using the setcookie() function.

Edit>> just saw in the first post that both methods are used. Sorry!

Edit 2>>
Maybe the cookies.php file has a BOM in it? Try saving it as a UTF-8 file without the BOM (Byte Order Marker)

That’s not the very top of the file now is it?

<?php
session_start();
?>
<!DOCTYPE html>

:cool:

Using header, the cookie.php script is executed as a new page. You don’t have session_start() in it, so the session variables are not available.