Eternal Learner
Eternal Learner

Reputation: 3870

Problem while redirecting user after registration

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 ?

Upvotes: 2

Views: 1260

Answers (7)

Iolo
Iolo

Reputation: 2232

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

Upvotes: 0

dnagirl
dnagirl

Reputation: 20446

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

Upvotes: 1

Joseph
Joseph

Reputation: 1963

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.

Upvotes: 0

Arda Xi
Arda Xi

Reputation: 2666

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.

Upvotes: 1

Zak
Zak

Reputation: 25215

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

Upvotes: 0

Claudiu
Claudiu

Reputation: 229461

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();

Upvotes: 1

Ian P
Ian P

Reputation: 12993

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

Upvotes: 2

Related Questions