Jump to content

Header Already Sent...


dtyson2000

Recommended Posts

Ever have one of those days when you've looked and looked and tried and tried, yet everything has become totally jumbled and backwards and the obvious answer to a problem, which is probably staring you right in the face but you just don't see it? Yeah, that's me today/yesterday...

 

I've been using this script (all one file... no separate form/processing files) for a year and everything has worked without a problem --- until I noticed that session_register() was deprecated and I made the changes necessary to no longer use it.

 

I am getting a "Cannot modify header information - headers already sent" error, which I understand but I canNOT get past it since the addition of the isset($_SESSION condition.

 

I need this script to check the date and depending on the last login date in the table either show a full list or a partial list. Then, once the user has clicked their name, insert their name and the time into a table, which is immediately followed by a "header: location" to avoid duplicate entries should someone click the reload button and to show who has not clicked their name yet today. The script isn't making it by that line since the addition of the isset($_SESSION condition.

 

The insert into the database happens but I immediately get the "header already sent" error as a result and I no longer get the list of names that should be returned. Can someone please have a look at this and maybe offer some guidance explaining where I might put the "header: location" to get that reload to work without throwing the error?

 

This is killing me.  :confused: And thank you so much for taking the time to think about it!

 

Here's a sanitized version of the code:

 


<?php 
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['password'])) {

	include ('header.php');

	echo "<p>logged in as: ". $_SESSION['login'];	

	checkdate();

	include ('footer.php');

	}

	else {

		header("location: login.php");

		}

function checkdate() {

// check current date

include ('config.php');
include ('opendb.php');

$today = date('Y-m-d');

$query = "SELECT * FROM datetable DESC limit 1";

$result = mysql_query($query) or die ("Couldn't execute query");

while ($row= mysql_fetch_array($result)) {

	$date = $row["date"];		

	switch(TRUE) {

		case ($date == $today) :

			if ($_SERVER['REQUEST_METHOD'] == "POST") {

				$userid = mysql_real_escape_string($_POST["userid"]);					

				mysql_query ("INSERT INTO datetable (userid, date) VALUES ('$userid', DATE_ADD(NOW(), INTERVAL 1 HOUR))");

				header("location: index.php");

			}

			else {

			userlogin();

			}
			break;

		case ($date != $today) :

			if ($_SERVER['REQUEST_METHOD'] == "POST") {

				$userid = mysql_real_escape_string($_POST["userid"]);

				mysql_query ("INSERT INTO datetable (userid, date) VALUES ('$userid', DATE_ADD(NOW(), INTERVAL 1 HOUR))");

				header("location: index.php");

			}

			else {

			userlist();

			}

			break;
	}
}
}

function userlist() {
//	form that shows all users
}

function userlogin() {
//	another form that shows only users who haven't logged in today
}
?>


Link to comment
https://forums.phpfreaks.com/topic/264932-header-already-sent/
Share on other sites

u make output

 

echo "<p>logged in as: ". $_SESSION['login'];

then u are using header in ur function

try to replace them

session_start();
if(isset($_SESSION['login']) && isset($_SESSION['password'])) {
	checkdate();
	include ('header.php');		
	echo "<p>logged in as: ". $_SESSION['login'];			
	include ('footer.php');

	}
...

Link to comment
https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357671
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.