Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My conditional needs a look over... 2

Status
Not open for further replies.

ITGL72

MIS
Jul 2, 2001
105
0
0
US
I have a page with just the following code in it:

<?php
if ($_COOKIE['company'] == 'HC')
{
header('Location: subscribeto_hc.php');
}
elseif ($_COOKIE['company'] == 'SVC')
{
header('Location: subscribeto_svc.php');
}
else
{
header('Location: subscribeto_sti.php');
}
?>



When I bring up this page I get errors like so:

Notice: Undefined index: company in C:\Accounts\stiesaco\ on line 2

Notice: Undefined index: company in C:\Accounts\stiesaco\ on line 6

Warning: Cannot modify header information - headers already sent by (output started at C:\Accounts\stiesaco\ in C:\Accounts\stiesaco\ on line 12





Very green at PHP, can someone direct me to the proper code syntax to make this work?
 
The "headers already sent" error will likely go away when you fix the first two.

The problem is that $_COOKIE['company'] is empty. If you attempt to use an uninitialized variable in a PHP conditional, you're going to get that error.

Take care of the case where $_COOKIE is empty first, then take care of the rest of the cases:

Code:
<?php
if (!isset($_COOKIE['company'])
{
	header('Location: subscribeto_sti.php');
}
else
{
	switch ($_COOKIE['company'])
	{
		case 'HC':
			header('Location: subscribeto_hc.php');
			break;
		case 'SVC':
			header('Location: subscribeto_svc.php');
			break;
		default:
			header('Location: subscribeto_sti.php');
	}
}
?>


Want the best answers? Ask the best questions! TANSTAAFL!
 
Don't worry sleipnir214...
I'll organise a star for you! :)

Reality is built on a foundation of dreams.
 
Didnt know I could do that. I overlooked it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top