Redirecting to a new page AFTER headers has been set

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
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Redirecting to a new page AFTER headers has been set

Post by robster »

Is there a way to do this?

I have code that tells the user a bunch of stuff, then if certain conditions are met I want the page to create a session and move onto another page (which is page 2 of a signup / registration script, for those interested :)).

I have to output text to let the user know certain things and I have to be able to redirect if a condition is met.

So far, at the end of my script I have this:

Code: Select all

if ($pass = true)
		{
		//redirect
		}
		else
		{
		//error
		}
Any suggestions?
I'd appreciate anything :)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

If I understand correctly then you're going to have to use some javascript or similarly capable client side language. If you describe your situation a little bit more I can probably offer more insight into solutions.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Basically, the code is a page that accepts form data.
It then goes through the form data, does checks to see if username is correct, if the email address has been taken/is correct, if their birthdate makes them old enough to use the forums etc. When that's done it then shows them their input and or any errors.

If there's errors it re-directs them back to the input page they came from (with highlited errors) or if it was error free it will re-direct them forward to page 2 of registration.

It seems you're most likely correct, I'll have a hunt around for some java scripty stuff (damn).

Thanks for the advice :)

Rob
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Why have you had to send headers then?
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Well, I've output info to the browser in the checks. I presume that this will send headers as I'm getting a headers sent error.

Actually, I just removed all output and I still get the error, strange. Very strange....
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I'm still a little unsure of what's going on, but I'll assume I'm not.

If you want to validate form data you can do the following:

Code: Select all

<?php
if ($_POSTї'doSubmit']) {
  // The form has been submitted, from here we can proceed to validate individual fields:
  if ($_POSTї'requiredField1')) {
    // requiredField1 has a value
    // send user to the next form:
    header(&quote;Location: form2.php&quote;);
  } else {
    // requiredField1 has no value
    // here you can set some error variable that can be used later to tell the user that she didn't complete the form
  }
}
?>
<form>
<input type=&quote;text&quote; name=&quote;requiredField1&quote; />
<input type=&quote;submit&quote; name=&quote;doSubmit&quote; />
</form>
Does this make sense?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

As a note.
You cannot send headers after you have echoed data, furthermore, I really really don't see why you would ever want that with redirect, as doing a redirect would never show what has been echoed, which would just waste bandwidth.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Don't output things in the same script you're doing checks in. Do the checks and if an error is hit, redirect somewhere else.

I always write scripts dedicated to validation, login, logout etc etc... these scripts don't output anything at all... they just process data... use other scripts which can be controlled via the above to handle data output.

Note: Don't rely on javaScript if your website will not function correctly without it neither.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

This is some great feedback and advice.

What I'll aim to do is seperate error checking / processing from output (it's almost like that already) and I think I'll use sessions to do it (as I can't stand doing a tonne of $_GET's ;)).

Most of my scripts written for the site actually are seperate dedicated login/logout etc scripts, I guess this time I tried to get too tricky in one script, and for no reason at all. That will be my next mission then ;) Get this particular script more modular in its design and execution.

Thanks again everyone. I'll begin with that code snippit and go from there.
Post Reply