PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
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.
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
<?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>
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
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.
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.
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.
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???
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 :-