开发者

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: ' .开发者_JS百科 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 ?


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


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


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.


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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜