tags:

views:

68

answers:

7

I am creating a simple website . My situation is like this. After registering an user, I want to redirect the user after say 3 seconds to a main page(if the registration succeeds) . The code I have now is as below

$query = "INSERT INTO Privileges VALUES('$user','$password1','$role')";

$result = mysql_query($query, $dbcon) 
 or die('Registration Failed: ' . mysql_error());
print 'Thanks for Registering , You will be redirected shortly';

ob_start();

echo "Test";

header("Location: http://www.php.net");

ob_flush()

I get the error message Warning: Cannot modify header information - headers already sent by (output started at/home/srinivasa/public_html/ThanksForRegistering.php:27) in /home/srinivasa /public_html/ThanksForRegistering.php on line 35.

What do I need to do now ?

+1  A: 

For starters, remove echo "Test"; and the print statement and let us know what happens.

Ian P
His issue is with issuing http headers after he has already sent content. Feel free to re-read his question and un-down vote me.
Ian P
Ian is correct here...
Zak
@Ian P: It is not an answer, it should be a comment.
Web Logic
It is an answer, as it solves the question he is asking.
Ian P
Web Logic. Your answer shouldn't even be in anyone's mind.I mean, I don't like to be rude, but this is too much. Ian's answer actually does solve the problem, albeit not exactly how OP would like.
Arda Xi
@Ian P : After removing the Print and echo statements I am redirected . It solves part of my problem.Now to display the user Thanks for registering message , what should I do?
Eternal Learner
Eternal Learner - I would suggest redirecting them to a page that says something like "You are now registered." In that HTML page, I'd include <meta http-equiv="refresh" content="3;http://whateversite.com/mainpage.php"> in the <head> element.
Ian P
+1  A: 

You can't do this from the PHP, as that all gets evaluated before the page gets sent to the client. What you can do is output some JavaScript which does the redirection after 3 seconds pass.

It would look something like this (I don't know PHP very well so I'm not sure what the ob_start and ob_flush do):

$query = "INSERT INTO Privileges VALUES('$user','$password1','$role')";

$result = mysql_query($query, $dbcon) 
 or die('Registration Failed: ' . mysql_error());
print 'Thanks for Registering , You will be redirected shortly';
print '<script type="text/javascript">'
print 'setTimeout(function() { window.location = "http://www.php.net"; }, 3000);'
print '</script>'

ob_start();
ob_flush();
Claudiu
The ob functions do output buffering. Basically, when start is called, output is stored in a buffer, it is sent when ob_flush is called. If the OP would have put the ob_start() at the beginning, there wouldn't have been a problem, because it buffers the output and sends the headers before any content.
Arda Xi
@Arda: Wouldn't that have done the redirect without displaying any "Thanks for Registering" text, though?
Claudiu
True, but the problem (error) would be gone.
Arda Xi
A: 

You can't echo 'test' or print any other information to the browser then set a redirect header... If you send text a header for text/html is sent to the browser, followed by content.

The only way to redirect after non-header content is sent is via javascript. So if you want to tell the client something before you redirect, you can do 1 of 2 things.

1) send javascript on the page that will count for 3 seconds, then do the redirect

2) set a refresh header (before echoing anything) to refresh the same page, and redirect when the refresh happens.

<?php

//  refresh / redirect to an internal web page
//  ------------------------------------------
header( 'refresh: 5; url=/webdsn/' );
echo '<h1>You will be re-directed in 5 seconds...</h1>';

From a quick google search at this location: http://www.desilva.biz/php/phprefresh.html

Zak
you're right.. Edited to not call all the people ignoring the "Warning: cannot modify header information- headers already sent" warning retards.
Zak
People sure are creative when it comes to trying to do redirects. Unfortunately, there's no `refresh` header in the HTTP spec.
Arda Xi
I do have to state, I haven't used the refresh header in a long time, but just remembered it.. Wikipedia says it is something introduced via the original netscape browser :http://en.wikipedia.org/wiki/URL_redirection#Refresh_Meta_tag_and_HTTP_refresh_headerAnd is not part of the HTTP spec.
Zak
+1  A: 

In order to redirect the user after 3 seconds, you have two options.

First, remove the header function.

Then you can either use Javascript:

<script type="text/javascript">
    setTimeout('function() {window.location = "http://example.com";}',3000);
</script>

Or a meta refresh tag in the head section:

<meta http-equiv="refresh" content="3;url=http://example.com/" />

You can't do it with PHP alone.

Arda Xi
got to fix up your JavaScript there, it won't do anything as it is. If `redirect()` does somehow get called, then it'll just keep calling itself every 3 seconds.
Claudiu
I realised that right before you left the comment and fixed it.
Arda Xi
A: 

Use javascript instead so that you can make sure the text prints on screen.

You can use the method here as one option.

Make sure to include a link for them to click on if for some reason javascript does not work in their browser.

Joseph
+1  A: 

I guess the question is 'why are you redirecting?'. If you want to send the user on to the page they wanted before they had to register AND still give them a friendly "Thanks for registering" message, you might consider putting the success message into a $_SESSION variable and echoing it out on the target page.

Something like:

//process registration
if($success) {
  $_SESSION['reg_success']="You're one of us now!";
  header("Location: http://www.php.net"); 
  exit;
}
//otherwise have another go at registering
dnagirl
Or maybe something like $_SESSION['message'] so that failure can be sent as well.
Arda Xi
A: 

Your problem is that output is created before you try to modify your header. Try reordering your code like this.

$query = "INSERT INTO Privileges VALUES('$user','$password1','$role')";

ob_start();

$result = mysql_query($query, $dbcon) 
 or die('Registration Failed: ' . mysql_error());
print 'Thanks for Registering , You will be redirected shortly';


echo "Test";

header("Location: http://www.php.net");

ob_flush();

This rectifies your header error message, but you're going to have to use one of the other solutions posted to do the redirect bit with javascript or meta tags

iolo
Doesn't redirect after showing the message like he wants though.
Arda Xi