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 Wanet Telecoms Ltd 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 to a new page

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
PH
im encountering a problem on the page im working right now in which it should be redirected to a new page, whenever im using the header(Location...) im encountering an error message which is:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\wppp\login.php:6) in C:\xampp\htdocs\wppp\login.php on line 171

im using wamp server before and i never encounter this problem but right now im running it in xampp and having this problem....

here is my code:

Code:
<?php 
	$errors=array();
	if(isset($_REQUEST['submit']))
	{
		validate_input();
		if(count($errors) != 0)
		{
			display_form();
		}

		if(count($errors) == 0)
		{
				include "connection_dbase_settings.php";
				$user_name = $_POST['username'];
				$pass_word = $_POST['password'];
				$result1 = mysql_query("Select * from tbl_users where username='$user_name' and password='$pass_word'") or die(mysql_error());
				$num = mysql_num_rows($result1);
						
				<?php } 
   		
				if($num != 0)
				{
							header("Location: [URL unfurl="true"]http://www.yahoo.com");[/URL]
								}
				else
				{
				header("Location: [URL unfurl="true"]http://www.google.com");[/URL]
				}
		}
	}
	else
	{
		display_form();
	}
	
	function validate_input()
	{
		global $errors;
		if($_POST['username'] == "")
		{
			$errors['username'] = "<font color='red' size='1'>* Username required</font>";
		}
		if($_POST['password'] == "")
		{
			$errors['password'] = "<font color='red' size='1'>* Password required</font>";
		}
	}


	function display_form()
	{
		global $errors;
?>

<?php echo $errors['username']; ?><br />
<?php echo $errors['password']; ?><br />

<center>

<br /><table width=351 border="1" bordercolor="#e6cda5" cellspacing="2" cellpadding="2">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			  <tr>
              		<td width=113 height="24" class="t_table">Username:</td>
<!--width="23%" width="41%"width="37%"-->
                    <td width=218 class="t_table"><input type="text" name="username" value="<?php echo $_POST['username'] ?>" size=30/></td>
       	      </tr>
	          <tr>
                 	<td width=113 height="24" class="t_table">Password:</td>
                 	<td><input type="password" name="password" value="<?php echo $_POST['password'] ?>" size=30/></td>
              </tr>
              <tr>
                 	<td colspan="2" align="center"><input type="submit" name="submit" value="Submit" />&nbsp;&nbsp;&nbsp;<input type="reset" value="Clear" /></td>
              </tr>
              </form>
              </table>
<?php } ?>
 
Your code first produces an output (echo something, html, or even a blank space before the opening php tag) on line 171 and then it tries to modify the headers (redirect or session or cookie). Since the code you gave us doesn't have 171 lines, I assume there's more to the relevant you're showing.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
im using the same set of codes but running it in wamp server instead of xampp but i never encounter these problems in wamp is it the cause? or what?
 
Hi

That could mean that on line 171 is not an explicit output instruction, but something that generates an error, which is sent to the output too. So there could be a configuration difference between your two setups.

Feherke.
 
Hi

Oops, sorry.

That could mean that on line [red]6 of file login.php[/red] is not an explicit output instruction, but something that generates an error, which is sent to the output too. So there could be a configuration difference between your two setups.

Feherke.
 
what i mean is how come when i use the header function in wamp server no error arises but there is an error if im running it in xampp
 
Hi

Feherke said:
So there could be a configuration difference between your two setups.
And that configuration difference probably affects the operation on line 6.

I would bet on that line is a [tt]mysql_connect()[/tt]. And maybe the WAMP uses different php.ini which misses a line like this :
Code:
extension=mysql.dll
Could you check that ?

Feherke.
 
It is tough to determine why exactly or where since you are using functions for which we do not have the code.

That being said, if anything is sent to standard output, you will get said error upon attempting to execute the header(...) command/function.

As per why the error on one server and not the other, well, print and compare php_info() on both. One server could be set to not report errors even if they occur while the other is set to report them ... Check the php.ini files.

Hope this helps!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
hi, it finally worked all i did was to put the ob_start() at the top of my page and its now redirecting just fine im currently running it using wamp
 
that has not fixed the problem. it's only suppressing the symptom.

put this at the top of your page, and remove ob_start(). that will help diagnose.

Code:
ini_set('display_errors', true);
error_reporting(E_ALL);
 
Talk about a band-aid (patch) solution !!!

As suggested by jpadie, trouble-shoot the problem, identify its root cause and fix it there. What you've done is known in the business as a patch; do not allow yourself to simply patch bugs and convince yourself that they will not come back and bite you.

Besides, what fun is there if you walk away from the opportunity to learn something here?



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
doh !!, i looked as well. But in fairness it is embedded in other stuff, its a very common issue so if we could draw attention a bit more to it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top