header ERROR, when checking user validity

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
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

header ERROR, when checking user validity

Post by bironeb »

When Checking for the Validity of user and valid != "yes" i get this error:

Warning: Cannot modify header information - headers already sent by (output started at c:\apache group\apache\htdocs\inventory\do_delrecord.php:3) in c:\apache group\apache\htdocs\inventory\do_delrecord.php on line 4

Here is the code:

Code: Select all

//check for validity of user
if ($_SESSION['valid'] != "yes") {
	header("Location:http://byron/inventory/inventory_menu.php");
	exit;
}

Any Ideas? or possible better suggestions to check for validity ?
User avatar
allenmak
Forum Newbie
Posts: 8
Joined: Sat Mar 13, 2004 9:53 am
Location: Hong Kong

Post by allenmak »

Have you got any spaces before having <?php?
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Yes, This is the top part of my code:

Code: Select all

<?
//check for required form variables
if (!$_POST['user_name']) {
	header("Location:http://byron/inventory/pick_delrecord.php");
	exit;
} else {
	//if form variables are present,start a session
	session_start();
}

//check for validity of user
if ($_SESSION['valid'] != "yes") {
	header("Location:http://byron/inventory/inventory_menu.php");
	exit;
}
?>
User avatar
allenmak
Forum Newbie
Posts: 8
Joined: Sat Mar 13, 2004 9:53 am
Location: Hong Kong

Post by allenmak »

Please delete them.
It is because the spaces before <?php will be considered as webpage content.
You cannot send any webpage content before header.
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Code: Select all

<?php<? if ($_SESSION['valid'] != "yes") {
	header("Location:http://byron/inventory/inventory_menu.php");
	exit;
}
//check for required form variables
if (!$_POST['user_name']) {
	header("Location:http://byron/inventory/pick_delrecord.php");
	exit;
} else {
	//if form variables are present,start a session
	session_start();
}

//set up table and database names
$db_name ="Inventory";
$table_name ="client_hardware";

//connect to server and select database
$connection =@mysql_connect("localhost","byron","byronb") or die(mysql_error());
$db =@mysql_select_db($db_name,$connection) or die(mysql_error());

//build and issue query
$sql = "DELETE FROM $table_name WHERE user_id ='".$_POST['user_id']."'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Inventory Management System: Record Deleted</TITLE>
</HEAD>
<BODY bgcolor="B1D3EC">
<h2><em>Delete a Record - Record Deleted</em></h2>
<P><? echo "$_POST[user_name] $_POST[user_id]"; ?> has been deleted from <? echo "$table_name"; ?></p>
<br><p><a href="inventory_menu.php">Return to Main Menu</a></p>
</BODY>
</HTML>

?>

I changed the top two around and still get the errors.. I have no spaces before <? and i get this:

Notice: Undefined variable: _SESSION in c:\apache group\apache\htdocs\inventory\do_delrecord.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at c:\apache group\apache\htdocs\inventory\do_delrecord.php:1) in c:\apache group\apache\htdocs\inventory\do_delrecord.php on line 2
User avatar
allenmak
Forum Newbie
Posts: 8
Joined: Sat Mar 13, 2004 9:53 am
Location: Hong Kong

Post by allenmak »

may i know what

Code: Select all

<?php
$_SESSION['valid'] != "yes"
?>
is?
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

It is the validity of my session in the cookie... does this answer your question?
User avatar
allenmak
Forum Newbie
Posts: 8
Joined: Sat Mar 13, 2004 9:53 am
Location: Hong Kong

Post by allenmak »

I guess the header problem has already been solved.
But because your $_SESSION["valid"] is not defined. It shows error message which makes PHP parse out some content before the header is sent out.
User avatar
bironeb
Forum Commoner
Posts: 59
Joined: Thu Nov 20, 2003 12:02 pm

Post by bironeb »

Hmmm... thats very interesting... I may have to go at it another way maybe. Any Ideas?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

if(isset($_SESSION['valid']))
if($-SESSION['valid'])
header(whatever)

And I would use the reserved words true and false or 1 and 0 instead of 'yes' and 'no', that way you can simply write

if($_SESSION['valid']);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

doesnt session_start(); have to be on line1?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Phenom wrote:doesnt session_start(); have to be on line1?
Yup, without session_start() you can't access the session variables.

Mac
Post Reply