Cookie Setting Prob

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
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Cookie Setting Prob

Post by Ruski »

Im making a simple login form and everything works perfect exept for setting the cookie, it shows this error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/rsneebn/public_html/connect.php:9) in /home/rsneebn/public_html/logins.php on line 64

Code: Select all

line 64: setcookie("rscheetah_cookie", serialize(array($user, $pass)), time() + 31536000, '/', '', 0);
I never seen an error like that before.

Thanks in advance
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You've got output somewhere above that (content sent back to the browser)...

The cookie is set within the headers and if they have already been sent you get errors.

It might just be a chunk of whitespace or something....

Search these forums for "Headers already sent" and you'll get a tonne of posts.
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Post by Ruski »

is setcookie funtion a header call?
as i checked my connect.php and the script where setcookie is called, i dont have any white space or outputs. Here is my connect.php:

Code: Select all

<?php 
include 'mysql.php'; 
$db = new DBLayer('localhost', 'user', 'pass', 'database', '', false);
?>
here is my script that sets the cookie:

Code: Select all

<?
include 'methods.php';
$do = $_GET['act'];
$user = trim($_POST['username']);
$pass = trim($_POST['password']);
if($do == 'verify')
{
	if($user == '' || $pass == '')
	{
		alert('Please type both username and password to login!');
		redirect('http://www.rscheetah.com/logins.php');
	}	
	$find = $db->query('SELECT * FROM users WHERE username=\''.$user.'\'');
	$finder = $db->fetch_assoc($find);
	if($finder['password'] != $pass)
	{	
		alert('Wrong username or password!');	
		redirect('http://www.rscheetah.com/logins.php');
	}
	else
	{		
	          	setcookie("rscheetah_cookie", serialize(array($user, $pass)), time() + 31536000, '/', '', 0);					 
			alert('Logged on successfully');
			redirect('http://www.rscheetah.com/logins.php');	
	} 
}
elseif($do == 'panel')
{
	echo '<label class="main">Cant find act specified</label>';
}
?>
Thanks for your help though
Post Reply