• 0

Redirection and Counter Update Problem


Question

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 not redirect to the page I want redirected to above).

Here is my table structure:

TABLE IF NOT EXISTS `properties` (
  `property_id` int(11) NOT NULL auto_increment,
  `visits` varchar(255) collate utf8_bin NOT NULL,
  `last_visit` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`property_id`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

I would like it to also count 1 per IP location so that it's fare, but I don't know how to log that into the table, I might need one for stats where it logs the users IP address and browser, screen resolution and so on.

This is what I have written so far.

<?php
///// Get more information about the property /////
$sql = "SELECT * FROM properties WHERE property_id='$id' 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'];
$active = $result['$active'];
}
///// Checks if property has been sold or removed. /////
if ($sale_type == 1){
	header("Location: delisted.php?id=$id");
?>
<?php
///// If property has not been sold or removed, continue to display property details. /////
}
///// Update Property Views Counter /////
if ($visits != "" || !empty($visits)){
	$visits_counter = $visits +1;
   	$lastvisit  = date("Y:m:d H:i:s");
	$sqlvisits = mysql_query("UPDATE properties SET visits='$visits_counter' AND 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.

7 answers to this question

Recommended Posts

  • 0

hey, erm im not really sure what you want?

i can say thou to get browser info you use the varible $_SERVER

hey, erm im not really sure what you want?

i can say thou to get browser info you use the varible $_SERVER (may need $server but i think u need the underscore either way it gives u loads of info on the viewer =D)

  • 0
  hackncrap said:
hey, erm im not really sure what you want?

i can say thou to get browser info you use the varible $_SERVER

hey, erm im not really sure what you want?

i can say thou to get browser info you use the varible $_SERVER (may need $server but i think u need the underscore either way it gives u loads of info on the viewer =D)

What I would like is clearly written in my post. The purpose of this code is to count how many people have visited the property but I only want the counter to be updated per ip. Also the property viewing page checks if the property is or has been sold or not before loading the page. If the property is sold then the user is redirected to another page on the website.

  • 0

I have updated the code a bit but still not getting either function working.

<?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);
		}
	}
}
?>

The sql will not update the columns set to be updated and detecting on wither or not the property is sold or removed reads correcly.

If any one can help me on this I would mostly appreciate it.

The IP thing i will leave out for the moment so don't help me with that feature.

Edited by admin2gd1
  • 0

Well, for one, you could simplify that update counter quite a bit, like down to two lines.

$sqlvisits = mysql_query("UPDATE ".TBL_PROPERTIES." SET visits=visits+1, last_visit=UNIX_TIMESTAMP() WHERE property_id='$id'");
$query = mysql_query($sqlvisits);

I would suggest altering how you store dates and times. Use a standard integer field and store a unix timestamp. You can easily display the human readable date in any form from that, plus it's less work to store, and MySQL has a UNIX_TIMESTAMP() function built-in.

Second, it sounds like you already know you have another page with output, so I'm not sure why you are having problems with your header() function call. It needs to be called, if it's going to be called, before anything is output to the browser.

If you can't avoid that, I'd suggest a simple trick I use. Echo out some Javascript that does the same thing:

echo '&lt;script type="text/javascript"&gt;document.location.href = "delisted.php?id='.$id.'";</script>

  • 0
  metal_dragen said:
Well, for one, you could simplify that update counter quite a bit, like down to two lines.

$sqlvisits = mysql_query("UPDATE ".TBL_PROPERTIES." SET visits=visits+1, last_visit=UNIX_TIMESTAMP() WHERE property_id='$id'");
$query = mysql_query($sqlvisits);

I would suggest altering how you store dates and times. Use a standard integer field and store a unix timestamp. You can easily display the human readable date in any form from that, plus it's less work to store, and MySQL has a UNIX_TIMESTAMP() function built-in.

Second, it sounds like you already know you have another page with output, so I'm not sure why you are having problems with your header() function call. It needs to be called, if it's going to be called, before anything is output to the browser.

If you can't avoid that, I'd suggest a simple trick I use. Echo out some Javascript that does the same thing:

echo '&lt;script type="text/javascript"&gt;document.location.href = "delisted.php?id='.$id.'";</script>

The java trick did not work.

  • 0
  metal_dragen said:
Well, for one, you could simplify that update counter quite a bit, like down to two lines.

$sqlvisits = mysql_query("UPDATE ".TBL_PROPERTIES." SET visits=visits+1, last_visit=UNIX_TIMESTAMP() WHERE property_id='$id'");
$query = mysql_query($sqlvisits);

I would suggest altering how you store dates and times. Use a standard integer field and store a unix timestamp. You can easily display the human readable date in any form from that, plus it's less work to store, and MySQL has a UNIX_TIMESTAMP() function built-in.

I've never understood why people feel the need to store dates as unix timestamps instead of datetimes. There's certainly no real benefits to it. Getting MySQL or even PHP to reformat your dates to a human readable form (or even to a unix timestamp) is quick and easy. Date and datetime exist for a reason.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Samsung Galaxy Buds 3 Pro AI TWS Bluetooth Earbuds are just $110 with this deal by Sayan Sen If you are in the market for true wireless earbuds and have a budget of around $100, then take a look at the Samsung Galaxy Buds 3 Pro AI True Wireless Bluetooth Earbuds which are currently on sale for just $110 making it an absolute steal (purchase link under the specs table down below). The earbuds promise good sound quality, decent battery life, and comes with IPX7 water resistance. The Bluetooth version of the model is 5.3 and has a range of up to 10 meters assuming there are no obstructions between it and the transmitter. The technical specifications of the Samsung Galaxy Buds 3 Pro AI True Wireless Bluetooth Earbuds are given below: Specification Detail Model Number SM-R630 Driver Configuration 2-way (11 mm Woofer + Planar Tweeter) Audio Codecs SBC, AAC, Samsung Seamless Codec (up to 24-bit/96 kHz) Bluetooth Version Bluetooth 5.3 (Profiles: A2DP, AVRCP, HFP) Wireless Range Up to 10 m (without obstruction) Battery Capacity (Earbuds) 61 mAh (typical) Battery Capacity (Case) 515 mAh (typical) Playback Time (ANC On) Up to 5 hrs (earbuds) + 13 hrs (with case) Playback Time (ANC Off) Up to 8 hrs (earbuds) + 24 hrs (with case) Charging USB-C fast charge; Wireless Charging (Qi) Noise Control Adaptive Noise Control (ANC) & Ambient Sound with Galaxy AI Microphones 4-Mic system with machine-learning beamforming AI Features Adaptive EQ, Live Translate, Real-Time Interpreter, Siren/Voice Detect Water Resistance IPX7 (earbuds) / IPX2 (case) Controls Blade Touch (Swipe/Pinch/Press) Compatibility Android 8.0+ (2 GB RAM+), iOS 12+ (limited features) Supported OS Features Auto Switch, Auracast, Voice Command Get the Samsung earbuds at the link below: Samsung Galaxy Buds 3 Pro AI True Wireless Bluetooth Earbuds, Sound Optimization, Real-Time Interpreter, Redesigned Comfort Fit (International Version): $109.99 (Sold by Woot) As an Amazon and Woot Associate we earn from qualifying purchases.
    • Flip flop flip flop. This guy has no idea what he's doing.
    • Trump announces a 25% tariff on India, leaving smartphone manufacturers stunned by Hamid Ganji On Wednesday, President Trump announced that the US administration had decided to impose a 25 percent tariff on imported goods and products from India. The tariffs will take effect on Friday. In addition, India may face further penalties for engaging in trade with Russia, including the purchase of Russian oil. Trump said India is a friend to the United States, whose "tariffs are far too high, among the highest in the world." He also criticized India's weapons and oil deals with Russia, "when everyone wants Russia to STOP THE KILLING IN UKRAINE." India currently applies a 39 percent tariff on imported agricultural products and a 45 percent tax on vegetable oils. In response to the US's new tariffs, the Indian government said it's examining the implications of Trump's announcement while continuing the negotiations with Washington for a "fair, balanced and mutually beneficial bilateral trade agreement." The 25 percent tariff places India among the countries subject to the highest import duties when exporting to the US market. By comparison, imports from the European Union face a tariff of just 15 percent. After officially taking office, President Trump moved to further escalate the trade war with China by raising tariffs to their highest levels. As the world's leading manufacturer of smartphones for major brands such as Apple, Google, and Samsung, China's increased tariff burden was expected to drive up smartphone prices in the US market. The increase in tariffs on China has prompted an increasing number of tech companies to shift their manufacturing focus to India. For example, most iPhones sold in the US are now Indian-made, while the latest data by Canalys shows around 44 percent of all smartphones imported to the US are also made in India. While smartphone manufacturers initially hoped that shifting production to India would protect them from tariffs, the recent imposition of a 25 percent tariff has complicated the situation significantly. In April, the US government imposed a 27 percent tariff on imports from India, but later backed away from the plan. Now, however, it seems that the two sides have failed to find common ground in their trade negotiations.
    • OpenAI to build giant AI hub in Norway, tightening US grip on Europe's tech future by Paul Hill OpenAI has announced Stargate Norway, its first AI data center initiative in Europe under the OpenAI for Countries program. It has a planned 230MW capacity and is expected to host 100,000 Nvidia GPUs by the end of next year, with a significant future expansion hoped for. The site will be built by Nscale and Aker will help on the energy side of things, they will form a 50/50 joint venture, owning the site. The Stargate Norway announcement follows Stargate UAE and other partnerships, indicating that OpenAI is looking at a global strategy for its infrastructure needs. Stargate Norway will run entirely on renewable hydropower in Narvik, Norway. OpenAI cited low-cost energy, cool climate, and mature industrial base, explaining that these make it an ideal place for the project to take shape. The facility will run on 100% renewable energy and will use closed-loop, direct-to-chip liquid cooling to ensure maximum cooling efficiency. The excess heat from the GPU systems will be made available to help support low-carbon enterprises in the region. While renewable energy is often seen as an ethical choice, in the case of Stargate Norway, it is being chosen for entirely practical reasons. The data center will require loads of energy to power AI so using Norway’s hydropower makes a lot of sense. OpenAI said that the project aims to deliver on Norway’s sovereign AI goals and provide sovereign compute capacity in Europe. The announcement also stated that Aker and Nscale will provide priority access to Norway’s AI ecosystem and any surplus capacity will be available to the public and private sectors across the UK, Nordics, and Northern Europe. With the establishment of Stargate Norway, the country expects to see new jobs created, more economic activity, and AI research collaboration with local institutions. OpenAI called the deal “one of the most ambitious AI infrastructure investments in Europe to date.”
    • Because it's just a one liner fix. One person ( StartAllBack dev ) was able to fix it... in a cave!
  • Recent Achievements

    • Week One Done
      whiloh earned a badge
      Week One Done
    • Week One Done
      memnoch earned a badge
      Week One Done
    • First Post
      UAVXP earned a badge
      First Post
    • Dedicated
      Xinotema earned a badge
      Dedicated
    • Rookie
      MrNukes went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      661
    2. 2
      ATLien_0
      205
    3. 3
      Xenon
      133
    4. 4
      neufuse
      124
    5. 5
      Michael Scrip
      123
  • Tell a friend

    Love Neowin? Tell a friend!