goto URL

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
soundjam
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2008 1:20 pm

goto URL

Post by soundjam »

Good afternoon,
I am new to the PHP lang all together.
I am wondering if anyone can help me out?

What I am trying to do is, on my index page have users answer the question of what their location is.
Then, pass this information off to location.php where it will direct the user to one of two web pages.
Any Ideas? and Thank you in advance,
Brian

index.html
<html>
<head></head>
<body>
<h2>Please Select Your Location</h2>
<p>
<form method="get" action="location.php">
<select name="location">
<option value="1">Ontario - Canada
<option value="2">Other Provinces - Canada
</select>
<input type="submit" value="Send">
</form>
</body>
</html>


location.php code

<?php
// get form selection
$location = $_GET['location'];
// check value and select appropriate item
if ($location == 1) {
$special = 'Location: http://www.url_A.com';
}
elseif ($location == 2) {
$special = 'Location: http://www.url_B.com';
}
?>
<?php header( $special); ?>
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: goto URL

Post by sparrrow »

Well the code you posted looks like it will work in theory. Are you getting an error or unexpected result?
soundjam
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2008 1:20 pm

Re: goto URL

Post by soundjam »

this is the error that comes up:
Warning: Cannot modify header information - headers already sent by (output started at /home/bria4964/public_html/location.php:3) in /home/bria4964/public_html/location.php on line 17

I cannot get it to forward.
Brian
soundjam
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2008 1:20 pm

Re: goto URL

Post by soundjam »

I just remembered hearing that spaces or empty line feeds above the <?php line should not be there..
I have removed them and it's working, Thank you for your reply.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: goto URL

Post by sparrrow »

You can clear out any printed data before the header data is executed like you say you did and that works. Some times this is impractical though, and you can put this on the first line of the page:

Code: Select all

 
<?php
ob_start();
?>
 
This will allow headers to be modified after the page is rendered.
Post Reply