Page 1 of 1

Redirect

Posted: Fri Nov 04, 2005 5:44 am
by bigdavemmu
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello!

I need a redirect to work in the middle of the page not before everything else. It won't work!!!! Here's my code:

Code: Select all

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3c.org/1999/xhtml" xml:lang="en" lang="en">


<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive">
<title>Connect To Database</title>
</head>
<body bgcolor="#ffffff">
		
<?PHP

// Connecting, selecting database
include('connect.inc');


// Performing SQL query
$pinID = $_REQUEST['thepin'];
//echo "$pinID";

//

$query = "select * from `keys` where `key` = '$pinID' ";
echo $pinID;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo '<br><br>';


if (mysql_num_rows($result)) {
  
 
  while ($ar = mysql_fetch_array($result)) {
    $keyID = htmlspecialchars(strip_tags($ar['keyID']));
    $key = htmlspecialchars(strip_tags($ar['key']));
    $used = htmlspecialchars(strip_tags($ar['used']));
    
  }

if ($key == $pinID && $used == 1){
header("Location: /membersin.php");

}elseif ($key == $pinID && $used == 0){
header("Location: /newmember.php");
//echo 'This is true';

}elseif ($key != $pinID){
header("Location: /members.php?wrong=1");

}else{
echo'Please Contact the Webmaster as there is an error!';
}

} else {
  print 'Please Contact the Webmaster as there is an error!';
}


	
?>
		
	</body>

</html>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Nov 04, 2005 6:11 am
by TJ
If you use header('Location: xxxx') it sends an HTTP redirect to the browser and any body is discarded.

If you want to display a page and then have it refresh to another page after a delay, put a meta-equiv REFRESH in the HEAD conditionally on the results of the tests.

Incidentally, you should be doing all your database work before the HEAD is sent so that the script can react to errors and conditions gracefully. You don't need to print the contents at that point - you can delay printing to a code block in the body of the document.

Once you've sent any body content the headers are gone - unless you use output buffering and discard it of course.

This isn't working either!

Posted: Fri Nov 04, 2005 6:33 am
by bigdavemmu
Thanks for the reply...but this isn't working either. Can you help? I'm new to PHP as you can probably see. Thanlks

Warning: Cannot modify header information - headers already sent by (output started at /home/.sites/64/site14/web/connect.inc:12) in /home/.sites/64/site14/web/login.php on line 34

Code: Select all

<?PHP

// Connecting, selecting database
include('connect.inc');


// Performing SQL query
$pinID = $_REQUEST['thepin'];
//echo "$pinID";

//

$query = "select * from `keys` where `key` = '$pinID' ";
echo $pinID;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo '<br><br>';


if (mysql_num_rows($result)) {
  
 
  while ($ar = mysql_fetch_array($result)) {
    $keyID = htmlspecialchars(strip_tags($ar['keyID']));
    $key = htmlspecialchars(strip_tags($ar['key']));
    $used = htmlspecialchars(strip_tags($ar['used']));
    
  }
//IF ALREADY A USER THAT HAS THE RIGHT PIN AND HAS VISITED BEFORE
if ($key == $pinID && $used == 1){
header("Location: /membersin.php");

//IF ALREADY A USER THAT HAS THE RIGHT PIN AND HAS NOT VISITED BEFORE
}elseif ($key == $pinID && $used == 0){
header("Location: /newmember.php");
//echo 'This is true';

//WRONG PIN NUMBER
}elseif ($key != $pinID){
header("Location: /members.php?wrong=1");

}else{
echo'Please Contact the Webmaster as there is an error!';
}

} else {
  print 'Please Contact the Webmaster as there is an error!';
}


	
?>

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3c.org/1999/xhtml" xml:lang="en" lang="en">


<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive">
<title>Connect To Database</title>
</head>
<body bgcolor="#ffffff">
</body>

</html>

Re: This isn't working either!

Posted: Fri Nov 04, 2005 6:45 am
by sheila
bigdavemmu wrote: Warning: Cannot modify header information - headers already sent by (output started at /home/.sites/64/site14/web/connect.inc:12) in /home/.sites/64/site14/web/login.php on line 34
Line 12 in connect.inc is generating some output. Also you still have an echo statements in the code you posted

Code: Select all

$query = "select * from `keys` where `key` = '$pinID' ";
echo $pinID;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo '<br><br>';
You need to remove those too or you'll get the same error message.

Posted: Fri Nov 04, 2005 6:48 am
by TJ
Do you see those debugging echo statements?

As far as PHP is concerned what they write is content and therefore headers are flushed. If you remove them the header() function calls will work as you expect.

When debugging like this, its sometimes better to have a string variable that you add messages to as the script progresses, and then you print it once you're into the body of the HTML when its safe.

e.g.

Code: Select all

<?php 

$debug='';

// do stuff
$debug .= 'Done some stuff<br/>';

// do more stuff
$debug .= 'Done more stuff<br/>';

?>
<html><head><title>Testing</title></head>
<body>
<?php echo $debug; ?>
</body>
</html>

Posted: Fri Nov 04, 2005 6:57 am
by Chris Corbyn
You can use output buffering to solve that issue too....

Search these boards for "headers AND output buffering" with all terms selected ;)

Posted: Fri Nov 04, 2005 7:13 am
by bigdavemmu
Hello and thanks for the replys...

The rason for the echos are so I know where I am in the code and what's happening once it's been processed. I don't want to print anything on this page...I just want a PHP page full of code. Does that mean I can get rid of all HTML???

And the headers thing I just do not understand. I am good at ASP and figured PHP would have a similar function to

Response.Redirect("some_url")

But it doesn't as far as I know.


Basically what I'm trying to do is;

If a user enters a pin and hasn't visited the site before, they're sent to a certain page.

If a user enters a pin and HAS visited the site before then they can commence to the default logged in page

If a user enters an incorrect pin, then theyre redirected to the login page.


How can I achieve this??

Thanks for all of your help

Posted: Fri Nov 04, 2005 7:27 am
by TJ
Response.Redirect("http://php.net") is the equivilent of header('Location: http://php.net');

PHP is just more faithful to HTTP - it lets you set the raw header rather than masking it. If you read up on the HTTP protocol (which all web-application developers should!) you'd see how useful header can be.

Posted: Fri Nov 04, 2005 7:28 am
by sheila
bigdavemmu wrote: The rason for the echos are so I know where I am in the code and what's happening once it's been processed. I don't want to print anything on this page...I just want a PHP page full of code. Does that mean I can get rid of all HTML???
Yes, and check line 12 of connect.inc.

Posted: Fri Nov 04, 2005 7:34 am
by bigdavemmu
TJ...very sorry if I offended you!

Shiela, this is the code in connect.inc

Code: Select all

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3c.org/1999/xhtml" xml:lang="en" lang="en">


<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive">
<title>Connect To Database</title>
</head>
<body bgcolor="#ffffff">
<?PHP
		
// Connecting, selecting database
$link = mysql_connect(':/var/lib/mysql/mysql.sock', '****, '****)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully<br><br>';
mysql_select_db('login')
or die('Could not select database<br><br>');
//echo 'Connected to LOGIN database<br><br>';

?>
</body>
</html>
Line 12 is<?PHP
I don't know why this would cause an error and how do you know line 12 is causing an error???

Thanks

Posted: Fri Nov 04, 2005 7:36 am
by shiznatix
viewtopic.php?t=1157

that will explain the headers problem a bit more. just remove all of the html from the connect.inc file

Posted: Fri Nov 04, 2005 7:37 am
by bigdavemmu
By joves!!!! I've got it!

The HTML in the connect.inc file stopped it from working. Thanks for the help people.

Posted: Fri Nov 04, 2005 7:38 am
by bigdavemmu
Just done that mate! THanks!

Cheers Shiela!

This may help you .....

Posted: Fri Nov 04, 2005 9:22 pm
by smartknightinc
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

This will help you :-

Code: Select all

function FunJavaScriptRedirection($url)
	{?>
		 <script type="text/javascript">
		 <!--
		 	window.location = "<?=$url?>"
		 //-->
		 </script>	 
	<?}
how to use it :-

Code: Select all

$url="http://www.google.com";
FunJavaScriptRedirection($url);
thanks ,

harry
__________________________________________________________
software development , software , php development


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]