Redirect

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
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Redirect

Post by dave_c00 »

I would like to simply run an if statement and if the condition is met goto another web page. Do i use the header function??

I am getting all sorts of errors when i use that.

Thanks

Dave
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Yes use the header function (as shown below) but notice this will only work if you have not output any headers already (which could be anything from a space in your file to an echo statement, etc). Basically this will only work if no other output has already been sent to the browser.

Code: Select all

<?
Header('Location: http://www.site.com/page.html');
exit;
?>
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Post by dave_c00 »

this is the error i am getting..

Warning: Cannot modify header information - headers already sent by (output started at /home/sites/site12/web/NewCustCosyData.php:15) in /home/sites/site12/web/NewCustCosyData.php on line 3

How can i get around it??

Thanks

Dave
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

launchcode wrote: but notice this will only work if you have not output any headers already (which could be anything from a space in your file to an echo statement, etc). Basically this will only work if no other output has already been sent to the browser.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Like I said (thanks Malcolm! :) ) - you are already outputting something in your script - some HTML or a space, whatever - it's causing the Header to fail.
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Post by dave_c00 »

Sorry for being such a novice but i need a page that simply opens up a new page if the value in the text field is correct and if is not just remains on the same page with an error message.

I have tried a number of different avenues and am getting quite annoyed.

Thanks

Dave
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Post your script - if you don't understand what "don't output any HTML before calling the Header function" means, then perhaps we can actually show you instead?
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Post by dave_c00 »

Initially I read a text file into an array:-

Code: Select all

&lt;?php 
$file = implode("",file("Eng_Internet_query.txt"));  
preg_match_all('/^(.*?),"(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"/ms',$file,$matches);
$worksord = $_POST&#1111;'worksord'];
?&gt;
Then i have a form to read in the works order number.

Code: Select all

&lt;div align="center"&gt;
  &lt;form action="&lt;?php print $PHP_SELF?&gt;"&gt;
  &lt;input type="text" name="worksord"&gt;
  &lt;input type="submit" value="Submit!"&gt;
  &lt;/form&gt;
  &lt;/div&gt;
Now i simply check the array for the worksord and if it is recognized i want to redirect to another page.

Code: Select all

<?php
if (in_array($worksord,$matches[3])){
Header("Location: http://www.website.com");
}else
{
print "Works Order number not found.";
}
?>
At what stage am i outputting headers??

Thanks

Dave



feyd | use

Code: Select all

and

Code: Select all

tags next time. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

<div align="center">
<form action="<?php print $PHP_SELF?>">
<input type="text" name="worksord">
<input type="submit" value="Submit!">
</form>
</div>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

At what stage am i outputting headers??
Outputting HTML automatically outputs a header. Your entire DIV Form block is HTML, ergo you've output headers.

Swap around the sequence - do the Header check BEFORE you output the Form.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
ob_start();
?>
put that at the top of page, for more info, read [php_man]ob_start[/php_man]
Post Reply