开发者

php - How to solve this setcookie issues

Here is the web structure of my page:

01.<?php // rnlogout.php
02.include_once 'rnheader.php';
03.echo "<h3>Log out</h3>";
04. 
05.if (isset($_SESSION['user']))
06.{
07.    destroySession(); // report error here!!!!!!
08.    echo "You have been logged out. Please
09.    <a href='index.php'>click here</a> to refresh the screen.";
10.}
11.else echo "You are not logged in";
12.?>

function destroySession()
{
    $_SESSION=array();

    if (session_id() != "" || isset($_COOKIE[session_name()]))
        setcookie(session_name(), '', time()-2592000, '/');

    session_destroy();
}

01.<?php // rnheader.php
02.include 'rnfunctions.php';
03.session_start();
04. 
05.if (isset($_SESSION['user']))
06.{
07.    $user = $_SESSION['user'];
08.    $loggedin = TRUE;
09.}
10.else $loggedin = FALSE;
11. 
12.echo "<html><head><title>$appname";
13.if ($loggedin) echo " ($user)";
14. 
15.echo "</title></head><body><font face='verdana' size='2'>"; // called first!!!!!
16.echo "<h2>$appname</h2>";
17. 
18.if ($loggedin)
19.{
20.    echo "<b>$user</b>:
21.         <a href='rnmembers.php?view=$user'>Ho开发者_JAVA百科me</a> |
22.         <a href='rnmembers.php'>Members</a> |
23.         <a href='rnfriends.php'>Friends</a> |
24.         <a href='rnmessages.php'>Messages</a> |
25.         <a href='rnprofile.php'>Profile</a> |
26.         <a href='rnlogout.php'>Log out</a>";
27.}
28.else
29.{
30.    echo "<a href='index.php'>Home</a> |
31.         <a href='rnsignup.php'>Sign up</a> |
32.         <a href='rnlogin.php'>Log in</a>";
33.}
34.?>

Please take a look the line 07

destroySession(); // report error here

Based on the php manual, http://us2.php.net/setcookie cookies must be sent before any output from your script. Here the rnheader.php call echo even before the setcookie call in function destroySession.

So how do I change the page structure to fix this problem?

The error message is as follows:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\LPMJ_examples\robinsnest\rnheader.php:12) in C:\xampp\htdocs\LPMJ_examples\robinsnest\rnfunctions.php on line 41

Thank you


read the PHP-Manual :: Output Control

if you don't want to rewrite your scripts use output control functions (as mention above)

<?php
ob_start();
include_once 'rmheader.php';
echo '<h3>Log Out</h3>';

if (isset($_SESSION['user'])) {
    destroySession(); // report error here!!!!!!
    echo "You have been logged out. Please <a href='index.php'>click here</a> to refresh the screen.";
} else {
    echo "You are not logged in";
}

function destroySession()
{
    $_SESSION=array();

    if (session_id() != "" || isset($_COOKIE[session_name()]))
        setcookie(session_name(), '', time()-2592000, '/');

    session_destroy();
}

ob_end_flush();
?>


The function destroySession() has to be set before you call it, so place the defined function at the top after the <?php which would work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜