Sayfa Görüntüleme PHP ve MySQL kullanarak karşı?

2 Cevap php

Ben birden çok sayfa mevcut olduğunda her sayfa için sayfa görünümleri saymak ve her sayfa için doğru sayfa görünümlerini böylece nasıl benim kod değiştirebilir merak ediyordum.

İşte aşağıda benim php kodudur.

//Adds one to the counter
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"UPDATE counter SET counter = counter + 1");

//Retreives the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT counter FROM counter"));

if (!$dbc) 
{
    // There was an error...do something about it here...
    print mysqli_error();
}

Ve burada benim MySQL kodudur.

CREATE TABLE `counter` ( `counter` INT( 20 ) NOT NULL );
INSERT INTO counter VALUES (0);

Bu daha önce benim alternatif tablo oldu.

CREATE TABLE `counter` (
  `id` int(11) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL default '',
  `page` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`),
 KEY `ip` (`ip`,`page`)
)

Bu benim yeni bir sayfa sayacı ile birleştirmek için çalışıyorum benim eski sayfa sayaç oldu.

//Get the viewer's IP Address and the page he / she is viewing
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];

//Check that the IP is not already listed for the current page
$viewer_check_query = "SELECT * FROM counter WHERE ip = '$ip' AND page = '$page'";
$viewer_check_result = mysql_query($viewer_check_query);
$viewer_check_numrows = mysql_num_rows($viewer_check_result);
	//If numrows is equal to zero, then the user is new
	if($viewer_check_numrows == 0){
		//Add the new entry
		$viewer_new_query = "INSERT INTO counter (ip, page) VALUES
		('$ip', '$page')";
		$viewer_new_result = mysql_query($viewer_new_query);
	}

//Get the total number of viewers for this page
$viewer_total_query = "SELECT * FROM counter WHERE page = '$page'";
$viewer_total_result = mysql_query($viewer_total_query);
$viewer_total_numrows = mysql_num_rows($viewer_total_result);

2 Cevap

Sana "canlı" veritabanını güncellemek için tavsiye etmem. Ama daha çok kullanmak mod_log_mysql veya Apache logfile "çevrimdışı" ayrıştırmak.

Neden değil? Eğer bir gün ziyaretçilerin bir sürü varsa veritabanı güncellendi kadar sayfa yükleme durak olur, merak ediyor olabilirsiniz. Veya veritabanı üzerinde ağır yük varsa güncelleme web sayfasının yüklenmesini durak olacaktır.

Vb ben aşağıdaki kodu kullanarak ediyorum tüm tarayıcıların oturum olmayacak şekilde ve bir daha uç, apache günlüğüne özel bir "LogEvent" yazmak için jQuery veya javascript kullanın:

 function logger() {
  var currenturl = location.href;
  $.get("/logthis", { url: currenturl } );
}
$(document).ready(function(){
   logger();
});

Sayfanızda yolunu içerecek bir alan "yol" eklemeniz gerekir.

CREATE TABLE `counter` (`counter` INT( 20 ) NOT NULL, 
             `path` VARCHAR(255) IS NOT NULL);

And in your request, you create a new empty counter if it doesn't exists yet. And you increment it if it already exists.

$mysqli = new mysqli("localhost", "root", "", "sitename");

// We get the counter the current URL
$counter = mysqli_query($mysqli, "SELECT counter 
                                  FROM counter 
                                  WHERE `path` = '" . $_SERVER['REQUEST_URI'] . "'";
if ($counter === NULL) 
{
    // If the counter does not exists yet, we create it
    mysqli_query($mysqli, "INSERT INTO counter 
                           VALUES (0, '" . $_SERVER['REQUEST_URI'] . "')";
}

// And we increment the counter
mysqli_query($mysqli, "UPDATE counter 
                       SET counter = counter + 1 
                       WHERE path = '" . $_SERVER['REQUEST_URI'] . "'");