littlepeg Posted May 17, 2007 Share Posted May 17, 2007 Hi everybody, would you please help me with this problem? Any help would be appreciated. I tried to put this php code into HTML webpage, however, I got error messages as follows: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\SNYP websites\loggedin.php:5) in C:\wamp\www\SNYP websites\loggedin.php on line 77 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\SNYP websites\loggedin.php:5) in C:\wamp\www\SNYP websites\loggedin.php on line 77 Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\SNYP websites\loggedin.php:5) in C:\wamp\www\SNYP websites\loggedin.php on line 89 Here is my php codes (It is strange that it works fine if I dont put it into a HTML web page) <?php # loggedin.php # User is redirected here from login.php. session_name ('YourVisitID'); session_start(); // Start the session. // If no session value is present, redirect the user. if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) { // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } $url .= '/index.php'; header("Location: $url"); exit(); // Quit the script. } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/ Share on other sites More sharing options...
ryeman98 Posted May 17, 2007 Share Posted May 17, 2007 If I understand correctly... Are you putting it in a .html page? If so, you need to make it .php If this isn't it then I don't understand what you're talking about. You can put html in a php file... Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255941 Share on other sites More sharing options...
littlepeg Posted May 17, 2007 Author Share Posted May 17, 2007 Sorry. I did not express clear. I did change it to .php. What I want to do is as follows: Here are some of the codes: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Log out</title> <style type="text/css"> <!-- .STYLE1 { font-size: 12px; font-family: "Times New Roman", Times, serif; font-weight: bold; color: #666666; } --> </style> </head> <body topmargin="0" bottommargin="0" background="images/bg.gif"> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> .... <?php // - logout.php session_name ('YourVisitID'); session_start(); // Access the existing session. // If no session variable exists, redirect the user. if (!($_SESSION['user_id'])) { // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } $url .= '/index.php'; // Add the page. header("Location: $url"); exit(); // Quit the script. } else { // Cancel the session. $_SESSION = array(); // Destroy the variables. session_destroy(); // Destroy the session itself. setcookie (session_name(), '', time()-300, '/', '', 0); // Destroy the cookie. } // Print a customized message. echo "<h1>Logged Out!</h1> <p>You are now logged out!</p> <p><br /><br /></p>"; ?> ..... </table> </body> </html> And the error messages are: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\SNYP websites\logout.php:5) in C:\wamp\www\SNYP websites\logout.php on line 77 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\SNYP websites\logout.php:5) in C:\wamp\www\SNYP websites\logout.php on line 77 Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\SNYP websites\logout.php:5) in C:\wamp\www\SNYP websites\logout.php on line 89 Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255945 Share on other sites More sharing options...
ryeman98 Posted May 17, 2007 Share Posted May 17, 2007 The session_start() needs to be put at the top of the page, before <html> and <head>. If you want to include a file, you should put session_start() before the include. Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255948 Share on other sites More sharing options...
littlepeg Posted May 17, 2007 Author Share Posted May 17, 2007 Hi ryeman98, thank you. I put session_start() before <html> and <head>, and two error messages are gone. however, there is still have the other error message, it is as follows: Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\SNYP websites\logout11.php:9) in C:\wamp\www\SNYP websites\logout11.php on line 89 How can I sorted out this problem? Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255950 Share on other sites More sharing options...
marf Posted May 17, 2007 Share Posted May 17, 2007 could you post your new modified code, and specifically the lines that the warnings point to? Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255953 Share on other sites More sharing options...
littlepeg Posted May 17, 2007 Author Share Posted May 17, 2007 Thank you. Here is the motified version. The orange color parts are the changes. And the red part is the problem code. <?php session_name ('YourVisitID'); session_start(); // Access the existing session. ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Log out</title> <style type="text/css"> <!-- .STYLE1 { font-size: 12px; font-family: "Times New Roman", Times, serif; font-weight: bold; color: #666666; } --> </style> </head> <body topmargin="0" bottommargin="0" background="images/bg.gif"> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> .... <?php // - logout.php // If no session variable exists, redirect the user. if (!($_SESSION['user_id'])) { // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } $url .= '/index.php'; // Add the page. header("Location: $url"); exit(); // Quit the script. } else { // Cancel the session. $_SESSION = array(); // Destroy the variables. session_destroy(); // Destroy the session itself. setcookie (session_name(), '', time()-300, '/', '', 0); // Destroy the cookie. } // Print a customized message. echo "<h1>Logged Out!</h1> <p>You are now logged out!</p> <p> </p>"; ?> ..... </table> </body> </html> Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\SNYP websites\logout.php:5) in C:\wamp\www\SNYP websites\logout.php on line 89 Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-255960 Share on other sites More sharing options...
ryeman98 Posted May 18, 2007 Share Posted May 18, 2007 hmm...I'm not too sure about this one but for now on could you please use the code thingy? <?php //Way easier to read the thread. ?> Quote Link to comment https://forums.phpfreaks.com/topic/51913-please-help-me-with-this-session-problem/#findComment-256065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.