PDA

View Full Version : Redirection and Counter Problem



admin2gd1
09-01-2008, 06:58 AM
OK i have 2 functions, one checks to see if the property has been sold, two updates the visitors counter on the property and the date of that last visit.

The first one does not work as I have a session file that already starts the header so I get this error.

Warning: Cannot modify header information - headers already sent by (output started at /home/account/public_html/listing.php:50) in /home/account/public_html/listing.php on line 50
The second one does not update the values that I have put. The code I have written is for when the visitor is on the property page so it should be collecting the information fine as the error appears for when trying to redirect to the new page. So the page is viewed like this in the address bar: - www.sitename.com/listing.php?id=1 (This is for when the property is still up for sale and if it's not for sale redirect to www.sitename.com/delisting.php?id=1).

This is what I have so far!


<?php
///// Collect information about the property /////
$sql = "SELECT * FROM ".TBL_PROPERTIES." WHERE property_id='$id' AND sale_type='1' AND status='1' AND active='1'";
$query = mysql_query($sql);
while ($result = @mysql_fetch_array($query)){
$id = $result['$property_id'];
$sale_type = $result['sale_type'];
$visits = $result['visits'];
$lastvisit = $result['$last_visit'];
}

///// Checks if property has been sold or removed. /////
if ($sale_type == 0){
header("Location: delisted.php?id=$id");
}

///// If property has not been sold or removed, continue to display property details. /////

///// Update Property Views Counter /////
if($visits = mysql_query("SELECT COUNT (visits) FROM ".TBL_PROPERTIES." WHERE property_id='$id' AND visits='$visits'")){
while($row = mysql_fetch_assoc($visits)){
$counter = $visits; //count the number of times the property has been viewed
if ($counter != "" || !empty($counter)){
$visits_counter = $counter +1; //add 1 to the counter when property is viewed
$lastvisit = date("Y:m:d H:i:s"); // update the date when property was last looked at
$sqlvisits = mysql_query("UPDATE ".TBL_PROPERTIES." SET visits='$visits_counter', last_visit='$lastvisit' WHERE property_id='$id'");
$query = mysql_query($sqlvisits);
}
}
}
?>
So if someone can help on this, it would be most appreciated.

Thank you.

BenInBlack
09-01-2008, 09:49 PM
you need to use the Output Buffering commands of php, because as soon as the first byte is sent to the client the header is gone. so if you want to do redirect in the middle of code then do this



<?php
//start output buffering
ob_start();
/*
Note: you do not have to close it because the end of file invokes an automatic output buffer close and sends the page to the browser
*/
///// Collect information about the property /////
$sql = "SELECT * FROM ".TBL_PROPERTIES." WHERE property_id='$id' AND sale_type='1' AND status='1' AND active='1'";
$query = mysql_query($sql);
while ($result = @mysql_fetch_array($query)){
$id = $result['$property_id'];
$sale_type = $result['sale_type'];
$visits = $result['visits'];
$lastvisit = $result['$last_visit'];
}

///// Checks if property has been sold or removed. /////
if ($sale_type == 0){
ob_clean();
header('HTTP/1.0 302 Found');
header("Location: delisted.php?id=$id");
exit();
}

///// If property has not been sold or removed, continue to display property details. /////

///// Update Property Views Counter /////
if($visits = mysql_query("SELECT COUNT (visits) FROM ".TBL_PROPERTIES." WHERE property_id='$id' AND visits='$visits'")){
while($row = mysql_fetch_assoc($visits)){
$counter = $visits; //count the number of times the property has been viewed
if ($counter != "" || !empty($counter)){
$visits_counter = $counter +1; //add 1 to the counter when property is viewed
$lastvisit = date("Y:m:d H:i:s"); // update the date when property was last looked at
$sqlvisits = mysql_query("UPDATE ".TBL_PROPERTIES." SET visits='$visits_counter', last_visit='$lastvisit' WHERE property_id='$id'");
$query = mysql_query($sqlvisits);
}
}
}
?>

admin2gd1
09-02-2008, 06:34 AM
sorry but nothing happened for updating counter but I did get 2 errors for the header saying can't start on the 2 header lines.


header('HTTP/1.0 302 Found');
header("Location: delisted.php?id=$id");

BenInBlack
09-02-2008, 09:19 AM
move ob_start up to first line, here is example top of all my php files


<?php
ob_start();
session_start();


and make the location a full url



header('HTTP/1.0 302 Found');
header("location: http://YOUR_DOMAIN/delisted.php?id={$id}");