Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to redirect ?

Status
Not open for further replies.

RLyra

Programmer
Dec 6, 2002
21
BR
How can I redirect to a new page in PHP ? I am new on it.

The reason that I need (want) it is simple. I like to split pages. In one I put the code to insert and update data, other hand form and a last one hamd a list. Maybe not so smart, but make things clear.

But I need to redirect. How I can it with PHP ?

Thnaks from Brazil.

Robert Lyra
 
Well, it didn't work.

"Warning: Cannot modify header information - headers already sent by ..."

Are you sure that it re-direct the page ?? What I am trying to do is to execute a pge, but before end it, redirect to anathor page and show just this second one.
 
The above example redirects to the new page immediately. It is very important that nothing was sent (echo, print) to the browser before this command. This is what the error was warning you about. If you want to redirect after the page has shown something you will need to use html meta tag in the document head part

<meta http-equiv=&quot;refresh&quot; content=&quot;0;url=newpage.html&quot; />

or Javascript

<script>
document.location.href='newpage.html';
</script>

Both these examples will redirect immediately as well. You can set time in meta (content=&quot;0; change to number of seconds) or in JS (setTimeout function).
 
Here is some sample code for a login script that should give you an idea on how to use the header command.

Code:
<?php
	include(&quot;Connection.php&quot;);
	$strQuery = &quot;SELECT * FROM tblUsers WHERE username = '&quot;.$frmUsername.&quot;' AND password = '&quot;.$frmPassword.&quot;'&quot;;
	$row = mysql_query($strQuery) or die(&quot;Error&quot;.mysql_error());
	if(mysql_num_rows($row)) {
		
		header(&quot;Location:[URL unfurl="true"]http://location/of/page.php&quot;);[/URL]
		
	} else {
		print(&quot;<br><br><b>Sorry, this login is invalid!</b><br><br>Please <a href='login.php'>GO BACK</a> and try again.&quot;);
	exit;
	}		
?>

 
Well. I still have sone problems.

I really don't need it anymore, I have other solutions. But I want to understand what is going on.

$kco_tema = $HTTP_GET_VARS['f_kco_tema'];

This is the line which is sending information about the header. So when I get a variable from a form, I cannot redirect anymore ??

RLyra
 
That is the code:

<?php
include ('lib_function.php');



xBody('Inicio', '');
$kco_tema = $HTTP_GET_VARS['f_kco_tema'];
$no_tema = $HTTP_GET_VARS['f_no_tema'];
$de_tema = $HTTP_GET_VARS['f_de_tema'];

if (xSQL())
{
if ($kco_tema == 0)
{
$sql='Insert into temas values ('.$kco_tema.', &quot;'.$no_tema.'&quot;, &quot;'.$de_tema.'&quot;) ';
$msg='inserção';
}
else
{
$sql='Update temas set no_tema = &quot;'.$no_tema.'&quot;, de_tema =&quot;'.$de_tema.'&quot; where kco_tema = '.$kco_tema;
$msg='Alteração';
}
$result = mysql_query($sql);
if (!$result)
$msg = 'Erro em Acesso a banco:'.$msg;
else
$msg = 'Operação Ok:'.$msg;
//echo '<a href=&quot;temasLista.php?msg='.$msg.'&quot;>Lista</a>';
header(&quot;Location:}
xBody('Fim');
?>

and the funtion xBody is:
function xBody($acao, $conteudo='')
{
if ($acao == 'Inicio')
{
if ($conteudo == '')
{
print x('+').'<html>'.x('+').'<body>';
}
else
{
print x('+').'<html>'.x('+').'<head>'.x('+').'<title>'.x().$conteudo.x('-').'</title>';
print x('-').'</head>'.x('+').'<body>';
print x('+').'<h1>'.x().$conteudo.x('-').'</h1><br><br>';
}
}
elseif ($acao == 'Fim')
print x('-').'</body>'.x('-').'</html>';
else
print 'xBody ERRO ERRO ERRO Acao';
return ;
}

I am just studing PHP, and looking for a simple way to use it. And by the way, thanks for the time you are spending here.

Rlyra
 
At a quick glance at it, I noticed an extra ; after the url.

This line:
Code:
header(&quot;Location:[URL unfurl="true"]http://localhost/php/phin01/temasBd.php&quot;;);[/URL]

Should look like:
Code:
header(&quot;Location:[URL unfurl="true"]http://localhost/php/phin01/temasBd.php&quot;);[/URL]

 
I don't know what had happend, but that &quot;;&quot; was not in the original code. I know that it is in my mail, but it is not in the original code, I am sure.


Again, to avoid other problems, this is my code:

<?php
include ('lib_function.php');



xBody('Inicio', '');
$kco_tema = $HTTP_GET_VARS['f_kco_tema'];
$no_tema = $HTTP_GET_VARS['f_no_tema'];
$de_tema = $HTTP_GET_VARS['f_de_tema'];

if (xSQL())
{
if ($kco_tema == 0)
{
$sql='Insert into temas values ('.$kco_tema.', &quot;'.$no_tema.'&quot;, &quot;'.$de_tema.'&quot;) ';
$msg='inserção';
}
else
{
$sql='Update temas set no_tema = &quot;'.$no_tema.'&quot;, de_tema =&quot;'.$de_tema.'&quot; where kco_tema = '.$kco_tema;
$msg='Alteração';
}
$result = mysql_query($sql);
if (!$result)
$msg = 'Erro em Acesso a banco:'.$msg;
else
$msg = 'Operação Ok:'.$msg;
//echo '<a href=&quot;temasLista.php?msg='.$msg.'&quot;>Retorna</a>';
header(&quot;Location:}
xBody('Fim');
?>




function xBody($acao, $conteudo='')
{
if ($acao == 'Inicio')
{
if ($conteudo == '')
{
print x('+').'<html>'.x('+').'<body>';
}
else
{
print x('+').'<html>'.x('+').'<head>'.x('+').'<title>'.x().$conteudo.x('-').'</title>';
print x('-').'</head>'.x('+').'<body>';
print x('+').'<h1>'.x().$conteudo.x('-').'</h1><br><br>';
}
}
elseif ($acao == 'Fim')
print x('-').'</body>'.x('-').'</html>';
else
print 'xBody ERRO ERRO ERRO Acao';
return ;
}
 
I DIDN'T DO THAT!!!

This forum, I don't know why, is writting that &quot;;&quot;. I am sure that the code I copy DIDN'T have any &quot;;&quot;. I still have it on the notepad (I am using the Zend Studio, I just copy it to the note pad).

Well, I think that the forum have some problem with URL and &quot;&quot;. Believe, that &quot;;&quot; don't exist here.
 
when i use the header() function, i always get the problem you are getting. you should put these two lines in your code. the first should go at the top of all your code BEFORE you ANY code and the second should go at the end of your code AFTER ALL of your code.

//at the beggining of all of your code
ob_start();


//at the end of all your code
ob_end_flush();

this will clear the output buffer for you, which If i understood it correctly a few years ago when i first was told about it, is the problem you're getting.


Hope this helps
 
JCour82:
Although use of output buffering will work around the &quot;headers already sent&quot; problem, it does so at the expense of greater server resource utilization. The output for every connection must be held in memory until streamed to the client. On busy sites, this can cause problems.

It's better to restructure code to not send content before headers.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok, here is the code again. Without problems I hope.

Code:
<?php
include ('lib_function.php');


	
xBody('Inicio', ''); 
$kco_tema = $HTTP_GET_VARS['f_kco_tema'];
$no_tema = $HTTP_GET_VARS['f_no_tema'];
$de_tema = $HTTP_GET_VARS['f_de_tema'];

if (xSQL())
{
	if ($kco_tema == 0)
	{
		$sql='Insert into temas values ('.$kco_tema.', &quot;'.$no_tema.'&quot;, &quot;'.$de_tema.'&quot;) ';
		$msg='inserção'; 
	}
	else 
	{
		$sql='Update temas set  no_tema = &quot;'.$no_tema.'&quot;, de_tema =&quot;'.$de_tema.'&quot; where kco_tema = '.$kco_tema;
		$msg='Alteração'; 
	}
	$result = mysql_query($sql);
	if (!$result)
		$msg = 'Erro em Acesso a banco:'.$msg;
	else 
		$msg = 'Operação Ok:'.$msg;
	//echo '<a href=&quot;temasLista.php?msg='.$msg.'&quot;>Retorna</a>';
	header(&quot;Location:[URL unfurl="true"]http://localhost/php/phin01/temasBd.php&quot;);[/URL] 
}
xBody('Fim'); 
?>




function xBody($acao, $conteudo='')	
{
	if ($acao == 'Inicio')
	{
		if ($conteudo == '')
		{
		print x('+').'<html>'.x('+').'<body>';
		}
		else 
		{
		print x('+').'<html>'.x('+').'<head>'.x('+').'<title>'.x().$conteudo.x('-').'</title>';
		print x('-').'</head>'.x('+').'<body>';
		print x('+').'<h1>'.x().$conteudo.x('-').'</h1><br><br>';
		}
	}
	elseif ($acao == 'Fim')
		print x('-').'</body>'.x('-').'</html>';
	else 
		print 'xBody ERRO ERRO ERRO Acao';
	return ;
}

Tahnks again for your time and attention.

rLyra
 
Well, if you inspect your code closely, you will notice that you are sending something to the browser before the header function. Right at the top you call the function xBody which prints <html><body>. That means that when the header function is called this is already printed in the file and what we were saying was absolutely nothing must be before header. If you comment out the xBody function call your redirect should work. If it doesn't, check for any whitespace (space or enter) before the opening <?php. Hope it helps.
 
Try running the script at the command prompt to see if you can identify the stuff being printed. Vragbound is more than lilely correct. If you search through these forums literaly hundereds of posts are raised about HTTP header problems (including cookies) and they all come down to printed output (even a space).

To run php at the command prompt (under windows for me) I just do c:\php\php script.php

You will see what php pumps out, including the HTTP headers. ignore the two blank lines, the stuff after this is what is being sent. This doesn't always trap the problem but often it does. If you want to supress the HTTP output at the command line type c:\php\php -q script.php
 
Ok, the function xbody sends &quot;<html><body>&quot;.

But if I remove it, or if I put it after all, after the HEADER function, I got anather erro.

I get an erro when I try to get values from a FORM:
$kco_tema = $http_GET_VARS['f_kco_tema'];

The message is:

Notice: Undefined index: f_kco_tema in c:\inetpub\ on line 7

So, with &quot;<body>&quot; no redirect. But withou it, no values from FORMS?
 
I think after you have issued the header your error may well be coming from the redirected page. Looking at your code the location you are redirecting to is the one dislayed in the error message i.e. teamsBd.php)which unless the original page is also teamsBd.php possibly conforms my guess.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top