Page 1 of 1

header errors with sessions!

Posted: Thu Apr 24, 2003 4:44 am
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!

Posted: Thu Apr 24, 2003 4:51 am
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