header errors with sessions!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gasoga
Forum Commoner
Posts: 36
Joined: Thu Apr 17, 2003 4:15 am
Location: Ireland

header errors with sessions!

Post by gasoga »

Hi
I keep gettin header errors with using sessions! I want to check that when a user logs in and a seesion is created that when the users go to a page that can only be viewed by user the session is still there and that user is still valid.

the code to create the session in my login form is

Code: Select all

<?php$IP  = $REMOTE_ADDR;
		$Q02 = "SELECT *
				FROM $dbtable 
				WHERE UserName='$UserName'";
		$R02 =  odbc_exec($db,$Q02) or die("Bad Q02:".mysql_error()); 
		$Info = odbc_fetch_row($R02,1);
		$SmartID = $Info['ID'];

		$Q03 = "UPDATE $dbtable
				SET IPAddress='$IP' 
				WHERE UserName='$UserName'";
		$R03 =  odbc_exec($db,$Q03) or die("Bad Q03:".mysql_error()); 
	
		$Expire = 365*24*3600; 
		setcookie("UserName","$UserName",time()+$Expire,"/","");
		setcookie("SmartID","$SmartID",time()+$Expire,"/","");    
		session_start();
		session_register('UserName');
		session_register('SmartID');
		$_SESSION['UserName'] = $UserName;
		$_SESSION['SmartID']  = $SmartID;
		header("Location: DeftCallLog.php");



?>
And when the user logs in and goes to the first page the code at the top of the page to check if the user is valid is :

Code: Select all

<?php

<?PHP
	include("functions.php");
	global $SmartID;
	if (!$HTTP_COOKIE_VARS['UserName'] || !$HTTP_COOKIE_VARS['SmartID'])
		{
		session_start();
		if(!session_is_registered('UserName') || !session_is_registered('SmartID'))
    		{
			$ErrorCode = "The Page You Requested Is For Members Only<BR><BR>Please Login Or <A HREF='register.php'><B>Register</B></A>";
    		ViewHeader($SmartID);
			//ViewLoginForm($UserName, $ErrorCode);
    		ViewFooter();
    		exit;
    		}
		}

	$db = odbc_connect($dbsource, $dbuser, $dbpass) or die(odbc_errormsg);
	$Q01 = "SELECT * 
			FROM $dbtable 
			WHERE ID='$SmartID'";
	$R01 =  odbc_exec($db,$Q01) or die("Bad Q01:".mysql_error()); 

	ViewHeader($SmartID);
?>


?>
The error im gettin is:

Cannot send session cookie - headers already sent in F:\Support on line 6 PHP Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent in F:\Support on line 6

I dont understand it because i have no spaces etc!
Thanx for any help!
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

i think you should read the sticky posts at the top of this forum *grin*
the php manual page would do you well also php.net/sessions should get you there
Post Reply